Android

Text To Speech Android App.

Before starting example using Android SDK, you have to make sure that you have setup your Android development environment properly in your system and you you have knowledge with Eclipse IDE.

The first step is to create a simple Android Application using Eclipse IDE. Follow the option File -> New -> Project and finally select Android New Application wizard from the wizard list.

a7

Create blank activity with MainActivity

a6

This Android app. allows you convert your text into voice. Android provides TextToSpeech class for this purpose. In order to use this class, you need to instantiate an object of this class and also specify the initListener. Its syntax is given below:

private EditText write;
ttobj=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
}
}
);

In this listener , you have to specify the properties for TextToSpeech object , such as its language ,pitch e.t.c. Language can be set by calling setLanguage() method. Its syntax is given below:

ttobj.setLanguage(Locale.UK);
following are the steps that demonstrates the working procedure of this app:

1.You will use Eclipse IDE to create an Android application and name it as TextToSpeech under a package com.example.texttospeech. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs.

2.Modify src/MainActivity.java file to add TextToSpeech code

3.Modify layout XML file res/layout/activity_main.xml

4.Modify res/values/string.xml file.

5.Run the application and choose a running android device and install the application on it and verify the results.

here is the content of src/com.example.texttospeech/MainActivity.java…..

package com.example.texttospeech;

import java.util.Locale;
import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

TextToSpeech ttobj;
private EditText write;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
write = (EditText)findViewById(R.id.editText1);
ttobj=new TextToSpeech(getApplicationContext(),
new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR){
ttobj.setLanguage(Locale.UK);
}
}
});
}
@Override
public void onPause(){
if(ttobj !=null){
ttobj.stop();
ttobj.shutdown();
}
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void speakText(View view){
String toSpeak = write.getText().toString();
Toast.makeText(getApplicationContext(), toSpeak,
Toast.LENGTH_SHORT).show();
ttobj.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);

}
}

Here is the content of activity_main.xml….

Leave a Reply