Skip to content

Instantly share code, notes, and snippets.

@lethargicpanda
Created October 19, 2010 14:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lethargicpanda/634245 to your computer and use it in GitHub Desktop.
Save lethargicpanda/634245 to your computer and use it in GitHub Desktop.
A basic activity to start to play with TextToSpeech on Android
public class TTSTestActivity extends Activity implements OnInitListener {
private static final int MY_DATA_CHECK_CODE = 0;
private TextToSpeech mTts;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.tts_layout);
// check whether TTS resources are available on the device
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
super.onCreate(savedInstanceState);
}
@Override
public void onInit(int i) {
// called as soon as the TextToSpeech instance is intialized
// set the locale
mTts.setLanguage(Locale.US);
// add your text speech
String myText1 = "Hello Master !";
String myText2 = "I am C3PO, Human-Cyborg relations. And this is my counterpart, R2D2.";
mTts.speak(myText1, TextToSpeech.QUEUE_FLUSH, null);
mTts.speak(myText2, TextToSpeech.QUEUE_ADD, null);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode==MY_DATA_CHECK_CODE) {
if(resultCode==TextToSpeech.Engine.CHECK_VOICE_DATA_PASS){
// if TTS resources are available you instanciate your TextToSpeech object
mTts = new TextToSpeech(this, this);
} else {
// else you ask the system to install it
Intent installTTSIntent = new Intent();
installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
}
}
}
@lethargicpanda
Copy link
Author

You can check-out Very short intro to Text To Speech on Android for more infomation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment