I need to implement error handling for AVPlayer
. I create an instance using AVPlayer(url: audioUrl)
where audioUrl
is an URL to a remote mp3 file.
I subscribe for notifications like this:
func observeAudioTrack() {
guard let currentItem = player.currentItem else { return }
notificationCenter.addObserver(self, selector: #selector(playerDidFail), name: .AVPlayerItemFailedToPlayToEndTime, object: currentItem)
notificationCenter.addObserver(self, selector: #selector(playerDidLogError), name: .AVPlayerItemNewErrorLogEntry, object: currentItem)
}
However, none of these methods is called when I test turning off the internet connection or using an invalid URL.
player.status
is readyToPlay
, player.error
is nil
.
I managed to receive AVPlayerItemNewErrorLogEntry
, but only for a stream (m3u8
format).
PS. I'm using iPhone 12 Pro with iOS 15.0 for testing.
CodePudding user response:
Change
object: currentItem
To
object: nil
CodePudding user response:
Try to register like this NotificationCenter.default.addObserver(self, selector: #selector(finishedPlaying), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerItem)