关闭视频播放器如果视频播放Swift iOS

时间:2015-01-26 08:47:56

标签: ios swift video mpmovieplayercontroller

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

 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被删除玩了吗?

1 个答案:

答案 0 :(得分:8)

您需要订阅MPMoviePlayerPlaybackDidFinishNotification中的MPMoviePlayerController。播放器播放完视频后会发布此通知。

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

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

不要忘记从通知中心删除控制器,这是viewWillDisappear方法中的ussualy:

NSNotificationCenter.defaultCenter().removeObserver(self)

请参阅MPMoviePlayerController的文档here