admin管理员组文章数量:1122846
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.
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.
Share Improve this question edited Nov 21, 2024 at 23:54 Faraz Ahmed Khan asked Nov 21, 2024 at 23:46 Faraz Ahmed KhanFaraz Ahmed Khan 1113 silver badges12 bronze badges1 Answer
Reset to default 0For 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)
本文标签:
版权声明:本文标题:swift - How to Prevent WebRTC Audio and Mapbox Navigation Audio from Interrupting Each Other in iOS? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736306488a1932975.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论