Skip to content

Commit 201e5ef

Browse files
committed
Select built in mic and speaker for speech when sound modem is in use (uses external headset and mic)
1 parent ad5b2b1 commit 201e5ef

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

codec2talkie/src/main/java/com/radio/codec2talkie/app/AppWorker.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.media.AudioRecord;
1111
import android.media.AudioTrack;
1212
import android.media.MediaRecorder;
13+
import android.os.Build;
1314
import android.os.Handler;
1415
import android.os.Looper;
1516
import android.os.Message;
@@ -99,10 +100,10 @@ public AppWorker(TransportFactory.TransportType transportType,
99100
_processPeriodicTimer = new Timer();
100101
_recordAudioBuffer = new short[_protocol.getPcmAudioBufferSize()];
101102

102-
constructSystemAudioDevices();
103+
constructSystemAudioDevices(transportType);
103104
}
104105

105-
private void constructSystemAudioDevices() {
106+
private void constructSystemAudioDevices(TransportFactory.TransportType transportType) {
106107
int _audioRecorderMinBufferSize = AudioRecord.getMinBufferSize(
107108
AUDIO_SAMPLE_SIZE,
108109
AudioFormat.CHANNEL_IN_MONO,
@@ -119,15 +120,6 @@ private void constructSystemAudioDevices() {
119120
AudioFormat.ENCODING_PCM_16BIT,
120121
10 * _audioRecorderMinBufferSize);
121122

122-
/*
123-
AudioManager audioManager = (AudioManager)_context.getSystemService(Context.AUDIO_SERVICE);
124-
for (AudioDeviceInfo inputDevice : audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS)) {
125-
boolean isBuiltIn = inputDevice.getType() == AudioDeviceInfo.TYPE_BUILTIN_MIC;
126-
Log.i(TAG, "input device: " + isBuiltIn + " " + inputDevice.getProductName());
127-
if (isBuiltIn) _systemAudioRecorder.setPreferredDevice(inputDevice);
128-
}
129-
*/
130-
131123
int _audioPlayerMinBufferSize = AudioTrack.getMinBufferSize(
132124
AUDIO_SAMPLE_SIZE,
133125
AudioFormat.CHANNEL_OUT_MONO,
@@ -152,13 +144,36 @@ private void constructSystemAudioDevices() {
152144
.setBufferSizeInBytes(10 * _audioPlayerMinBufferSize)
153145
.build();
154146

155-
/*
147+
// Use built in mic and speaker for speech when sound modem is in use
148+
if (transportType == TransportFactory.TransportType.SOUND_MODEM) {
149+
selectBuiltinMicAndSpeakerEarpiece(isSpeakerOutput);
150+
}
151+
}
152+
153+
private void selectBuiltinMicAndSpeakerEarpiece(boolean isSpeakerOutput) {
154+
AudioManager audioManager = (AudioManager)_context.getSystemService(Context.AUDIO_SERVICE);
155+
156+
for (AudioDeviceInfo inputDevice : audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS)) {
157+
boolean isBuiltIn = inputDevice.getType() == AudioDeviceInfo.TYPE_BUILTIN_MIC;
158+
Log.i(TAG, "input device: " + isBuiltIn + " " + inputDevice.getProductName() + " " + inputDevice.getType());
159+
if (isBuiltIn) {
160+
boolean isSet = _systemAudioRecorder.setPreferredDevice(inputDevice);
161+
if (!isSet)
162+
Log.w(TAG, "cannot select input " + inputDevice.getProductName());
163+
break;
164+
}
165+
}
166+
156167
for (AudioDeviceInfo outputDevice : audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS)) {
157-
boolean isBuiltIn = outputDevice.getType() == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER;
158-
Log.i(TAG, "output device: " + isBuiltIn + " " + outputDevice.getProductName());
159-
if (isBuiltIn) _systemAudioRecorder.setPreferredDevice(outputDevice);
168+
boolean isBuiltIn = outputDevice.getType() == (isSpeakerOutput ? AudioDeviceInfo.TYPE_BUILTIN_SPEAKER : AudioDeviceInfo.TYPE_BUILTIN_EARPIECE);
169+
Log.i(TAG, "output device: " + isBuiltIn + " " + outputDevice.getProductName() + " " + outputDevice.getType());
170+
if (isBuiltIn) {
171+
boolean isSet = _systemAudioPlayer.setPreferredDevice(outputDevice);
172+
if (!isSet)
173+
Log.w(TAG, "cannot select output " + outputDevice.getProductName());
174+
break;
175+
}
160176
}
161-
*/
162177
}
163178

164179
public static int getAudioMinLevel() {

0 commit comments

Comments
 (0)