Milton90
Milton90

Reputation: 537

Use Android Speech Recognition so that it stops only at the press of a button

I want to use speech recognition to record until you press the stop button . Do you know any way to do this?

I tried a solution but it only works as long as you do not take breaks too long(4-5s). If you press the STOP button when it stops then starts again to work...

How can I correct it?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myText = (TextView) findViewById(R.id.STT);

    intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    recognizer = SpeechRecognizer
            .createSpeechRecognizer(getApplicationContext());
    recognizer.setRecognitionListener(this);

}

public void start(View view) {
    recognizer.startListening(intent);
}

public void stop(View view) {
    recognizer.stopListening();
}

@Override
public void onResults(Bundle res) {

    ArrayList<String> results = res
            .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

    myText.setText(results.get(0));
    // ...

    recognizer.startListening(intent);

}

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

@Override
public void onReadyForSpeech(Bundle params) {
    //...
}

@Override
public void onPartialResults(Bundle data) {
    //...
}

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

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

@Override
public void onEndOfSpeech() {
    //...
}

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

@Override
public void onBeginningOfSpeech() {
    //...
}

Upvotes: 2

Views: 3348

Answers (1)

Hoan Nguyen
Hoan Nguyen

Reputation: 18151

This is a JB problem maybe by design. For a work around you can implement the voice recognition in a service and then send update to your UI based on the results. For an implementation of service work around see Android Speech Recognition as a service on Android 4.1 & 4.2

Upvotes: 1

Related Questions