SpeechRecognizer:没有选定的语音识别服务

时间:2017-03-20 19:27:24

标签: java android speech-recognition speech-to-text

这就是我启动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);

然而,我得到一个奇怪的行为。它适用于某些手机(三星Galaxy 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>

4 个答案:

答案 0 :(得分:7)

在我的情况下,如果他/她没有默认选择语音识别器服务这个警告案例这里是设置女孩的照片,这个问题,你在照片中看到没有默认选择的服务,

enter image description here

通过向我的GoogleRecognitionService明确添加 SpeechRecognizer来解决此问题,最后我的代码看起来像这样

 this.speechRecognizer = SpeechRecognizer.createSpeechRecognizer(getActivity(), ComponentName.unflattenFromString("com.google.android.googlequicksearchbox/com.google.android.voicesearch.serviceapi.GoogleRecognitionService"));

所以你的代码看起来像你使用默认的语音意图,在这里制作你自己的自定义识别器是How can I use speech recognition without the annoying dialog in android phones的链接

注意:请务必安装Google app

答案 1 :(得分:5)

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

答案 2 :(得分:3)

错误表示没有可用的语音识别器服务。您应该在调用SpeechRecognizer.createSpeechRecognizer之前测试此条件。

import android.speech;

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

答案 3 :(得分:0)

要使Hamza's solution更加安全,可以添加以下检查,以抢占startVoiceRecognition()上日志中的不同错误

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("com.google.android.googlequicksearchbox/com.google.android.voicesearch.serviceapi.GoogleRecognitionService"));