SpeechRecognizer听不到第一个结果后, [英] SpeechRecognizer not Hearing After First Result

查看:463
本文介绍了SpeechRecognizer听不到第一个结果后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SpeechRecognizer和RecognizerIntent在Android中实现语音识别。我的目的是要重新启动听完演讲后,我的语音识别在屏幕上显示的结果。为此,我使用下面的code。

现在的问题是,第一次运行正常,并显示结果,但它开始听第二次(从onResults方法调用)后,它并没有听到别人讲的某些原因。然后,它给出了一个ERROR_SPEECH_TIMEOUT误差,这意味着没有语音输入。在logcat中,我可以看到它进入onReadyForSpeech(),但不知何故,它不会听到我在说什么。

有谁知道为什么会发生?是否继续听之后,它返回一个结果?它正确调用startListening再次明确?

 公共类VR扩展活动实现RecognitionListener {


    私有静态最终诠释VOICE_RECOGNITION_REQUEST_ code = 1234;
    私人TextView的vrtext;
    私人SpeechRecognizer讲话= NULL;
    私人意图的意图;
    私人字符串变量=VR;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.vr);

        vrtext =(TextView中)findViewById(R.id.vrtext);

    }

    @覆盖
    公共无效onResume()
    {
        听();
        super.onResume();
    }

    私人无效听()
    {
        演讲= SpeechRecognizer.createSpeechRecognizer(本);
        speech.setRecognitionListener(本);
        意图=新的意图(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_ preFERENCE,恩);
        intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,this.getPackageName());
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,3);

        speech.startListening(意向);
    }

    @覆盖
    保护无效的onPause(){
        super.onPause();
        // TODO自动生成方法存根

        如果(讲话!= NULL)
        {
            speech.destroy();
            Log.i(TAG,消灭);
        }

    }

    公共无效onBeginningOfSpeech(){
        // TODO自动生成方法存根
        Log.i(TAG,onbeginningofspeech);
    }

    公共无效onBufferReceived(byte []的为arg0){
        // TODO自动生成方法存根
        //Log.i(TAG,onbufferreceived);
    }

    公共无效onEndOfSpeech(){
        // TODO自动生成方法存根
        Log.i(TAG,onendofspeech);
    }

    公共无效onerror的(INT为arg0){
        // TODO自动生成方法存根
        Log.i(TAG,错误code:+为arg0);
    }

    公共无效的onEvent(INT为arg0,捆绑ARG1){
        // TODO自动生成方法存根
        Log.i(TAG的OnEvent);
    }

    公共无效onPartialResults(捆绑为arg0){
        // TODO自动生成方法存根
        Log.i(TAG,onpartialresults);
    }

    公共无效onReadyForSpeech(捆绑为arg0){
        // TODO自动生成方法存根
        Log.i(TAG,onreadyforspeech);
    }

    公共无效onResults(捆绑为arg0){
        // TODO自动生成方法存根
        Log.i(TAG,onresults);
        ArrayList的<字符串>比赛= arg0.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        字符串s =;
        对于(字符串结果:火柴)
            S + =结果+\ N的;

        vrtext.setText(多个);

        speech.startListening(意向);

    }

    公共无效onRmsChanged(浮点为arg0){
        // TODO自动生成方法存根
        //Log.i(TAG,onrmschanged);
    }

}
 

解决方案

是否继续听之后,它返回一个结果? 否

是正确的,再次打电话startListening明确? 是。

另外,如果你想保留的认同不断发生,你应该叫 startListening 如果再出现这样的一些错误:

  @覆盖
公共无效onerror的(INT错误code)
{
    如果((错误code == SpeechRecognizer.ERROR_NO_MATCH)
            || (错误code == SpeechRecognizer.ERROR_SPEECH_TIMEOUT))
    {
        Log.d(TAG,不承认什么);
        // 继续
        recognizeSpeechDirectly();
    }
    其他
    {
        Log.d(TAG,
                失败
                        + SpeechRecognitionUtil
                                .diagnoseError code(错误code));
    }
}
 

看看我的$ C $下使用 SpeechRecognizer 来检测某口头语言<一href="https://github.com/gast-lib/gast-lib/blob/master/library/src/root/gast/speech/activation/WordActivator.java"相对=nofollow>这里。

I am using SpeechRecognizer and RecognizerIntent in Android to implement speech recognition. My aim is to restart listening to speech after my speech recognizer displays the results on the screen. For that purpose, I am using the following code.

The problem is, the first time runs fine and displays the results but after it starts listening for the second time (called from onResults method), it does not hear what is being spoken for some reason. Then it gives a ERROR_SPEECH_TIMEOUT error, which means there was no speech input. On Logcat, I can see it entering onReadyForSpeech() but somehow, it won't hear what I am saying.

Does anyone know why this might happen? Does it keep listening after it returns a result? Is it correct to call startListening again explicitly?

public class VR extends Activity implements RecognitionListener {


    private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
    private TextView vrtext;
    private SpeechRecognizer speech = null;
    private Intent intent;
    private String TAG = "VR";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vr);

        vrtext = (TextView) findViewById(R.id.vrtext);  

    }

    @Override
    public void onResume()
    {
        listen();
        super.onResume();
    }

    private void listen()
    {
        speech = SpeechRecognizer.createSpeechRecognizer(this);
        speech.setRecognitionListener(this);
        intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
        intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);

        speech.startListening(intent);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // TODO Auto-generated method stub

        if(speech != null)
        {
            speech.destroy();
            Log.i(TAG,"destroy");
        }

    }

    public void onBeginningOfSpeech() {
        // TODO Auto-generated method stub
        Log.i(TAG, "onbeginningofspeech");
    }

    public void onBufferReceived(byte[] arg0) {
        // TODO Auto-generated method stub
        //Log.i(TAG, "onbufferreceived");
    }

    public void onEndOfSpeech() {
        // TODO Auto-generated method stub
        Log.i(TAG, "onendofspeech");
    }

    public void onError(int arg0) {
        // TODO Auto-generated method stub
        Log.i(TAG, "error code: " + arg0);
    }

    public void onEvent(int arg0, Bundle arg1) {
        // TODO Auto-generated method stub
        Log.i(TAG, "onevent");
    }

    public void onPartialResults(Bundle arg0) {
        // TODO Auto-generated method stub
        Log.i(TAG, "onpartialresults");
    }

    public void onReadyForSpeech(Bundle arg0) {
        // TODO Auto-generated method stub
        Log.i(TAG, "onreadyforspeech");
    }

    public void onResults(Bundle arg0) {
        // TODO Auto-generated method stub
        Log.i(TAG, "onresults");
        ArrayList<String> matches = arg0.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        String s = "";
        for (String result:matches)
            s += result + "\n";

        vrtext.setText(s);

        speech.startListening(intent);

    }

    public void onRmsChanged(float arg0) {
        // TODO Auto-generated method stub
        //Log.i(TAG, "onrmschanged");
    }

}

解决方案

"Does it keep listening after it returns a result?" No

"Is it correct to call startListening again explicitly?" Yes.

Also, if you want to keep the recognition happening continuously, you should call startListening again if some errors occur like this:

@Override
public void onError(int errorCode)
{
    if ((errorCode == SpeechRecognizer.ERROR_NO_MATCH)
            || (errorCode == SpeechRecognizer.ERROR_SPEECH_TIMEOUT))
    {
        Log.d(TAG, "didn't recognize anything");
        // keep going
        recognizeSpeechDirectly();
    }
    else
    {
        Log.d(TAG,
                "FAILED "
                        + SpeechRecognitionUtil
                                .diagnoseErrorCode(errorCode));
    }
}

Check out my code for using SpeechRecognizer to detect a certain spoken word here.

这篇关于SpeechRecognizer听不到第一个结果后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆