Android Studio-语音识别器-onResults

时间:2019-03-07 21:44:17

标签: java android

任何人都认为为什么此代码不调用onResults方法?

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import java.util.List;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

private TextToSpeech myTTS;
private SpeechRecognizer mySpeechRecognizer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            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,3);
            mySpeechRecognizer.startListening(intent);
        }
    });
    initializeTextToSpeech();
    initializeSpeechRecognizer();
}

private void initializeSpeechRecognizer() {
    if(SpeechRecognizer.isRecognitionAvailable(this)){
        mySpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
        mySpeechRecognizer.setRecognitionListener(new RecognitionListener() {
            @Override
            public void onReadyForSpeech(Bundle params) {

            }

            @Override
            public void onBeginningOfSpeech() {

            }

            @Override
            public void onRmsChanged(float rmsdB) {

            }

            @Override
            public void onBufferReceived(byte[] buffer) {

            }

            @Override
            public void onEndOfSpeech() {

            }

            @Override
            public void onError(int error) {

            }

            @Override
            public void onResults(Bundle results) {
                speak("im here");
                List<String> result = results.getStringArrayList(
                        SpeechRecognizer.RESULTS_RECOGNITION
                );
                processResult(result.get(0));
            }

            @Override
            public void onPartialResults(Bundle partialResults) {

            }

            @Override
            public void onEvent(int eventType, Bundle params) {

            }

        });
    }
}

private void initializeTextToSpeech() {
    myTTS=new TextToSpeech(this, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if(myTTS.getEngines().size()==0) {
                Toast.makeText(MainActivity.this, "There's no TTS engine on this device", Toast.LENGTH_LONG).show();
                finish();
            }
            else{
                myTTS.setLanguage(Locale.US);
                speak("Hello Im ready");
            }
        }
    });
}

private void speak(String message){
    if(Build.VERSION.SDK_INT>=21){
        myTTS.speak(message,TextToSpeech.QUEUE_FLUSH,null,null);
    }
    else
        myTTS.speak(message,TextToSpeech.QUEUE_FLUSH,null);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
private void processResult(String command){
    command=command.toLowerCase();
    speak(command);
}S
@Override
protected void onPause(){
    super.onPause();
    myTTS.shutdown();
}
}

-在我的AndroidManifest.xml中,我获得了uses-permission,我真的不知道有什么问题,欢迎任何意见,看来该应用程序在某种程度上跳过了initializeSpeechRecognizer();。该应用程序仅应重复我说的内容,因此,为什么我的processResult方法仅包含speak(“ command”);

1 个答案:

答案 0 :(得分:0)

问题是我的虚拟设备出于某种原因未与PC麦克风配合使用,但在实际计算机上可以正常工作。