In this article, speech to text feature is implemented in an application in Android. Speech to text means that anything that the user says is converted into text. This feature has come out to be a very common and useful feature for the users. In various places where search feature is implemented like Google Search also in apps like google keyboard, etc because it gives users a great experience.
Approach:
Step 1: Add the below code in activity_main.xml. Here an image view for the mic icon and a textview to show the text that is converted from the speech is added.
activity_main.xml
<? xml version = "1.0" encoding = "utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > < ImageView android:id = "@+id/iv_mic" android:layout_width = "60dp" android:layout_height = "60dp" android:layout_alignParentTop = "true" android:layout_alignParentEnd = "true" android:layout_marginTop = "204dp" android:src = "@drawable/ic_mic" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintHorizontal_bias = "0.498" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toTopOf = "parent" /> < TextView android:id = "@+id/tv_speech_to_text" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignParentTop = "true" android:layout_alignParentEnd = "true" android:layout_centerHorizontal = "true" android:layout_marginTop = "44dp" android:textSize = "30sp" android:padding = "10dp" android:text = "@string/tap_mic_and_speek" app:layout_constraintEnd_toEndOf = "@+id/iv_mic" app:layout_constraintHorizontal_bias = "0.489" app:layout_constraintStart_toStartOf = "@+id/iv_mic" app:layout_constraintTop_toBottomOf = "@+id/iv_mic" /> </ androidx.constraintlayout.widget.ConstraintLayout > |
Step 2: Add the below code in MainActivity.java. Here onClickListener is added with the mic icon so when the user clicks on the icon(image) of mic it is invoked. RecognizerIntent.ACTION_RECOGNIZE_SPEECH is used in the listener that starts an activity that prompts the user for speech and send it through a speech recognizer. The results will be returned via activity results in the onActivityResult() method, when the intent is started using startActivityForResult(). In onActivityResult() method a list of strings is returned and the text is replaced with it in the textview.
MainActivity.java
class MainActivity extends AppCompatActivity(){ private ImageView iv_mic; private TextView tv_Speech_to_text; private static final int REQUEST_CODE_SPEECH_INPUT = 1 ; @Override protected void onCreate(Bundle savedInstanceState){ super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv_mic = findViewById(R.id.iv_mic); tv_Speech_to_text = findViewById(R.id.tv_speech_to_text); iv_mic.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak to text" ); try { startActivityForResult(intent, REQUEST_CODE_SPEECH_INPUT); } catch (Exception e) { Toast .makeText(MainActivity. this , " " + e.getMessage(), Toast.LENGTH_SHORT) .show(); } } }); } @Override protected void onActivityResult( int requestCode, int resultCode, @Nullable Intent data) { super .onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE_SPEECH_INPUT) { if (resultCode == RESULT_OK && data != null ) { ArrayList<String> result = data.getStringArrayListExtra( RecognizerIntent.EXTRA_RESULTS); tv_Speech_to_text.setText( Objects.requireNonNull(result).get( 0 )); } } } } |
Output: