In this tutorial, we will know that how to convert a text into speech. Android has a predefined convert engine called TextToSpeech which converts text words in speech.
You can use this feature in many application such as a dictionary application where a user can use this feature to understand the real pronunciation of a particular word. We create TextToSpeech instance and this instance can be used only to synthesize text, once it has completed its initialization. Implement the TextToSpeech.OnInitListener
to be notified of the completion of the initialization.
Step 1- Create a XML file activity_main.xml
In this XML file define one EditText to type any words and one Button on which click we will create an instance of TextToSpeech class and get type words to convert in speech.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/speech_input"
android:paddingLeft="10dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Convert text to speech"
android:id="@+id/text_to_speech"
android:layout_below="@+id/speech_input"
android:layout_centerHorizontal="true"
android:textColor="#000000"
android:layout_marginTop="36dp" />
</RelativeLayout>
Step 2- Create a Activity MainActivity.java
We will complete up this tutorial by writing the code that let the user enter words in EditText and after click on Button convert that word in speech. We create an instance of the TextToSpeech class which implements onInit callback method. This implemented method signify when the TextToSpeech engine is ready to be used.
public class MainActivity extends AppCompatActivity {
private EditText userInputToSpeech;
private TextToSpeech convertToSpeech;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_textspeech);
userInputToSpeech = (EditText) findViewById(R.id.speech_input);
Button textToSpeech = (Button) findViewById(R.id.text_to_speech);
textToSpeech.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String convertTextToSpeech = userInputToSpeech.getText().toString();
if (convertTextToSpeech.equals("")) {
Toast.makeText(MainActivity.this, "The input field must not be empty", Toast.LENGTH_LONG).show();
return;
}
convertToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
convertToSpeech.setLanguage(Locale.US);
convertToSpeech.speak(convertTextToSpeech, TextToSpeech.QUEUE_FLUSH, null, null);
}
}
});
}
});
}
}
0 Comment(s)