我正在用AVPlayer阅读30s的MP4视频。当它结束时,我想继续向后播放,然后正常播放,然后向后播放,依此类推...
使用以下代码可以正常工作,但每次我以其他方式再次播放视频时,我都会使用黑框。
以下是我的代码viewDidLoad
:
let playerVideoURL = Bundle.main.url(forResource: "my_video", withExtension: "mp4")!
self.backgroundPlayer = AVPlayer(url: playerVideoURL)
self.backgroundPlayerLayer = AVPlayerLayer(player: self.backgroundPlayer)
self.backgroundPlayerLayer.videoGravity = AVLayerVideoGravityResize
self.backgroundPlayer.volume = 0
self.backgroundPlayer.actionAtItemEnd = .none
self.backgroundPlayerLayer.frame = view.layer.bounds
self.view.layer.insertSublayer(self.backgroundPlayerLayer, at: 0)
NotificationCenter.default.addObserver(self,
selector: #selector(playerItemDidReachEnd(notification:)),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: self.backgroundPlayer.currentItem)
以下是每次视频播放完毕后调用的方法:
func playerItemDidReachEnd(notification: Notification) {
self.backgroundPlayer.rate *= -1
}
谢谢你,如果你有任何想法。我想这可能是因为当我改变费率时,视频没有立即缓存,但我在AVPlayer文档中找不到我要找的内容......