如果播放了视频,请关闭视频播放器子视图 Swift iOS

Close Video Player Subview if video played Swift iOS

我尝试让我的视频播放器在播放完视频后关闭。

 func playVideoPepper() {

        var maxVideoCount:Int =  5 //AnzahlderVideos
        var Random = Int(arc4random_uniform(maxVideoCount + 0))
        var VideoNumber = Random + 1


        let path = NSBundle.mainBundle().pathForResource("pepper\(VideoNumber)", ofType:"mp4")
        let url = NSURL.fileURLWithPath(path!)
        moviePlayer = MPMoviePlayerController(contentURL: url)
        if let player = moviePlayer {
            player.view.frame = self.view.bounds
            player.prepareToPlay()
            player.scalingMode = .AspectFill
            player.controlStyle = .None
            self.view.addSubview(player.view)
            var time = player.duration
            println(time)
        }

    }

我试图通过 .duration 或 .playableduration 获取其中一个视频(随机播放 5 个视频)的时间,但两者都给了我 0.00 是否有任何简单的方法让 SubView 被删除,如果视频播放了吗?

您需要订阅 MPMoviePlayerPlaybackDidFinishNotification,共 MPMoviePlayerController。当播放器播放完视频时会发布此通知。

可以在viewWillAppear方法中添加控制器作为观察者:

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("playerDidFinish:"), name: MPMoviePlayerPlaybackDidFinishNotification, object: player)

不要忘记从通知中心删除控制器,这通常在 viewWillDisappear 方法中完成:

NSNotificationCenter.defaultCenter().removeObserver(self)

查看 MPMoviePlayerController 的文档here