我有一个 AVPlayer
蕴含 UIViewController
. 我加了 AVPlayerItemDidPlayToEndTime
通知 UIViewController
这样我就可以重新启动我的 AVPlayer
播放完毕后 currentItem
NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object:player.currentItem)
@objc private func playerDidFinishPlaying(_ notification: Notification) {
guard let url = URL(string: self.video.alt_content) else { return }
let item = self.getAssetToPlay(url: url)
item.seek(to: .zero, toleranceBefore: .zero, toleranceAfter: .zero)
self.player.replaceCurrentItem(with: item)
player.play()
}
private func getAssetToPlay(url: URL) -> AVPlayerItem {
let asset = AVURLAsset(url: url)
let item = AVPlayerItem(asset: asset)
return item
}
但这个通知也是从 AppDelegate's
applicationDidBecomeActive
,比如我的应用从后台来。因此,而不是玩 AVPlayer
从当前时间开始,代码在以下情况下重启播放器 playerDidFinishPlaying
.
我需要一种方法,使当应用程序从后台开始播放,它是离开的地方。并在完全完成 currentItem
,它重新启动了 AVPlayerItem
所以问题是非常愚蠢的,说实话。這是由於我加入了 Observer
在初始化之前 AVPlayer
. 当我修改了代码后,它不再与 applicationDidBecomeActive