关闭时,AVPlayerViewController会弹出整个视图控制器堆栈

时间:2018-12-03 10:33:17

标签: swift avplayerviewcontroller

我正在使用AVPlayerViewController在我的应用程序中显示视频内容。可以通过一系列视图联系此玩家。该序列嵌入在导航视图控制器中。我的问题是,每当我关闭播放器时,都会从导航控制器中的视图控制器列表中弹出整个视图控制器堆栈,这意味着我被发送回主屏幕(但是我只想从中弹出AVPlayerViewController列表并返回到屏幕之前)。我试图找到一种方法来覆盖关闭按钮,但没有找到方法。此外,我尝试通过重新初始化整个视图控制器堆栈来推送通知并在主屏幕中处理它-此解决方案有效,但似乎不合适。我附加了从AVPlayerViewController继承的类和实现viewController的代码。感谢任何提示。

import UIKit
import AVKit
import AVFoundation

// MARK: - EduMediaVideoViewController
class EduMediaVideoViewController: AVPlayerViewController, EduMediaViewController {

    // MARK: Stored Type Properties
    weak var eduMediaDelegate: EduMediaElementDelegate?
    var videoMedia: VideoMediaElement?

    // MARK: Lifecycle Methods
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpVideo()
    }

    // MARK: Instance Methods
    func setContent(content: MediaElement) {
        guard let videoMedia = content as? VideoMediaElement else {
            return
        }
        self.videoMedia = videoMedia
    }

    // MARK: Private Instance Methods
    private func setUpVideo() {
        let playerItem = videoMedia?.video
        let player = AVPlayer(playerItem: playerItem)
        self.player = player
        player.play()
    }

    private func updateVideoProgress() {
        self.eduMediaDelegate?.updateProgress(id: 1, progress: 1)
    }
}


// create an extension of AVPlayerViewController
extension EduMediaVideoViewController {
    // override 'viewWillDisappear'
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        guard let category = eduMediaDelegate?.getCategory() else {
            return
        }
        let dict = ["category": category]
        NotificationCenter.default.post(name: .kAVDismissNotification, object: nil, userInfo: dict)
    }
}

extension Notification.Name {
    static let kAVDismissNotification = Notification.Name.init("dismissing")
}

初始化视频控制器的代码:

let eduVideoViewController = EduMediaVideoViewController()
eduVideoViewController.setContent(content: mediaElement)
eduVideoViewController.eduMediaDelegate = self
navigationController?.pushViewController(eduVideoViewController, animated: false)

0 个答案:

没有答案