iOS Swift App AVPlayerController dismiss not working on external display

狂风中的少年 提交于 2019-12-11 10:49:28

问题


I built an app to display several short movies for an exhibition, the app is fully functional on the iPad, but on external display the video will stay at the last frame. We want to use the iPad as external control for the display, but users should always see the video selection gui when no video is played

That is the snippet to start a video

let playerItem = AVPlayerItem(URL: NSURL(fileURLWithPath: videoPath!))
let player = AVPlayer(playerItem: playerItem)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(VideoCollectionViewController.playerDidReachEnd(_:)), name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem)

playerController.player = player
self.presentViewController(playerController, animated: true) {
    self.playerController.player!.play()
}

And here is the function called when it reaches the end.

func playerDidReachEnd(notification: NSNotification) {
    NSNotificationCenter.defaultCenter().removeObserver(self)

    playerController.player?.currentItem
    playerController.dismissViewControllerAnimated(true, completion: nil)
}

It does also not fall back to the app screen if the video is stopped using the "Done" button of the player

Is there any trick to make it dismiss the AVPlayerController so it would fall back to my app directly on every display?


回答1:


I totally forgot to post my answer here, in case anybody else might come to that problem.

The following code is to be placed in the playerDidReachEnd, not much different, but it removed the player from all screens.

playerController.dismissViewControllerAnimated(true, completion: nil)
playerController.view.removeFromSuperview()
self.presentedViewController?.dismissViewControllerAnimated(true, completion: nil)


来源:https://stackoverflow.com/questions/36353247/ios-swift-app-avplayercontroller-dismiss-not-working-on-external-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!