我的应用程序已经进行了语音识别。一位用户告诉我它不适用于蓝牙耳机。大多数情况下,我能够通过蓝牙耳机进行语音识别(感谢另一位 Stack Overflow 贡献者,而不是官方 Android 文档)。
然而,有一个恼人的问题:当切换到使用蓝牙耳机时,它可能会在切换过程中短暂地消除音频输出。特别是,它有时可以省略通常提示用户进行语音识别的蜂鸣声。
我目前的解决方法是在蓝牙耳机连接到 SCO 音频通道后,在进行语音识别之前延迟一秒钟。
我的问题:有没有更可靠的方法来避免这种冲突?
static AudioManager sAudioManager;
static BluetoothHeadset sBluetoothHeadset;
static BluetoothDevice sBluetoothDevice;
static public void initBluetooth(Context pContext)
{
sAudioManager = (AudioManager) pContext.getSystemService(Context.AUDIO_SERVICE);
// Register to get notifications about Bluetooth profiles
sBluetoothAdapter.getProfileProxy(
pContext, new BluetoothProfileListener(), BluetoothProfile.HEADSET);
// Register to receive notification when the headset is dis/connected
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
pContext.registerReceiver(
new BluetoothHeadsetBroadcastReceiver(), intentFilter);
}
public static void startSpeechRecognition()
{
sAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
sAudioManager.startBluetoothSco();
sAudioManager.setBluetoothScoOn(true);
sBluetoothHeadset.startVoiceRecognition(sBluetoothDevice);
}
public static void stopSpeechRecognition()
{
sBluetoothHeadset.stopVoiceRecognition(sBluetoothDevice); …Run Code Online (Sandbox Code Playgroud)