Android Text-to-Speech
[Android Text-to-Speech]( (TTS) is a feature that enables an Android device to convert text into speech. It provides a convenient way for developers to add voice capabilities to their applications, making them more accessible and user-friendly. In this article, we will explore the basics of using TTS in Android, along with some code examples.
Setting Up Text-to-Speech
To use TTS in an Android application, we need to perform the following steps:
-
Add the necessary permissions to the
AndroidManifest.xml
file:<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
-
Check if TTS is available on the device:
TextToSpeech textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() { @Override public void onInit(int status) { if (status == TextToSpeech.SUCCESS) { // TTS is available } else { // TTS is not available } } });
-
Set the language and speech rate:
Locale locale = new Locale("en", "US"); textToSpeech.setLanguage(locale); textToSpeech.setSpeechRate(1.0f);
-
Convert text to speech:
String text = "Hello, world!"; textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
-
Release the TextToSpeech instance when it is no longer needed:
textToSpeech.shutdown();
Customizing Text-to-Speech
TTS also allows us to customize the speech output by adjusting pitch, volume, and language. Here is an example:
textToSpeech.setPitch(0.8f);
textToSpeech.setSpeechRate(1.2f);
textToSpeech.setLanguage(Locale.UK);
Handling TTS Events
TTS provides several events to monitor the status of speech synthesis. Some common events are:
onInit
: Triggered when TTS initialization is complete.onStart
: Triggered when speech synthesis starts.onUtteranceCompleted
: Triggered when speech synthesis is completed.
Here is an example of handling the onUtteranceCompleted
event:
HashMap<String, String> params = new HashMap<>();
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "utteranceId");
textToSpeech.setOnUtteranceCompletedListener(new TextToSpeech.OnUtteranceCompletedListener() {
@Override
public void onUtteranceCompleted(String utteranceId) {
// Speech synthesis completed
}
});
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, params);
Conclusion
Android Text-to-Speech provides a powerful tool for adding voice capabilities to applications. In this article, we explored the basic setup and usage of TTS, along with some code examples. With TTS, developers can create more accessible and user-friendly applications by enabling their apps to speak out text content to users.
For more information and advanced features of TTS, refer to the [Android TextToSpeech documentation](
表格
Permission | Description |
---|---|
android.permission.INTERNET |
Allows the application to access the internet. |
android.permission.ACCESS_NETWORK_STATE |
Allows the application to access network state information. |
android.permission.MODIFY_AUDIO_SETTINGS |
Allows the application to modify global audio settings. |
android.permission.READ_PHONE_STATE |
Allows read-only access to phone state. |