AVAudioSession: CarPlay: What session should be set While Playing any audio or Taking Input using Microphone.

What mode, category and different settings should be set while playing any audio or taking input using microphone.

One problem which I faced while playing Radio if I connect my phone to CarPlay system it doesn't plays audio. Also on finish of task setting Active state to false doesn't resumes Radio.

Below is my code which called while playing or recording:

Code Block swift
fileprivate func setSession(isRecording: Bool) throws {
    if #available(iOS 12.0, *) {
      var category: AVAudioSession.Category = .playback
      var mode: AVAudioSession.Mode = AVAudioSession.Mode.default
      if isRecording {
        category = .playAndRecord
        mode = .spokenAudio
      }
      try audioSession.setMode(mode)
      try audioSession.setCategory(category, options: [.duckOthers, .mixWithOthers])
    } else {
      let category: AVAudioSession.Category = isRecording ? .playAndRecord : .ambient
      try audioSession.setMode(AVAudioSession.Mode.default)
      try audioSession.setCategory(category, options: [.duckOthers, .mixWithOthers])
    }
    try audioSession.setActive(true)
  }


Below is code which called on finish of task:

Code Block swift
fileprivate func setSessionDeactivate() throws {
    try audioSession.setActive(false, options: [.notifyOthersOnDeactivation])
  }




AVAudioSession: CarPlay: What session should be set While Playing any audio or Taking Input using Microphone.
 
 
Q