8000 [Android] Fixed bug for enableSpeakerphone. · simplewebrtc/flutter-webrtc@3bfecd9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3bfecd9

Browse files
committed
[Android] Fixed bug for enableSpeakerphone.
1 parent 789a4ca commit 3bfecd9

File tree

5 files changed

+1364
-7
lines changed

5 files changed

+1364
-7
lines changed

android/src/main/java/com/cloudwebrtc/webrtc/FlutterWebRTCPlugin.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.cloudwebrtc.webrtc.utils.ConstraintsMap;
1616
import com.cloudwebrtc.webrtc.utils.EglUtils;
1717
import com.cloudwebrtc.webrtc.utils.ObjectType;
18+
import com.cloudwebrtc.webrtc.utils.RTCAudioManager;
1819

1920
import java.io.UnsupportedEncodingException;
2021
import java.io.File;
@@ -72,6 +73,8 @@ public class FlutterWebRTCPlugin implements MethodCallHandler {
7273

7374
private AudioDeviceModule audioDeviceModule;
7475

76+
private RTCAudioManager rtcAudioManager;
77+
7578
public Activity getActivity() {
7679
return registrar.activity();
7780
}
@@ -124,6 +127,35 @@ private FlutterWebRTCPlugin(Registrar registrar, MethodChannel channel) {
124127
.setVideoDecoderFactory(new DefaultVideoDecoderFactory(eglContext))
125128
.setAudioDeviceModule(audioDeviceModule)
126129
.createPeerConnectionFactory();
130+
131+
rtcAudioManager = RTCAudioManager.create(registrar.context());
132+
// Store existing audio settings and change audio mode to
133+
// MODE_IN_COMMUNICATION for best possible VoIP performance.
134+
Log.d(TAG, "Starting the audio manager...");
135+
rtcAudioManager.start(new RTCAudioManager.AudioManagerEvents() {
136+
// This method will be called each time the number of available audio
137+
// devices has changed.
138+
@Override
139+
public void onAudioDeviceChanged(
140+
RTCAudioManager.AudioDevice audioDevice, Set<RTCAudioManager.AudioDevice> availableAudioDevices) {
141+
onAudioManagerDevicesChanged(audioDevice, availableAudioDevices);
142+
}
143+
});
144+
/*
145+
if (audioManager != null) {
146+
audioManager.stop();
147+
audioManager = null;
148+
}
149+
*/
150+
}
151+
152+
// This method is called when the audio manager reports audio device change,
153+
// e.g. from wired headset to speakerphone.
154+
private void onAudioManagerDevicesChanged(
155+
final RTCAudioManager.AudioDevice device, final Set<RTCAudioManager.AudioDevice> availableDevices) {
156+
Log.d(TAG, "onAudioManagerDevicesChanged: " + availableDevices + ", "
157+
+ "selected: " + device);
158+
// TODO(henrika): add callback handler.
127159
}
128160

129161
@Override
@@ -942,13 +974,7 @@ public void mediaStreamTrackSetMicrophoneMute(final String id, boolean mute) {
942974
}
943975

944976
public void mediaStreamTrackEnableSpeakerphone(final String id, boolean enabled) {
945-
AudioManager audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
946-
947-
try {
948-
audioManager.setSpeakerphoneOn(enabled);
949-
} catch (Exception e) {
950-
Log.e(TAG, "setSpeakerphoneOn(): error", e);
951-
}
977+
rtcAudioManager.selectAudioDevice(enabled? RTCAudioManager.AudioDevice.SPEAKER_PHONE : RTCAudioManager.AudioDevice.EARPIECE);
952978
}
953979

954980
public void mediaStreamAddTrack(final String streaemId, final String trackId, Result result) {

0 commit comments

Comments
 (0)
0