没有调用Android监听器语音识别

时间:2013-02-01 18:18:27

标签: android listener

我正在尝试使用SpeechRecognizer类在Android上进行语音识别,并将其影响为RecognitionListener。

问题:未调用受影响的侦听器。

以下是我主要活动的代码:

package com.example.testvoicepaper;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

public int VOICE_RECOGNITION_REQUEST_CODE = 1;
private Button launchRecognition;
private TextView textRecognised;
private ImageView picture;
private SpeechRecognizer sr;
private Context context;
private MyRecognitionListener listener;

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

    textRecognised = (TextView) findViewById(R.id.textRecognised);
    launchRecognition = (Button) findViewById(R.id.launchRecognition);
    picture = (ImageView) findViewById(R.id.picture);
    context = this;

    sr = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
    listener = new MyRecognitionListener();
    sr.setRecognitionListener(listener);

    launchRecognition.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN: {
                sr.startListening(RecognizerIntent.getVoiceDetailsIntent(context));
                listener.onBeginningOfSpeech();
                break;
            }

            case MotionEvent.ACTION_UP: {
                listener.onEndOfSpeech();
                sr.stopListening();
                break;
            }
            }
            return false;

        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

class MyRecognitionListener implements RecognitionListener {

    @Override
    public void onBeginningOfSpeech() {
        Log.d("Speech", "onBeginningOfSpeech");
    }

    @Override
    public void onBufferReceived(byte[] buffer) {
        Log.d("Speech", "onBufferReceived");
    }

    @Override
    public void onEndOfSpeech() {
        Log.d("Speech", "onEndOfSpeech");
    }

    @Override
    public void onError(int error) {
        Log.d("Speech", "onError" + error);
    }

    @Override
    public void onEvent(int eventType, Bundle params) {
        Log.d("Speech", "onEvent");
    }

    @Override
    public void onPartialResults(Bundle partialResults) {
        Log.d("Speech", "onPartialResults");
    }

    @Override
    public void onReadyForSpeech(Bundle params) {
        Log.d("Speech", "onReadyForSpeech");
    }


    @Override
    public void onResults(Bundle results) {
        Log.d("Speech", "onResults");
        ArrayList strlist = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        for (int i = 0; i < strlist.size();i++ ) {
            Log.d("Speech", "result=" + strlist.get(i));
        }
    }

    @Override
    public void onRmsChanged(float rmsdB) {
        Log.d("Speech", "onRmsChanged");
    }

}
}

它调用了beginOfSpeech()和endOfSpeech(),但从不调用侦听器。

3 个答案:

答案 0 :(得分:1)

确保您的应用已启用RECORD_AUDIO权限。

答案 1 :(得分:0)

这是错误的Intent使用

RecognizerIntent.getVoiceDetailsIntent(context))

你想要像

这样的东西
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt);

查看GAST以获取更多首发内容,例如this class

答案 2 :(得分:0)

尝试在MainActivity上实施RecognitionListener,而不是创建&#39; MyRecognitionListener&#39;。