I am working on an iOS app that integrates:
WebRTC for a voice communication module.
Mapbox Navigation SDK for turn-by-turn navigation audio.
The challenge I'm facing is that when Mapbox Navigation, which uses SDK's UI NavigationViewController
, plays audio instructions (e.g., "Turn left in 100 meters"), it interrupts and stops the audio of the ongoing WebRTC voice communication. This behavior negatively impacts the user experience since both real-time communication and navigation are critical to the app.
Here is the WebRTC audio session setup I am using for the voice communication module:
func setupAudioSession() {
self.rtcAudioSession.lockForConfiguration()
do {
print(self.rtcAudioSession.categoryOptions)
try self.rtcAudioSession.setCategory(AVAudioSession.Category.playAndRecord.rawValue, with: [.mixWithOthers])
try self.rtcAudioSession.setMode(AVAudioSession.Mode.voiceChat.rawValue)
try self.rtcAudioSession.overrideOutputAudioPort(.speaker)
} catch let error {
debugPrint("Error changing AVAudioSession category: \(error)")
}
self.rtcAudioSession.unlockForConfiguration()
}
This configuration allows the WebRTC audio stream to work perfectly in isolation.
When Mapbox Navigation starts playing an audio instruction, it seems to reconfigure AVAudioSession and changes it's mode to AVAudioSessionModeDefault
while WebRTC set it to AVAudioSessionModeVoiceChat
previously, causing WebRTC audio to stop.
After the Mapbox audio finishes, WebRTC audio does not recover its original state.
My Goal
I want both WebRTC voice communication and Mapbox Navigation audio to work simultaneously without interruptions. WebRTC audio should not stop after Mapbox played instructions.
For anyone coming back later to find the answer.
I have achieved this using SpeechSynthesizing
Protocol.
Below is the code for reference:
class CustomSpeechSynthesizer: NSObject, SpeechSynthesizing {
var delegate: SpeechSynthesizingDelegate?
var muted: Bool = false
var volume: Float = 1.0
var isSpeaking: Bool = false
var locale: Locale? = .current
var managesAudioSession: Bool = true
private var currentInstruction: SpokenInstruction?
private let synthesizer = AVSpeechSynthesizer() // Persistent instance
override init() {
super.init()
synthesizer.delegate = self
}
func prepareIncomingSpokenInstructions(_ instructions: [SpokenInstruction], locale: Locale?) {
// Prepare instructions if needed
}
func speak(_ instruction: SpokenInstruction, during legProgress: RouteLegProgress, locale: Locale?) {
guard !muted else { return }
currentInstruction = instruction
do {
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, options: [.allowBluetooth, .allowBluetoothA2DP, .mixWithOthers])
try AVAudioSession.sharedInstance().setActive(true)
} catch let error as NSError {
print("Failed to set audio session category: \(error), code: \(error.code)")
}
let utterance = AVSpeechUtterance(string: instruction.text)
utterance.voice = AVSpeechSynthesisVoice(language: locale?.identifier)
utterance.volume = volume
synthesizer.speak(utterance)
}
using it like this:
// Create custom speech synthesizer and fallback
let customSpeechSynthesizer = CustomSpeechSynthesizer()
customSpeechSynthesizer.delegate = self
let speechSynthesizer = MultiplexedSpeechSynthesizer([customSpeechSynthesizer, SystemSpeechSynthesizer()])
// Configure the RouteVoiceController
let routeVoiceController = RouteVoiceController(navigationService: navigationService,
speechSynthesizer: speechSynthesizer)
// Set up NavigationOptions
let navigationOptions = NavigationOptions(navigationService: navigationService,
voiceController: routeVoiceController)
// Initialize NavigationViewController
let navigationMapViewController = NavigationViewController(for: response,
routeIndex: 0,
routeOptions: routeOptions,
navigationOptions: navigationOptions)
navigationMapViewController.modalPresentationStyle = .overFullScreen
self.present(navigationMapViewController, animated: true, completion: nil)