vosk-android-demo icon indicating copy to clipboard operation
vosk-android-demo copied to clipboard

Anyone had success hooking up the demo with BlueTooth mic?

Open dhamya opened this issue 2 years ago • 7 comments

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?

dhamya avatar Feb 16 '23 14:02 dhamya

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);

Repkap11 avatar Feb 16 '23 21:02 Repkap11

Thanks @Repkap11 - I'll try this out. Do you have a fuller "gist" available for above?

dhamya avatar Feb 17 '23 02:02 dhamya

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

Repkap11 avatar Feb 17 '23 17:02 Repkap11

This is great, thanks a LOT @Repkap11

dhamya avatar Feb 17 '23 20:02 dhamya

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?

albert0m avatar Sep 08 '23 07:09 albert0m

From what I've read, Android restricts bluetooth mic usage to a few of their own apps.

actor10 avatar Jan 16 '24 14:01 actor10

fwiw: I ended up using a modified solution inspired by code @Repkap11 shared, seems to work,

dhamya avatar Jan 16 '24 18:01 dhamya