带蓝牙麦克风的Android语音识别器

我一直在写一个聊天应用程序,以配合蓝牙耳机/耳机.
到目前为止,我已经能够通过蓝牙耳机和麦克风录制音频文件
我已经能够使用Android设备的内置麦克风,使用RecogniserIntent等进行语音到文本的处理.

但我找不到让SpeechRecogniser通过蓝牙麦克风收听的方法.它甚至可以这样做,如果是这样,怎么样?

当前设备:三星galax

Android版本:4.4.2

编辑:我在语音识别器的平板电脑设置中发现了一些隐藏的选项,其中一个是标有“使用蓝牙麦克风”的复选框,但它似乎没有任何效果.

最佳答案
找到我自己的问题的答案,所以我发布给其他人使用:

为了使用蓝牙麦克风进行识别识别,您首先需要将设备作为BluetoothHeadset对象,然后在其上调用.startVoiceRecognition(),这会将模式设置为语音识别.

完成后,您需要调用.stopVoiceRecognition().

您可以获得BluetoothHeadset:

private void SetupBluetooth()
{
    btAdapter = BluetoothAdapter.getDefaultAdapter();

    pairedDevices = btAdapter.getBondedDevices();

    BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
        public void onServiceConnected(int profile,BluetoothProfile proxy)
        {
            if (profile == BluetoothProfile.HEADSET)
            {
                btHeadset = (BluetoothHeadset) proxy;
            }
        }
        public void onServicedisconnected(int profile)
        {
            if (profile == BluetoothProfile.HEADSET) {
                btHeadset = null;
            }
        }
    };
    btAdapter.getProfileProxy(SpeechActivity.this,mProfileListener,BluetoothProfile.HEADSET);

}

然后你得到调用startVoiceRecognition()并发送你的语音识别意图,如下所示:

private void startVoice()
{
    if(btAdapter.isEnabled())
    {
        for (BluetoothDevice tryDevice : pairedDevices)
        {
            //This loop tries to start VoiceRecognition mode on every paired device until it finds one that works(which will be the currently in use bluetooth headset)
            if (btHeadset.startVoiceRecognition(tryDevice))
            {
                break;
            }
        }
    }
    recogIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    recogIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

    recog = SpeechRecognizer.createSpeechRecognizer(SpeechActivity.this);
    recog.setRecognitionListener(new RecognitionListener()
    {
       .........
    });

    recog.startListening(recogIntent);
}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...