我有一个父视图,其中包含一个实例化AVPlayerViewController对象的播放按钮。如果用户按下播放器的默认“完成”按钮,则播放器视图将消失,父视图将按预期工作。但是,当播放到达视频结尾时,播放器消失,但父视图屏幕对按钮点击无响应。我不明白,因为两个场景都使用相同的回调来关闭玩家视图。
以下是我实例化AVPlayer的代码,并设置了两个有关如何完成播放的通知:
func playVideoLocally() {
// create player
let playerItem = AVPlayerItem(asset: AVAsset(URL: NSURL(string: media.url)!))
player = AVPlayer(playerItem: playerItem)
// create modal view with callback for done button
// This works fine
playerController = CustomAVPlayerViewController()
playerController.player = player
playerController.modalPresentationStyle = UIModalPresentationStyle.OverFullScreen
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "onLocalPlayCompletion:",
name: CustomAVPlayerViewController.viewWillDisappearNotification,
object: nil)
self.presentViewController(playerController, animated: true, completion: nil)
// create player completion callback
// This leads to unresponsive parent view
NSNotificationCenter.defaultCenter().addObserver(self, selector: "onLocalPlayCompletion:",
name: AVPlayerItemDidPlayToEndTimeNotification,
object: playerItem)
// play it
player.play()
}
这是两个场景都在调用的onLocalPlayCompletion回调:
func onLocalPlayCompletion(note: NSNotification) -> Void {
playerController.view.removeFromSuperview()
onCompletion()
}
答案 0 :(得分:0)
我发现了问题。按下"完成"的唯一原因没有影响父视图是因为它使用默认的AVPlayer动作来解雇自己。我试图从内部onLocalPlayCompletion(),
中解雇玩家playerController.view.removeFromSuperview()
不正确,在我的情况下,我用
替换它self.dismissViewControllerAnimated(true, completion: nil)
做我想要的。