0

Any thoughts why doesnt this code invoke the onResults method ?

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();
}
}

-in my AndroidManifest.xml I got the uses-permission I really dont know whats wrong any opinion is welcomed, It seems like the app is somehow skipping initializeSpeechRecognizer();. The app is only supposed to repeat what I said so thats why my processResult method contains only speak("command");

Martin Lukas
  • 314
  • 5
  • 17
  • Have you verified that SpeechRecognizer.isRecognitionAvailable(this) actually returns true on the device you are running it on? – TofferJ Mar 07 '19 at 22:20
  • Yes, it is true but it seems like the virtual device doesnt catch the voice from my pc mic – Martin Lukas Mar 07 '19 at 22:23
  • Maybe this is helpful: https://stackoverflow.com/questions/5916521/how-can-you-make-the-android-emulator-support-speech-recognition – TofferJ Mar 07 '19 at 22:26

1 Answers1

0

The problem was that my virtual device for some reason didn't cooperate with PC mic, on real machine it works.

Martin Lukas
  • 314
  • 5
  • 17