我尝试让视频播放器在视频播放后关闭。
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 在视频播放时被移除?
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("playerDidFinish:"), name: MPMoviePlayerPlaybackDidFinishNotification, object: player)
订阅通知,这应该在viewWillAppear
方法中完成,并且你应该在viewWillDisappear
方法中删除控制器NSNotificationCenter.defaultCenter().removeObserver(self)
。 - Marius Fanu