Android语音识别不说话

时间:2019-02-01 12:26:04

标签: android speech-recognition

我正在制作一个应用程序,在其中我问一个问题,答录机根据问题回答我,但是问题是手机在听,但没有响应,就是说不讲话。

她正在接受欢迎的开放式应用程序中发言,但无话可说

我的代码

公共类MainActivity扩展了AppCompatActivity {

private TextToSpeech myTTS;
private SpeechRecognizer mySpeechReconizer;

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

    Button mic = (Button) findViewById(R.id.mic);
    mic.setOnClickListener((view) ->
    {

            Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            i.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
            mySpeechReconizer.startListening(i);


    });

    initializeTextToSpeech();
    initializeSpeechReconizer();
}

private void initializeSpeechReconizer() {
    if (SpeechRecognizer.isRecognitionAvailable(this)) {
        mySpeechReconizer = SpeechRecognizer.createSpeechRecognizer(this);
        mySpeechReconizer.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 bundle) {
                List<String> res = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                processResult(res.get(0));

            }

            @Override
            public void onPartialResults(Bundle partialResults) {

            }

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

            }
        });
    }
}

private void processResult(String s) {
    s = s.toLowerCase();

    //Qual é o teu nome?

    if (s.indexOf("What") != -1){
        if (s.indexOf("your name") != -1){
            speak("two");
        }
        if (s.indexOf("time") != -1){
            Date now = new Date();
            String time = DateUtils.formatDateTime(this,now.getTime(),
                    DateUtils.FORMAT_SHOW_TIME);
            speak(time);
        }
    }

     else if (s.indexOf("open") != -1){
        if (s.indexOf("browser") != -1){
        }
            speak("Sou a Lian, fui fundada pelo Leandro e moro em Baião.");
    }
}

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, "Teste", Toast.LENGTH_LONG).show();
                finish();
            } else {
                Locale l1 = new Locale("pt","PT");
                myTTS.setLanguage(l1);
                speak("Olá Leandro, estou pronta,podes falar!");
            }
        }
    });
}

private void speak(String s) {
    if (Build.VERSION.SDK_INT >=21){
        myTTS.speak(s,TextToSpeech.QUEUE_FLUSH,null,null);
        } else{
        myTTS.speak(s,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);
}

@Override
protected void onPause() {
    super.onPause();
    myTTS.shutdown();
}

}

0 个答案:

没有答案