Welcome! › Forums › Unity Plugins › Mobile Speech Recognizer › Audio Session on iOS › Reply To: Audio Session on iOS
April 16, 2019 at 11:50 pm
#1482
mansiva
Participant
What I ended up doing was forcing the AVAudioSession after stopping speech recognition. Here is the code although you might need different options:
#import <AVFoundation/AVFoundation.h>
void iOSAudio_setupAudioSession()
{
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback
mode:AVAudioSessionModeSpokenAudio
options:AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionAllowBluetooth
error:nil];
[session setActive:true error:nil];
}
Create a .m file (name doesn’t matter) and put in Plugins/iOS. To call add
[DllImport("__Internal")]
internal static extern void iOSAudio_setupAudioSession();
inside a class (I put it inside iOSSpeechRecognizer) and call right after StopIfRecording
. I ended up putting it inside SpeechRecognizer.cs so it ended up looking like this:
public static void StopIfRecording() {
Debug.Log("StopRecording...");
#if UNITY_IOS && !UNITY_EDITOR
iOSSpeechRecognizer._StopIfRecording();
iOSSpeechRecognizer.iOSAudio_setupAudioSession();
#elif UNITY_ANDROID && !UNITY_EDITOR
AndroidSpeechRecognizer.StopIfRecording();
#endif
}