1

我使用 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")
}
4

1 回答 1

1

AVPlayerViewController 有它自己的 AVPlayerLayer 并且不知道您创建的 AVPlayerLayer。因此,行 playerLayer.addAnimation(fadeOut, forKey: nil) 无效。您应该设置或动画 AVPlayerViewController 的视图的 alpha 值。对于淡入淡出动画,您可能需要查看 +[UIView animateWithDuration:(...)] 方法并在完成处理程序中调用 self.streamPlayer.view.removeFromSuperview() 。

于 2016-06-10T16:28:03.037 回答