You need to enable JavaScript to run this app. Porcupine唤醒词检测后无法启动SpeechRecognizer及PendingIntent问询
优惠活动
大模型
产品
解决方案
定价
更多

Porcupine唤醒词检测后无法启动SpeechRecognizer及PendingIntent问询

唤醒词检测与语音识别联动问题解答

核心问题排查

当前功能失效的根本原因来自调用时序错误、麦克风通道抢占、重复实例化三个方面:

  • 调用顺序完全颠倒:你在Porcupine的唤醒回调中,在listening()方法(该方法才会真正创建SpeechRecognizer实例)执行之前,就直接调用startRecognition(),此时speechRecognizer为null,调用startListening()自然会抛出异常。
  • 重复初始化语音识别:单次唤醒流程中,你在唤醒回调里直接调了一次startRecognition(),又在textSpeechInitialize()的TTS初始化成功回调里再次调用startRecognition(),会重复创建SpeechRecognizer实例,旧实例未被及时释放,系统就会提示需要先执行speechRecognizer.destroy()才能启动新的识别流程。
  • 麦克风通道抢占:Porcupine检测唤醒词的过程会持续占用麦克风采集通道,启动语音识别时没有暂停Porcupine的采集逻辑,两个音频采集任务同时抢硬件通道,会导致识别流程直接失效。

修复方案

  1. 调整唤醒回调的执行逻辑:检测到唤醒词后,先暂停Porcupine的音频采集,释放麦克风通道给语音识别流程
  2. 每次启动语音识别前,先判断speechRecognizer的状态:如果存在旧实例,先执行cancel()、destroy()释放资源,再初始化新实例
  3. 修正方法调用顺序:先执行listening()完成SpeechRecognizer初始化,再调用startListening()启动识别
  4. 语音识别流程结束(拿到识别结果/触发识别错误/超时)后,再恢复Porcupine的采集,进入下一轮唤醒监听

核心修复代码参考

// Porcupine 唤醒回调修正
(keywordIndex) -> {
    Log.i("YOU SAID IT!", "yesss");
    numUtterances++;
    try {
        // 暂停Porcupine采集,释放麦克风
        porcupineManager.stop();

        // 释放旧的语音识别实例
        if (speechRecognizer != null) {
            speechRecognizer.cancel();
            speechRecognizer.destroy();
            speechRecognizer = null;
        }

        // 初始化TTS,待问候语播报结束后再启动语音识别
        textSpeechInitialize();
    } catch (PorcupineException e) {
        Log.e("PORCUPINE", "暂停唤醒监听失败", e);
    }

    // 后续更新通知的逻辑保持不变
}
// 修正语音识别初始化与启动顺序
private void startRecognition() {
    // 先创建SpeechRecognizer实例,再启动监听
    listening();
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName());
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en");
    speechRecognizer.startListening(intent);
}
// 在SpeechRecognizer的结果/错误回调中,增加恢复Porcupine监听的逻辑
@Override
public void onResults(Bundle results) {
    // 原有识别结果处理逻辑
    try {
        // 识别完成,恢复唤醒词监听
        porcupineManager.start();
    } catch (PorcupineException e) {
        Log.e("PORCUPINE", "恢复唤醒监听失败", e);
    }
}

@Override
public void onError(int error) {
    String errorMessage = getErrorText(error);
    Log.i(">>> INFO", "Failed " + errorMessage);
    // 识别报错,也要恢复唤醒监听
    try {
        porcupineManager.start();
    } catch (PorcupineException e) {
        Log.e("PORCUPINE", "恢复唤醒监听失败", e);
    }
}

PendingIntent代码说明

你询问的代码段:

PendingIntent contentIntent = PendingIntent.getActivity(
        this,
        0,
        new Intent(this, MainActivity.class), // 这行的作用
        0);
  • 这行代码的作用是定义通知点击后跳转的目标页面:
    • 整个PendingIntent.getActivity()的作用,是生成一个可以授权给系统通知使用的跳转令牌,当用户点击状态栏上的应用通知时,系统会按照这个令牌携带的Intent配置,启动对应的页面
    • 传入的new Intent(this, MainActivity.class),表示跳转任务的目标是当前应用的MainActivity页面,构造时传入当前Service的Context作为跳转上下文
    • 最后一个参数传入的0,表示不对PendingIntent做特殊的更新、权限等配置

内容的提问来源于stack exchange,提问作者Vaibhav Patel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.22 21:36:09