Android – Text to speech simple example
| In this article I will show the most simple way to add text to speech abilities to your Android application. |
public class Text2SpeechTest extends Activity implements OnInitListener {
TextToSpeech talker;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
talker = new TextToSpeech(this, this);
}
public void say(String text2say){
talker.speak(text2say, TextToSpeech.QUEUE_FLUSH, null);
}
@Override
public void onInit(int status) {
say("Hello World");
}
@Override
public void onDestroy() {
if (talker != null) {
talker.stop();
talker.shutdown();
}
super.onDestroy();
}
}
- Actually that’s the whole code.
You just need to create a TextToSpeech object. - Notice the constructor takes 2 parameters, the first is the Context Activity extends from. The second is an OnInitListener which you will need to implement.
- Ofcourese there are a lot more options but this is the gist of it. You can read more in the Android developer website.
Android, text2speech
Here is a nice example given by android itself: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/TextToSpeechActivity.html