取消AVPlayerViewController不会“杀死”对象-它会持续存在

时间:2019-09-28 17:02:38

标签: ios swift avplayerviewcontroller avkit

我正在播放应用程序捆绑包中的视频。

他们正在正确播放。

但是,当我调用关闭AVPlayerViewController时,它显然已从视图层次结构中删除,但是,如果我关闭iOS设备并再次将其重新打开,则在锁定屏幕上会显示一个媒体控件,以显示该视频和一个“播放”按钮。

如果您触摸“播放”,则只会获得音频而没有视频。

我的问题是我不明白为什么“解雇”在用完后并不能完全“杀死”玩家。

这是演示代码:

let path = Bundle.main.path(forResource: filename, ofType: type)

let url = NSURL(fileURLWithPath: path!)

let player = AVPlayer(url: url as URL)

NotificationCenter.default.addObserver(self,
                                       selector: #selector(VideoLibraryViewController.didFinishPlaying(notification:)),
                                       name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
                                       object: player.currentItem)

self.playerController = AVPlayerViewController()

self.playerController?.player = player

self.playerController?.allowsPictureInPicturePlayback = true

self.playerController?.showsPlaybackControls = YES

self.playerController?.delegate = self

self.playerController?.player?.play()

self.present(self.playerController!, animated: true, completion : nil)

以下是解雇代码:


// Delegate can implement this method to be notified when Picture in Picture will start.
func playerViewController(_ playerViewController: AVPlayerViewController, willEndFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator)
{
    self.playerController?.dismiss(animated: NO, completion: nil )
}

这是锁屏/控制中心上显示的系统范围媒体播放器中剩余的内容:

enter image description here

1 个答案:

答案 0 :(得分:1)

仅适用于iOS 13 SDK:这是解决方案,但答案是,尽管关闭了AVPlayerViewController,但它知道的AVPlayer对象是持久的,需要将其设置为nil。

private func killVideoPlayer()
{ 
    self.playerController?.player?.pause()
    self.playerController?.player  = nil

    self.playerController?.dismiss(animated: YES, completion: { self.playerController = nil })
}

以前的SDK,仍然无法正常工作。

都没有将AVAudioSession.active设置为false ...?!?!仍然需要iOS 13之前的SDK解决方案。