I am creating a CarPlay music app, everything works fine except when I get a call in between my music is playing. Carplay pauses music in between but when I end phone call its not resuming back. Please help if anyone has faced same issue
-
What do you use to play the audio - avplayer ? – Shawn Frank Feb 25 '22 at 12:13
-
Please provide enough code so others can better understand or reproduce the problem. – Community Feb 25 '22 at 18:01
-
@ShawnFrank yes i am using AVPlayer – Sagar Thukral Feb 26 '22 at 17:42
-
@Community there is no code i have applied to handle in between call support, i am assuming apple should handle it automaticaly as its doing for lock screen. – Sagar Thukral Feb 26 '22 at 17:44
2 Answers
CarPlay is not actually playing or pausing the music, it is still your app on the user's device that is doing all the work and CarPlay is just presenting an interface to interact with your app which is what you do in your CarPlaySceneDelegate
Therefore, the handling of how your app resumes after the call is not part of CarPlay.
You can try the following in your player management module:
1. Listen for audio interruption notifications
NotificationCenter.default
.addObserver(self,
selector: #selector(handleInterruption(_:)),
name: AVAudioSession.interruptionNotification,
object: nil)
2. Check the status of the interruption and handle accordingly
@objc
private func handleInterruption(_ notification: Notification)
{
// Check if we have valid information from the notification
// with regards to the interruption
guard let info = notification.userInfo,
let typeValue = info[AVAudioSessionInterruptionTypeKey] as? UInt,
let type = AVAudioSession.InterruptionType(rawValue: typeValue)
else
{
return
}
// We check if the interruption has begun
if type == .began
{
// Pause the AVPlayer in that case
player?.pause()
}
else if type == .ended
{
// We check if we have instructions on how to handle the interruptions
guard let optionsValue = info[AVAudioSessionInterruptionOptionKey] as? UInt else
{
return
}
// Convert the optionsValue into an AVAudioSession.InterruptionOptions
// which can be tested
let options = AVAudioSession.InterruptionOptions(rawValue: optionsValue)
// Check if the playback can resume after the interruption
if options.contains(.shouldResume)
{
// Request AVPlayer to resume the playback
player?.play()
}
}
}
Give this a try and see if it helps your situation
Update based on OP comments
The audio not re-starting is not related to CarPlay, if you app resumes audio after a call, this will be carried over to CarPlay.
With that being said, if you have not already, add this code when your player initializes:
UIApplication.shared.beginReceivingRemoteControlEvents()
Maybe my code does not work for your exact scenario but I believe you will need to use the AVAudioSession.interruptionNotification
which is what get's called when a phone call, siri or similar things interrupt your audio.
When this interruption has ended, you need to restart your audio.
I can't show you a CarPlay example, but here is an example of the above code in action when Siri interrupts the audio, the lock screen shows the status of the audio being paused, when Siri is dismissed, the app resumes the player as you can see.

- 4,381
- 2
- 19
- 29
-
I tried this, play pause works fine but timer on slider is not getting stopped neither on CarPlay nor on phone's lock screen. Without this code phone's Lock Screen works fine – Sagar Thukral Feb 28 '22 at 06:02
-
@SagarThukral - added a small update. No change in my answer though, you will have to add some logic by listening to `AVAudioSession.InterruptionOptions` and reacting accordingly – Shawn Frank Feb 28 '22 at 07:37
-
Shawn - Thanks for all your support. I have accepted your answer as I am halfway there, Additionaly I have added MPNowPlayingInfoCenter.default().nowPlayingInfo![MPNowPlayingInfoPropertyElapsedPlaybackTime] = "current time". But next what I am facing is when my application is not in background and running app directly from CarPlay system, after interruption song in not resuming until I press play again from CarPlay console. @ShawnFrank – Sagar Thukral Mar 01 '22 at 09:16
-
1So I found solution: - I was missing one line of code, "try AVAudioSession.sharedInstance() .setCategory(AVAudioSession.Category.playback)" this is must for maintaining audio session – Sagar Thukral Mar 03 '22 at 11:19
-
@SagarThukral - nice, you can post it as your solution if this worked. – Shawn Frank Mar 03 '22 at 13:25
So I found solution: - I was missing one line of code, "try AVAudioSession.sharedInstance() .setCategory(AVAudioSession.Category.playback)" this is must for maintaining audio session

- 70
- 2