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 was getting the same problem in "AVAudioPlayer" controller. Using "AVAudioSession.interruptionNotification" we can resume audio playing once interruption ended even though audio playing in the background.
@objc func interruptionNotification(notification: Notification) {
guard let type = notification.userInfo?[AVAudioSessionInterruptionTypeKey] as? UInt,
let interruption = AVAudioSession.InterruptionType(rawValue: type) else {
return
}
if interruption == .began{
print("Pause audio....")
self.player?.pause()
do {
try AVAudioSession.sharedInstance().setActive(false)
print("AVAudioSession is inactive")
} catch let error as NSError {
print(error.localizedDescription)
}
}
if interruption == .ended{
print("Play audio....")
do {
try AVAudioSession.sharedInstance().setActive(true)
print("AVAudioSession is Active again")
self.player?.play()
} catch let error as NSError {
print(error.localizedDescription)
}
}
}
Here first you need to InActive AVAudioSession when it's interrupted and Active AVAudioSession once interruption ended. It's working perfectly please try it!