alphacep / vosk-android-demo

Offline speech recognition for Android with Vosk library.
Apache License 2.0
755 stars 207 forks source link

Anyone had success hooking up the demo with BlueTooth mic? #197

Open dhamya opened 1 year ago

dhamya commented 1 year ago

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?

Repkap11 commented 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);
dhamya commented 1 year ago

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

Repkap11 commented 1 year ago

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

dhamya commented 1 year ago

This is great, thanks a LOT @Repkap11

albert0m commented 1 year ago

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?

actor10 commented 10 months ago

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

dhamya commented 10 months ago

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