我一直在编写一个聊天应用程序,以与蓝牙耳机/耳机配合使用。
到目前为止,我已经能够通过蓝牙耳机中的麦克风录制音频文件,并且
我已经能够使用RecogniserIntent等与Android设备的内置麦克风一起使用语音转文本功能。
但是我找不到让SpeechRecogniser通过蓝牙麦克风收听的方法,甚至可以这样做,如果可以的话,怎么办呢?
当前设备:三星Galax
安卓版本:4.4.2
编辑:我发现我的平板电脑设置中隐藏了一些语音识别器选项,其中之一是一个标记为“使用蓝牙麦克风”的复选框,但似乎没有作用。
最佳答案
找到了我自己问题的答案,因此我将其发布给他人使用:
为了获得语音识别以与Bluetooth Mic一起使用,您首先需要将设备作为BluetoothHeadset Object获取,然后在其上调用.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()并发送语音识别 Intent ,如下所示:
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);
}