This
is the most easiest way to create speech to text converter . We have another method also (method 2) that is speech to text converter without dialog box which we will discuss later.
This code is tested carefully and have 100% accuracy rate. So, you will get the desire result after using it.
activity_main.xml File:-
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageButton
android:id="@+id/btnSpeak"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@null"
android:padding="16dp"
android:layout_gravity="center"
android:src="@drawable/ic_keyboard_voice_black_24dp"
/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:textSize="12sp"
style="@style/TextAppearance.AppCompat.Body1"
android:textColor="@android:color/holo_orange_light"
android:text="@string/text"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/voiceInput"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="22dp"
android:textAlignment="center"
android:textColor="@android:color/background_dark"/>
</RelativeLayout>
</LinearLayout>
MainActivity.java File:-
package
com.example.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.ImageButton;
import
android.widget.TextView;
import
android.widget.Toast;
import
java.util.ArrayList;
import
java.util.Locale;
public
class MainActivity
extends
AppCompatActivity
{
private
static final int REQ_CODE_SPEECH_INPUT
=
100;
private TextView
txtSpeechInput;
private ImageButton
mSpeakBtn;
@Override
protected
void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtSpeechInput=findViewById(R.id.voiceInput);
mSpeakBtn
=
(ImageButton) findViewById(R.id.btnSpeak);
mSpeakBtn.setOnClickListener(new
View.OnClickListener()
{
@Override
public
void onClick(View
v) {
startListening();
}
});
}
private
void startListening()
{
//Intent
to listen to user vocal input and return result in same activity
Intent
intent = new
Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//Use
a language model based on free-form speech recognition.
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,
Locale.getDefault());
//Message
to display in dialog box
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_to_text_info));
try {
startActivityForResult(intent,
REQ_CODE_SPEECH_INPUT);
}
catch
(ActivityNotFoundException
a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
@Override
protected
void onActivityResult
(
int
requestCode,
int resultCode,
Intent
data){
super.onActivityResult(requestCode,
resultCode,
data);
switch (requestCode)
{
case
REQ_CODE_SPEECH_INPUT:
{
if
(resultCode
== RESULT_OK
&&
null
!=
data) {
ArrayList<String> result =
data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtSpeechInput.setText(result.get(0));
}
break;
}
}
}
}
String.Xml File:-
resources>
<string name="app_name">
STT
converter</string>
<string name="speech_to_text_info">Hello!
How can I help you.</string>
<string name="speech_not_supported">speech
not supported</string>
<string name="text">Press
the button to convert speech into text. </string>
</resources>
Output:-
Thanks for reading.If you have any query feel free to comment. Please subscribe this blog for regular updates through email, if you found any difficulty then, comment below.
Great work ๐๐
ReplyDelete