I am trying Speech recognition sample. If I started to recognise my speech via microphone, then I tried to get iPhone\'s voice of that recognised text. It is working. But, v
After digging into the technical details its observed that, overrideOutputAudioPort()
temporarily changes the current audio route.
func overrideOutputAudioPort(_ portOverride: AVAudioSession.PortOverride) throws
If your app uses the playAndRecord
category, calling this method with the AVAudioSession.PortOverride.speaker option causes audio to be routed to the built-in speaker
and microphone
regardless of other settings.
This change remains in effect only until the current route changes or you call this method again with the AVAudioSession.PortOverride.none option.
try audioSession.setMode(AVAudioSessionModeDefault)
If you would prefer to permanently enable
this behavior, you should instead set the category's defaultToSpeaker
option. Setting this option will always route to the speaker rather than receiver if no other accessory such as headphones are in use.
In Swift 5.x above code looks like -
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(.playAndRecord)
try audioSession.setMode(.default)
try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
try audioSession.overrideOutputAudioPort(.speaker)
} catch {
debugPrint("Enable to start audio engine")
return
}
By setting mode to the measurement
, its responsible for minimize the amount of system-supplied signal
processing to input and output signals.
try audioSession.setMode(.measurement)
By commenting this mode and using default
mode responsible for permanently enabling
the audio route to the built-in speaker
and microphone
.
Thanks @McDonal_11 for you answer. Hope this will helps to understand the technical details.
Finally, I got Solution.
func startRecognise()
{
let audioSession = AVAudioSession.sharedInstance() //2
do
{
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
try audioSession.setMode(AVAudioSessionModeDefault)
//try audioSession.setMode(AVAudioSessionModeMeasurement)
try audioSession.setActive(true, with: .notifyOthersOnDeactivation)
try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
}
catch
{
print("audioSession properties weren't set because of an error.")
}
...
}
Once I comment this line, try audioSession.setMode(AVAudioSessionModeMeasurement)
, volume is working normal.