视频从“所有屏幕"完成后自动关闭AVPlayer [英] Automatically dismiss AVPlayer when video completes from ALL SCREENS

查看:73
本文介绍了视频从“所有屏幕"完成后自动关闭AVPlayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

视频播放完毕后,我希望它拉下AVPlayer并返回到主视图控制器.

When my video completes, I want it to pull down the AVPlayer and return to the Main View Controller.

我还想在连接到外部屏幕/显示器时将视频拉下来.

I also want to pull the video down from my external screen/display when that's connected.

我该怎么做?

我在这里尝试了几种不同的答案,到目前为止,它们都没有起作用.

I've tried a few different answers on here and none of them have worked so far.

注意:我的视频的名字是四个

NOTE: Four is the name of my video

@IBAction func fourVideoPlayButton(_ sender: Any) {

    if let path = Bundle.main.path(forResource: "Four", ofType: "mp4") {

        let fourVideo = AVPlayer(url: URL(fileURLWithPath: path))
        let fourVideoPlayer = AVPlayerViewController()
        fourVideoPlayer.player = fourVideo

        present(fourVideoPlayer, animated: true, completion: {
            fourVideo.play()
        })
    }
}

推荐答案

您需要添加观察者以检测玩家何时完成播放

You need to add observer to detect when player is finished playing by

NotificationCenter.default.addObserver(self, selector: Selector(("playerDidFinishPlaying:")), 
       name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: fourVideoPlayer.player.currentItem)

func playerDidFinishPlaying(note: NSNotification) {
    fourVideoPlayer.dismiss(animated: true, completion: nil)
}

希望获得帮助!

这篇关于视频从“所有屏幕"完成后自动关闭AVPlayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆