I playback a video asset through avplayer
and wish to carry out an motion when the merchandise has completed enjoying. For some motive, playerItemDidReachEnd
does not get referred to as and I am utterly misplaced as to why. I’ve additionally tried addPeriodicTimeObserver
to the participant and generally it really works, generally it does not.
Under is my code. Any steerage can be appreciated.
func setupVideo(with asset: AVAsset) {
let video = AVPlayerItem(asset: asset)
video.addObserver(self, forKeyPath: "standing", choices: [.new, .initial], context: nil)
avPlayerPlayback.replaceCurrentItem(with: video)
avPlayerPlayback.actionAtItemEnd = .none
let playbackLayer = AVPlayerLayer(participant: avPlayerPlayback)
playbackLayer.body = capturedVideoPlaybackView.body
playbackLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
capturedVideoPlaybackView.layer.addSublayer(playbackLayer)
print("Participant standing: (avPlayerPlayback.standing.rawValue)")
print("Participant merchandise period: (CMTimeGetSeconds(avPlayerPlayback.currentItem?.period ?? CMTime.zero))")
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "standing" {
if let merchandise = object as? AVPlayerItem {
if merchandise.standing == .readyToPlay {
// The AVPlayerItem is able to play
print("Merchandise is able to play")
DispatchQueue.principal.async { [weak self] in
guard let self = self else { return }
NotificationCenter.default.addObserver(self,
selector: #selector(playerItemDidReachEnd(notification:)),
identify: .AVPlayerItemDidPlayToEndTime,
object: self.avPlayerPlayback.currentItem)
self.toggleMedia(shouldHideVideo: false)
self.playVideo()
print("Participant standing: (avPlayerPlayback.standing.rawValue)")
print("Participant merchandise period: (CMTimeGetSeconds(avPlayerPlayback.currentItem?.period ?? CMTime.zero))")
print("enjoying video")
}
// Carry out any further setup or begin playback
} else if merchandise.standing == .failed {
// Deal with failure
print("Participant merchandise failed: (String(describing: merchandise.error?.localizedDescription))")
}
}
}
}
func playVideo() {
DispatchQueue.principal.async { [weak self] in
guard let self = self else { return }
// self.avPlayerPlaybackTimeObserver =
//
// self.avPlayerPlayback.addPeriodicTimeObserver(forInterval: CMTime(worth: 1, timescale: 2) , queue: .principal, utilizing: { time in
//
// if let currentItem = self.avPlayerPlayback.currentItem {
//
// let totalDuration = CMTimeGetSeconds(currentItem.period)
// let secondsElapsed = CMTimeGetSeconds(time)
//
// print(totalDuration)
// print(secondsElapsed)
//
// let progress = CGFloat(secondsElapsed / totalDuration)
//
// if progress >= 1 {
// self.handlePlaybackItemCompletion()
// }
// }
// })
self.avPlayerPlayback.play()
}
}
@objc func playerItemDidReachEnd(notification: Notification) {
// This operate is named when the AVPlayer finishes enjoying the merchandise
print("Playback completed")
handlePlaybackItemCompletion() // Name your completion dealing with technique
}