Created
October 19, 2010 14:10
-
-
Save lethargicpanda/634245 to your computer and use it in GitHub Desktop.
A basic activity to start to play with TextToSpeech on Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can check-out Very short intro to Text To Speech on Android for more infomation.