我使用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没有任何作用/使用AVPlayer它很好地消失了?
playerLayer.addAnimation(fadeOut, forKey: nil)
这简单地删除了AVPlayerViewController,如果我无法使其工作,则计划B!
//self.streamPlayer.view.removeFromSuperview()
print("VIDEO finished")
}
答案 0 :(得分:1)
AVPlayerViewController拥有自己的AVPlayerLayer,并且不知道您创建的AVPlayerLayer。这样,行 playerLayer.addAnimation(fadeOut,forKey:nil) 没有效果。您应该设置AVPlayerViewController视图的alpha值或设置其动画。对于淡入淡出动画,您可能需要查看+ [UIView animateWithDuration:(...)]方法并调用 self.streamPlayer.view.removeFromSuperview() 在完成处理程序中。