通过蓝牙耳机进行Android语音识别有时会忽略蜂鸣声以提示用户说话

时间:2016-10-14 13:39:57

标签: android bluetooth speech-recognition headset

我的应用已经进行语音识别。一位用户告诉我,它无法使用蓝牙耳机。大多数情况下,我能够通过蓝牙耳机获得语音识别功能(感谢另一个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);

    sAudioManager.stopBluetoothSco();
    sAudioManager.setBluetoothScoOn(false);
    sAudioManager.setMode(AudioManager.MODE_NORMAL);
}

static public void doSpeechRecognition(Context pContext)
{
    SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(pContext);
    Intent intent =
        new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(
        RecognizerIntent.EXTRA_LANGUAGE_MODEL,
        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    sr.startListening(intent);
}

static public class BluetoothHeadsetBroadcastReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context pContext, Intent pIntent)
    {
        int state = pIntent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
        if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED)
        {
            // CURRENT WORKAROUND
            // Delay speech recognition to avoid obliterating beep prompt

            try
            {
                Thread.sleep(1000);
            }
            catch (InterruptedException e)
            {
            };

            doSpeechRecognition(pContext);
        }
    }
}

static public class BluetoothProfileListener implements BluetoothProfile.ServiceListener
{
    @Override
    public void onServiceConnected(int pProfile, BluetoothProfile pProxy)
    {
        sBluetoothHeadset = (BluetoothHeadset) pProxy;

        List<BluetoothDevice> devices = pProxy.getConnectedDevices();
        int numDevices = devices.size();

        if (numDevices > 0)
        {
            BluetoothDevice device = devices.get(0);
            sBluetoothDevice = device;
        }
    }
}

0 个答案:

没有答案
相关问题