I am using AVPlayer for music playback. My problem is that after an incoming call, the player won\'t resume. How do I handle this when an incoming call comes?>
I have to wait for 2 seconds to make it work.
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.player.play()
}
The whole func:
func playerInterruption(notification: NSNotification) {
guard let userInfo = notification.userInfo,
let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,
let type = AVAudioSessionInterruptionType(rawValue: typeValue) else {
return
}
if type == .began {
// Interruption began, take appropriate actions (save state, update user interface)
player.pause()
}
else if type == .ended {
guard let optionsValue =
userInfo[AVAudioSessionInterruptionOptionKey] as? UInt else {
return
}
let options = AVAudioSessionInterruptionOptions(rawValue: optionsValue)
if options.contains(.shouldResume) {
// Interruption Ended - playback should resume
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.player.play()
}
}
}
}