10
10
import android .media .AudioRecord ;
11
11
import android .media .AudioTrack ;
12
12
import android .media .MediaRecorder ;
13
+ import android .os .Build ;
13
14
import android .os .Handler ;
14
15
import android .os .Looper ;
15
16
import android .os .Message ;
@@ -99,10 +100,10 @@ public AppWorker(TransportFactory.TransportType transportType,
99
100
_processPeriodicTimer = new Timer ();
100
101
_recordAudioBuffer = new short [_protocol .getPcmAudioBufferSize ()];
101
102
102
- constructSystemAudioDevices ();
103
+ constructSystemAudioDevices (transportType );
103
104
}
104
105
105
- private void constructSystemAudioDevices () {
106
+ private void constructSystemAudioDevices (TransportFactory . TransportType transportType ) {
106
107
int _audioRecorderMinBufferSize = AudioRecord .getMinBufferSize (
107
108
AUDIO_SAMPLE_SIZE ,
108
109
AudioFormat .CHANNEL_IN_MONO ,
@@ -119,15 +120,6 @@ private void constructSystemAudioDevices() {
119
120
AudioFormat .ENCODING_PCM_16BIT ,
120
121
10 * _audioRecorderMinBufferSize );
121
122
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
-
131
123
int _audioPlayerMinBufferSize = AudioTrack .getMinBufferSize (
132
124
AUDIO_SAMPLE_SIZE ,
133
125
AudioFormat .CHANNEL_OUT_MONO ,
@@ -152,13 +144,36 @@ private void constructSystemAudioDevices() {
152
144
.setBufferSizeInBytes (10 * _audioPlayerMinBufferSize )
153
145
.build ();
154
146
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
+
156
167
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
+ }
160
176
}
161
- */
162
177
}
163
178
164
179
public static int getAudioMinLevel () {
0 commit comments