Reputation: 146
I'm making a voice call in ReactNative
App
with Opentok ReactNaitve SDK
.
Does anyone know how I can switch between earpiece and speaker (mobile phone) when subscribing to a audio streaming?
Upvotes: 2
Views: 2665
Reputation: 146
I have written native functions for both platforms on this issue. Currently, I haven't figured out a better way to resolve it, so you can try my way.
I think you already know How to write a native module for android and iOS platforms, so I will only give you the main stuff.
For iOS:
RCT_EXPORT_METHOD(switchAudioOutput: (BOOL *)isSpeckerPhoneOn) {
NSError* error;
AVAudioSession* session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
[session setMode:AVAudioSessionModeVoiceChat error:&error];
RCTLogInfo(@"Setting audio output");
if (isSpeckerPhoneOn) // Enable speaker
{
[session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
RCTLogInfo(@"Enable Speaker");
}
else // Disable speaker
{
[session overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&error];
RCTLogInfo(@"Disable Speaker");
}
[session setActive:YES error:&error];
}
For Android:
@ReactMethod
public void switchAudioOutput(Boolean isSpeckerPhoneOn) {
AudioManager audioManager = (AudioManager)this.mContext.getSystemService(this.mContext.AUDIO_SERVICE);
if (isSpeckerPhoneOn) {
audioManager.setSpeakerphoneOn(true);
} else {
audioManager.setSpeakerphoneOn(false);
}
}
I hope it will help you get out of stuck. Good luck!
Upvotes: 5