SpeechRecognizer: no selected voice recognition service

一笑奈何 提交于 2019-12-23 12:53:58

问题


This is how i start my RecogniseListener intent:

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);

However, i get a strange behavior. It works on some phones (Samsung galaxy S5, in this case), but i get the following error on Lenovo K50-T5:

E/SpeechRecognizer: no selected voice recognition service

This is my 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>

回答1:


That means either the user doesn't have a speech recognizer installed at all, or doesn't have one configured to run. There's nothing you can do to fix that, the user has to install one.




回答2:


In my case this warning case by user device if he/she not selected speech recognizer service by default here is photo of settings witch casing this problem, you see in photo no service selected by default,

i fix this by explicitly adding GoogleRecognitionService to my SpeechRecognizer,At the end my code look like this one

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

So your code look like you using default speech intent, make your own custom recognitionlistener here is link to How can I use speech recognition without the annoying dialog in android phones

Note: Make sure you have install Google app




回答3:


The error indicates there are no speech recognizer services available. You should test for this condition before calling SpeechRecognizer.createSpeechRecognizer.

import android.speech;

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



回答4:


To make Hamza's solution fool-proof, you can add the following checks, to pre-empt the different errors in the logs on 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;
}

and then use the same component name to initialise the speech recogniser

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


来源:https://stackoverflow.com/questions/42912347/speechrecognizer-no-selected-voice-recognition-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!