关闭视频播放器子视图如果视频迅速iOS - Close Video Player Subview if video played Swift iOS

- 此内容更新于:2015-12-13
主题:

我试着让我的视频播放器关闭视频。我试图让时间多长时间的视频(5视频播放随机的)。或持续时间。playableduration但都给我一个0.00有什么简单的方法让子视图被删除,如果视频播放?

原文:

I try to let my Video player close after the video is played.

 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)
        }

    }

I tried to get the time how long one of the videos (random on of 5 videos is played) with .duration or .playableduration but both give me a 0.00 is there any easy way to let the SubView be removed if the video is played?

解决方案:
你需要订阅的。这个通知发布当玩家完成玩视频。您可以添加控制器作为观察者的方法:不要忘记删除控制器通知中心这是ussualy完成方法:看到MPMoviePlayerController这里的文档
原文:

You need to subscribe to MPMoviePlayerPlaybackDidFinishNotification, of the MPMoviePlayerController. This notification is posted when the player finished playing the video.

You can add the controller as an observer in the viewWillAppear method:

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

Don't forget to remove the controller from the notification center this is ussualy done in viewWillDisappear method:

NSNotificationCenter.defaultCenter().removeObserver(self)

See docs of MPMoviePlayerController here

楼主:我真的不知道如何订阅“Notificaitons”等,这只是一个小项目(从学习者)为我的女儿:(

(原文:I m really not sure how to Subscribe to the "Notificaitons" etc as this is just a small project (from a learner) for my daughter :()

网友:你可以订阅通知使用这个应该在方法和你应该删除方法的控制器

(原文:Well you can subscribe to notifications using NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("playerDidFinish:"), name: MPMoviePlayerPlaybackDidFinishNotification, object: player) this should be done in viewWillAppear method and you should remove the controller NSNotificationCenter.defaultCenter().removeObserver(self) in viewWillDisappear method)

楼主:你可以添加这个帖子:)?我愿意投票,将会是一个伟大的帮助其他人,而不是仅这是评论

(原文:could you add this as post :)? i would love to vote it up and would be a great help for others not just have this as comment)

网友:我有编辑我的回答反映说,发表评论

(原文:I have edited my answer to reflect what was said in that comment)