语音识别器:未选择语音识别服务

4
这是我启动RecogniseListener意图的方式:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);   
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

intent.putExtra("android.speech.extra.DICTATION_MODE", true);               
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,this.getPackageName());
intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,1);
sr.startListening(intent);

然而,我遇到了一些奇怪的问题。它在某些手机上可以正常工作(例如三星S5),但在联想K50-T5上却出现以下错误:

E/SpeechRecognizer: no selected voice recognition service

这是我的AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="lips.deafcommunication.deaflips">

<uses-permission android:name="android.permission.RECORD_AUDIO" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppThemeNoBar">
    <activity android:name=".MainActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".ChatInProgressActivity" android:screenOrientation="portrait"
                android:windowSoftInputMode="adjustPan"
                android:configChanges="keyboardHidden|orientation|screenSize"
        ></activity>
</application>
</manifest>

我之所以出现相同的错误,是因为如果您进入移动设备设置 > 语言和输入 > 语音输入 > 您会发现两个选项,默认情况下没有选择任何一个。 - Hamza
4个回答

7
在我的案例中,如果用户设备没有默认选择语音识别服务,则会出现此警告。以下是导致此问题的设置照片,您可以在照片中看到未选择服务:

enter image description here

我通过在我的SpeechRecognizer中明确添加GoogleRecognitionService 来解决这个问题。最终我的代码看起来像这样:
 this.speechRecognizer = SpeechRecognizer.createSpeechRecognizer(getActivity(), ComponentName.unflattenFromString("com.google.android.googlequicksearchbox/com.google.android.voicesearch.serviceapi.GoogleRecognitionService"));

所以,你的代码似乎使用了默认的语音意图,建议创建自己的自定义RecognitionListener。这里是一个链接:如何在Android手机中使用语音识别而不出现烦人的对话框

注意:请确保已安装Google应用程序


这是一个根本错误的解决方案:“googlequicksearchbox”可能未安装,或将来可能不会安装,或者可能不是用户想要使用的。正确的解决方案是通知用户没有安装/选择识别器,然后打开一个对话框,帮助用户进行安装/选择。 - Kaarel
告知用户,语音识别器不是UX的一部分,我只展示了问题的情况。我相信程序员足够聪明,在调用Google语音之前会通知用户没有语音识别器检查。 - Hamza
到目前为止,Hamza提供在createSpeechRecognizer中提供ComponentName的解决方案是唯一对我有效的方法。我无论如何都找不到在任何Android设备上“安装”识别器的位置或方式。 - AlanKley
1
请确保您已在手机上安装了Google应用程序,大多数手机都默认安装了该应用程序,但有时用户会禁用它。https://play.google.com/store/apps/details?id=com.google.android.googlequicksearchbox - Hamza
谢谢,那个有效。还可以加上这个检查,使其更加完善 https://dev59.com/86Dha4cB1Zd3GeqP_Stm#56319307 - AA_PV

6
错误表明没有可用的语音识别服务。在调用SpeechRecognizer.createSpeechRecognizer之前,应该测试这种情况。
import android.speech;

if(SpeechRecognizer.isRecognitionAvailable(this)) {
    SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(this);
} else {
    // SOME SORT OF ERROR
}

5

这意味着用户没有安装语音识别器,或者没有配置可运行的语音识别器。您无法解决此问题,用户必须安装一个语音识别器。


0
为了使Hamza's solution无懈可击,您可以添加以下检查,以预防在startVoiceRecognition()日志中出现的不同错误。
private static final String GOOGLE_RECOGNITION_SERVICE_NAME = "com.google.android.googlequicksearchbox/com.google.android.voicesearch.serviceapi.GoogleRecognitionService"

public static boolean isSpeechRecognizerAvailable() {
    if (sIsSpeechRecognizerAvailable == null) {
        boolean isRecognitionAvailable = context != null && context.getPackageManager() != null
                && SpeechRecognizer.isRecognitionAvailable(context);
        if (isRecognitionAvailable) {
            ServiceConnection connection = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {

                }

                @Override
                public void onServiceDisconnected(ComponentName name) {

                }
            };
            Intent serviceIntent = new Intent(RecognitionService.SERVICE_INTERFACE);
            ComponentName recognizerServiceComponent = ComponentName.unflattenFromString(GOOGLE_RECOGNITION_SERVICE_NAME);
            if (recognizerServiceComponent == null) {
                return false;
            }

            serviceIntent.setComponent(recognizerServiceComponent);
            boolean isServiceAvailableToBind = context.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE);
            if (isServiceAvailableToBind) {
                context.unbindService(connection);
            }
            sIsSpeechRecognizerAvailable = isServiceAvailableToBind;
        } else {
            sIsSpeechRecognizerAvailable = false;
        }
    }
    return sIsSpeechRecognizerAvailable;
}

然后使用相同的组件名称来初始化语音识别器

this.speechRecognizer = SpeechRecognizer.createSpeechRecognizer(context, ComponentName.unflattenFromString(GOOGLE_RECOGNITION_SERVICE_NAME));

1
"GOOGLE_RECOGNITION_SERVICE_NAME" 是什么? - user2983041

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接