Android SpeechRecognizer服务不会调用侦听器

时间:2014-01-28 01:01:47

标签: android speech-recognition

我使用SpeechRecognizer类实现语音识别的代码似乎是正确的......但它无论如何都不起作用。任何RecognitionListener方法中的日志语句都没有被记录。

我非常仔细地遵循Android参考文档,我没有看到我错过的内容。

我已添加了录音音频权限。

package com.example.voicetest;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class VoiceActivity extends Activity {


SpeechRecognizer recognizer;
TextView display;
private boolean listening;
private Button button1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_voice);

    listening=false;
    display=(TextView)findViewById(R.id.textView1);
    button1=(Button)findViewById(R.id.button1);





    recognizer=SpeechRecognizer.createSpeechRecognizer(this);

    recognizer.setRecognitionListener(
                new RecognitionListener()
                {

                    @Override
                    public void onBeginningOfSpeech() {
                        // TODO Auto-generated method stub
                        Log.i("test","onBeginning");
                    }

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

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

                    @Override
                    public void onError(int error) {
                        // TODO Auto-generated method stub
                        Log.i("test","onError");
                    }

                    @Override
                    public void onEvent(int eventType, Bundle params) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onPartialResults(Bundle partialResults) {
                        // TODO Auto-generated method stub
                        Log.i("test","onPartial");
                    }

                    @Override
                    public void onReadyForSpeech(Bundle params) {
                        // TODO Auto-generated method stub
                        display.setText("Ready and waiting..");
                        Log.i("test","onReady");
                    }

                    @Override
                    public void onResults(Bundle results) {
                        // TODO Auto-generated method stub
                        Log.i("test","onResults");
                        String result=stringListToString(results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION));

                        display.setText(result);

                    }

                    @Override
                    public void onRmsChanged(float rmsdB) {
                        // TODO Auto-generated method stub

                    }

                }
            );




}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.voice, menu);
    return true;
}

public void buttonOnClick(View v)
{
    if(!listening)
    {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); 
             recognizer.startListening(intent);
             Log.i("test","recognizer listening");

        listening=true;
        button1.setText("Stop Listening");

    }
    else
    {
        recognizer.stopListening();
        listening=false;
        Log.i("test","recognizer stopped");
        button1.setText("Listen");
    }
}

private String stringListToString(ArrayList<String> strings)
{
    String result="";

    for(int i=0;i<strings.size();i++)
    {
        result=result+strings.get(i)+'\n';
    }

    return result;
}

@Override
public void onPause()
{
    recognizer.stopListening();
}
}

2 个答案:

答案 0 :(得分:1)

您还需要

intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
                                     this.getPackageName());

答案 1 :(得分:1)

尝试以下方法:

  1. 注销错误代码。如果它提供服务器错误,那么您没有连接到互联网。

  2. 如果要使用离线语音识别,则需要下载语言模型。只需验证语音输入是否适用于您的键盘(按下麦克风按钮,说出一些内容并查看它是否正在转换)。如果没有得到识别,那么您的代码就没有错误,您只需要下载语音模型。