Reputation: 8995
I using AVPlayerViewController which I want to fadeout when the video ends... I can capture the end-of-video event, and I the code put together for AVPlayer works, but for the AVPlayerViewController not.
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)
This code works too, but goes full screen- I don't want full screen...
//self.presentViewController(self.streamPlayer, animated: true) {
// self.streamPlayer.player!.play()
//}
I use this?
self.view.addSubview(self.streamPlayer.view)
self.streamPlayer.player!.play()
The event capture ..
override func viewWillAppear(animated: Bool) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "finishedPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem)
}
The event Code ...
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
This line does nothing with AVPlayerViewController/With AVPlayer it fades out nicely ?
playerLayer.addAnimation(fadeOut, forKey: nil)
This simply removes AVPlayerViewController, plan B if I cannot make this work!
//self.streamPlayer.view.removeFromSuperview()
print("VIDEO finished")
}
Upvotes: 1
Views: 1094
Reputation: 141
AVPlayerViewController has it's own AVPlayerLayer and does not know about the AVPlayerLayer you created. Thus, the line playerLayer.addAnimation(fadeOut, forKey: nil) has no effect. You should set or animate the alpha value of AVPlayerViewController's view. For the fade animation you may want to look at the +[UIView animateWithDuration:(...)] methods and call self.streamPlayer.view.removeFromSuperview() in the completion handler.
Upvotes: 2