Open dhamya opened 1 year ago
I did this with reflection just before calling SpeechService.startListening()
@RequiresApi(api = Build.VERSION_CODES.M)
private static void setPreferredDeviceWithReflection(SpeechService service, AudioDeviceInfo audioDevice) {
if (audioDevice == null) {
Log.i(TAG, "No external mic requested");
return;
}
try {
Field recorderField = SpeechService.class.getDeclaredField("recorder");
recorderField.setAccessible(true);
AudioRecord recorder = (AudioRecord) recorderField.get(service);
if (recorder == null) {
Log.w(TAG, "Getting recorder with reflection failed");
return;
}
boolean worked = recorder.setPreferredDevice(audioDevice);
if (!worked) {
Log.w(TAG, "Unable to request that the mic be used");
}
} catch (IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();
}
}
An AudioDeviceInfo comes from
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
AudioDeviceInfo[] devices = audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS);
Thanks @Repkap11 - I'll try this out. Do you have a fuller "gist" available for above?
In my case I was using a USB mic. The code is from another project I work on and specifically targets a unique piece of hardware. https://github.com/Six15-Technologies/ST1-Examples/blob/master/vosk-speech-recognition/src/main/java/com/six15/vosk_speech_recognition/HudSpeechRecognitionHelper.java
This is great, thanks a LOT @Repkap11
thank you for the snippet! I tried connecting a generic bluetooth headset, it's recognized by the AudioManager and is set correctly as preferred device, but when I speak no words are received by the onPartialResult an onResult methods. As soon as I disable the bluetooth it works again. Any ideas?
From what I've read, Android restricts bluetooth mic usage to a few of their own apps.
fwiw: I ended up using a modified solution inspired by code @Repkap11 shared, seems to work,
Based on this link, it seems default mic on the android device will be used. Has anyone tried using BT mic connected to android device? Any ideas/pointers/forks?