Speech To Text
Speech To Text
A PROJECT REPORT
Submitted by
BOOMINATHAN N (21CS242)
SRI RAM S (21CS244)
GUNASEELAN S (21CS201)
ABDUL KUTHUS J (21CS109)
GOPI P (21CS168)
degree of
BACHELOR OF ENGINEERING
in
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"
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/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(){
@Override
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
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());
try {
startActivityForResult(intent, REQUEST_CODE_SPEECH_INPUT);
catch (Exception e) {
Toast
Toast.LENGTH_SHORT)
.show();
});
@Override
protected void onActivityResult(int requestCode, int resultCode,
if (requestCode == REQUEST_CODE_SPEECH_INPUT) {
RecognizerIntent.EXTRA_RESULTS);
tv_Speech_to_text.setText(
Objects.requireNonNull(result).get(0));