我使用 AVPlayerViewController ,我想在视频结束时淡出...我可以捕获视频结束事件,并且我为 AVPlayer 放在一起的代码可以工作,但对于 AVPlayerViewController 不能。
self.playerItem = AVPlayerItem(URL: videoURL)
self.player = AVPlayer(playerItem: self.playerItem)
self.playerLayer = AVPlayerLayer(player: self.player)
self.streamPlayer = AVPlayerViewController()
self.streamPlayer.player = self.player
self.streamPlayer.view.frame = CGRect(x: 128, y: 222, width: 512, height: 256)
这段代码也可以,但是全屏显示-我不想要全屏...
//self.presentViewController(self.streamPlayer, animated: true) {
// self.streamPlayer.player!.play()
//}
我用这个?
self.view.addSubview(self.streamPlayer.view)
self.streamPlayer.player!.play()
事件捕获..
override func viewWillAppear(animated: Bool) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "finishedPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem)
}
事件代码...
func finishedPlaying(myNotification:NSNotification) {
let fadeOut = CABasicAnimation(keyPath: "opacity")
fadeOut.fromValue = 1.0
fadeOut.toValue = 0.0
fadeOut.duration = 8.0
fadeOut.delegate = self
fadeOut.setValue("video", forKey:"fadeOut")
fadeOut.removedOnCompletion = false
fadeOut.fillMode = kCAFillModeForwards
这条线对 AVPlayerViewController/With AVPlayer 没有任何作用,它会很好地淡出吗?
playerLayer.addAnimation(fadeOut, forKey: nil)
如果我不能完成这项工作,这只是删除了 AVPlayerViewController,计划 B!
//self.streamPlayer.view.removeFromSuperview()
print("VIDEO finished")
}