How to Convert Speech to Text in Android?

Hello Guys,

   In this tutorial , we are going to create convert speech to text on android studio. In this app convert the speech into text like you speak "Hello" in the mic and written in textview. This app is example of Artificial intelligence. Here we used to ImageView and textview. Here imageview is used to speek and textview is used to set text speak by user.


SpeechToText App MainScreen looking like below picture: 




Step 1 : Create a new Project.

Step 2 : Edit activity_main.xml file.

  • In this xml file we create one imageview and textview under RelativeLayout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<ImageView
android:id="@+id/btn_speek"
android:layout_width="55dp"
android:layout_height="55dp"
android:src="@drawable/ic_baseline_mic_24"
android:layout_centerInParent="true"/>

<TextView
android:id="@+id/btn_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Hello"
android:textSize="25sp"
android:layout_centerInParent="true"
android:layout_marginBottom="100dp"/>

</RelativeLayout>


Step 3 : Implement MainActivity.java class file.
  • This class is used for convert speech to text.
import androidx.appcompat.app.AppCompatActivity;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {


private TextView txt;
private ImageView imageView;
private final int Record_code = 1;

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

txt = findViewById(R.id.btn_text);
imageView = findViewById(R.id.btn_speek);

imageView.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_LANGUAGE ,
Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT ,
"Need to Speak");

try {
startActivityForResult(intent, Record_code);
}catch (ActivityNotFoundException e){
Toast.makeText(getApplicationContext() ,
"Device not supported" , Toast.LENGTH_LONG).show();
}
}
});

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case Record_code:{
if (resultCode == RESULT_OK && null != data){
ArrayList<String> res =
data.getStringArrayListExtra(RecognizerIntent.
EXTRA_RESULTS);
txt.setText(res.get(0));
}
break;
}
}
}


}

  • ACTION_RECOGNIZE_SPEECH is used to start activity that will bring on the user for speech and send it speech recognizer.
  • EXTRA_LANGUAGE_MODEL is used to inform the speech recognizer which speech model to prefer when performing.
  • LANGUAGE_MODEL_FREE_FROM is used use language model based on free-form speech recognition.
  • EXTRA_LANGUAGE is used to define extra language.
  • EXTRA_PROMPT is used to text bring on the show to the user when asking them to speak.
  • EXTRA_RESULTS is used to display the recognition result when performing.

Output :




If you liked this post do comment ,share and promote the post 🙏 . Please stay with us and support.  🙏   

Post a Comment

0 Comments