关闭视图时 AVPlayer 在设备上崩溃

AVPlayer Crashes on Device When Dismissing View

我在使用 AVPlayer 时遇到问题。我尝试了很多解决方案,但在我移动到不同的视图后它仍然崩溃。

class GuideVideo : BaseViewController{

var avPlayer: AVPlayer?
var avPlayerLayer: AVPlayerLayer?

override func viewDidLoad() {
    super.viewDidLoad()
    generateVideo()
}

func generateVideo () {
    let videoURLWithPath = data["VideoUrl"]
    let videoFilePath = NSURL(string: videoURLWithPath!)

    let avAsset: AVAsset = AVAsset.assetWithURL(videoFilePath) as! AVAsset
    let avPlayerItem = AVPlayerItem(asset: avAsset)
    avPlayer = AVPlayer(playerItem: avPlayerItem)
    avPlayerLayer = AVPlayerLayer(player: avPlayer)

    avPlayerLayer!.frame = self.videoView.bounds
    self.videoView.layer.addSublayer(avPlayerLayer)
    avPlayer!.play()
}

我还尝试从中删除观察者,因为我认为崩溃与零观察者有关。

override func viewWillDisappear(animated: Bool) {
    dispatch_async(dispatch_get_main_queue(),{
        if self.avPlayerLayer != nil {
            self.avPlayerLayer!.player.pause()
            NSNotificationCenter.defaultCenter().removeObserver(self.avPlayerLayer!)
            self.avPlayerLayer!.removeFromSuperlayer()
            self.avPlayerLayer = nil
        }
        self.avPlayer!.pause()
        self.avPlayer = AVPlayer()
    })
    super.viewWillDisappear(animated)
}

没有任何效果,崩溃没有提供任何数据。要么它崩溃而不指示一行或一般

Thread 1: EXC_BAD_ACCESS * Import thing to note is that this crash only happens on the iPhone 6/6+. Our iPhone 5C handles the class well. * I only get the crash after moving to another view controller or a different navigation stack, but a few seconds after the view had been dismissed.

谢谢,现在已经坐了 2 天的大部分时间了。

编辑: 这个问题显然与 SWReveal 有关。它会在实例生命周期结束之前取消分配实例。 接受了最佳方案,但问题与SWReveal有关。

  1. 尝试使用 MPMoviePlayerController 而不是 AVPlayer,对于简单的解决方案,MPMoviePlayerController 更容易 API。
  2. 将 MPMoviePlayerController 设为全局实例,使其可通过整个应用程序访问,然后在需要时对其进行初始化。
  3. 先尝试在本地播放视频,看看是否能解决问题