尝试使用SpeechRecognizer(Android语音识别),但它没有启动?

时间:2017-02-01 05:32:42

标签: android android-studio speech-recognition

这是我的代码:

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

    SetupButton();

}

private void SetupButton()
{
    Button createNewMessage = (Button) findViewById(R.id.button);

    createNewMessage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ListenForNewMessage();
        }
    });
}

private void ListenForNewMessage()
{
    final SpeechRecognizer newDeliverySpeech = SpeechRecognizer.createSpeechRecognizer(this);
    RecognitionListener newDeliveryRecognitionListener = new RecognitionListener() {
        @Override
        public void onReadyForSpeech(Bundle params) {
            Log.d("SpeechListening","onReadyForSpeech");
        }
        @Override
        public void onBeginningOfSpeech() {
            Log.d("SpeechListening","onBeginningOfSpeech");
        }

        @Override
        public void onRmsChanged(float rmsdB) {
            //do nothing
        }

        @Override
        public void onBufferReceived(byte[] buffer) {
            //do nothing
        }

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

        @Override
        public void onError(int error) {
            //do nothing
        }

        @Override
        public void onResults(Bundle results) {
            ArrayList<String> userMessage;
            userMessage = results.getStringArrayList(RESULTS_RECOGNITION);
            PushNewDelivery(userMessage);
        }

        @Override
        public void onPartialResults(Bundle partialResults) {
            //do nothing
        }

        @Override
        public void onEvent(int eventType, Bundle params) {
            //do nothing
        }
    };
    newDeliverySpeech.setRecognitionListener(newDeliveryRecognitionListener);
    if (newDeliverySpeech.isRecognitionAvailable(getApplicationContext()))
    {
        Log.d("SpeechListening","started listening hopefully");
        newDeliverySpeech.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));
    }
}

}

问题是,只记录了开始聆听希望RecognitionListener从未调用onReadyForSpeech()或其任何方法。

有人可以告诉我,我在这里做错了什么吗?

2 个答案:

答案 0 :(得分:1)

您可以在以下行

中进行更改
newDeliverySpeech.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));

要,

 Intent mSpeechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    mSpeechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    mSpeechIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
    mSpeechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "voice.recognition.test");
    mSpeechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
    newDeliverySpeech.startListening(mSpeechIntent);

答案 1 :(得分:0)

在致电startListening之前询问用户录制音频的权限:

String[] permissions = {Manifest.permission.RECORD_AUDIO};
ActivityCompat.requestPermissions(this, permissions, REQUEST_RECORD_AUDIO_PERMISSION);

https://developer.android.com/guide/topics/media/mediarecorder#audio-record-permission