skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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


  2. 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.

    CarPlay resume audio after phone call interruption AVPlayer Swift iOS

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search