Saturday, 23 January 2021

Android SpeechRecorder live speech to text preview in EditText

hope you are feeling great and safe. I have an app that records audio from the user and then displays it on screen as text in EditText. The problem I have is that the text-only appears after the user finishes speaking, What I want is to display the text while speaking "In short like google assistant".

What I have done:

  • I have searched on the internet and I found a way to do it, it is said you can overrides onPartialResults and then makes ArrayList to get the data "see below code". the way

          @Override
          public void onPartialResults(Bundle bundle) {
              micButton.setImageResource(R.drawable.ic_mic_on);
              ArrayList data = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
              String word = (String) data.get(data.size() - 1);
              editTextWriteText.setText(word);
    
              Log.i("TEST", "partial_results: " + word);
          }
    

But the problem was not solved, So how can I solve this problem?

Here's My code below.

.

MainActivity.java

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {

    EditText editTextWriteText;
    Button speakButton;
    private SpeechRecognizer speechRecognizer;
    ImageView micButton;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SpeechRecorder();
    }


    /*
     * Methods for SpeechRecorder button
     */
    private void SpeechRecorder () {
        micButton = findViewById(R.id.speak_button);
        speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
        final Intent speechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());

        speechRecognizer.setRecognitionListener(new RecognitionListener() {
            @Override
            public void onReadyForSpeech(Bundle bundle) {
                micButton.setImageResource(R.drawable.ic_mic_on);
            }

            @Override
            public void onBeginningOfSpeech() {
                editTextWriteText.setHint("Listening...");
            }

            @Override
            public void onRmsChanged(float v) {

            }

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

            }

            @Override
            public void onEndOfSpeech() {
            }

            @Override
            public void onError(int i) {
                micButton.setImageResource(R.drawable.ic_mic_off);
                editTextWriteText.setHint("Tap to enter text ...");
            }

            @Override
            public void onResults(Bundle bundle) {
                micButton.setImageResource(R.drawable.ic_mic_off);
                ArrayList<String> data = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                editTextWriteText.setText(data.get(0));
                editTextWriteText.setHint("Tap to enter text ...");
            }

            @Override
            public void onPartialResults(Bundle bundle) {
                micButton.setImageResource(R.drawable.ic_mic_on);
                ArrayList data = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                String word = (String) data.get(data.size() - 1);
                editTextWriteText.setText(word);

                Log.i("TEST", "partial_results: " + word);
            }

            @Override
            public void onEvent(int i, Bundle bundle) {
            }
        });

        micButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    speechRecognizer.stopListening();
                }
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    micButton.setImageResource(R.drawable.ic_mic_on);
                    speechRecognizer.startListening(speechRecognizerIntent);
                    textToSpeech.stop();
                }
                return false;
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        speechRecognizer.destroy();
    }
}

activity_main.xml

<EditText
    android:id="@+id/text_write"
    android:layout_width="match_parent"
    android:layout_height="@dimen/_390sdp"
    android:layout_above="@+id/speech_button"
    android:layout_below="@+id/text_name"
    android:layout_marginStart="@dimen/_11sdp"
    android:layout_marginTop="@dimen/_24sdp"
    android:layout_marginEnd="@dimen/_11sdp"
    android:layout_marginBottom="@dimen/_47sdp"
    android:background="@drawable/rounder_edit_text"
    android:elevation="2dp"
    android:gravity="center"
    android:hint="@string/edittext_hint"
    android:inputType="textMultiLine"
    android:ems="10"
    android:maxLength="900"
    android:paddingLeft="8dp"
    android:paddingTop="16dp"
    android:paddingRight="8dp"
    app:counterEnabled="true" />

<ImageView
    android:id="@+id/speak_button"
    android:layout_width="46dp"
    android:layout_height="46dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="@dimen/_12sdp"
    android:layout_marginBottom="@dimen/_13sdp"
    android:background="@drawable/ic_mic_off" />


from Android SpeechRecorder live speech to text preview in EditText

No comments:

Post a Comment