Text-to-speech getting started and Advanced Learning notes (android)-Alibaba Cloud Developer Community

  1. // 1. Write a private final method to check whether tts is installed. Static  int CHECK_CODE = 1;
  2. public void checkTts() {
  3. Intent checkIntent = new Intent(); checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
  4. startActivityForResult(checkIntent, CHECK_CODE);}
  5. // Create a TTS
  6. private TextToSpeech mTts;
  7. // Find out what is going on with the onActivityResult first. I will not explain it in detail here.
  8. int requestCode int resultCode, Intent data) {
  9. // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data);
  10. if(requestCode == CHECK_CODE) {
  11. if(resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
  12. // Create a TTS instance.
  13. mTts = new TextToSpeech(this, this);
  14. } else {
  15. // Otherwise, install one
  16. Intent installIntent = new Intent(); installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); startActivity
  17. (installIntent); } } }
  18. // 2, let TTS read aloud
  19. // onCreate method
  20. // Check whether TTS checkTts properly ();
  21. // Create component private speakValue = (EditText)
  22. findViewById(R.id.speakValue);
  23. private speakButton = (Button)
  24. findViewById(R.id.sayHello);
  25. speakButton.setOnClickListener(new OnClickListener() {
  26. public void onClick ( View v) {
  27. // TODO Auto-generated method stub
  28. String text = speakValue.getText().toString();
  29. Log.d ( "result" , "text-->" +text); sayTts(text); } });
  30. // TTs reading private void sayTts(String text){
  31. // The simplest example is mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null );
  32. }
  33. // Additionally, use TTS to read aloud. Be sure to implement OnInitListener interfaces.
  34. // Initialize a TTs object after it is created.
  35. int status) {
  36. if(status == TextToSpeech.SUCCESS) {
  37. // Sets the language area. Int result = mTts.setLanguage(Locale.US);
  38. // If the language is not supported
  39. if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
  40. Log.e ( "error" , "Not supported" ) ****}

just a little bit of code can run normally...

2. Advanced usage: change the type of reading and playback, the use of callback functions, and the pronunciation of custom words.

1. Change the type of the stream to be played (for the moment, I hope someone can explain it)

  1. // Change the stream type used for playback.
  2. // Make the following modifications in sayTts
  3. // TTs reading private void sayTts(String text) { HashMap <String, String>myAlarm = new HashMap();
  4. // Set the playback type to myHashAlarm.put(TextToSpeech.Engine. Key_param_stream, String.valueOf(AudioManager.STREAM_ALARM)) through the alarm clock stream;
  5. // Enter mTts.speak(text, TextToSpeech.QUEUE_FLUSH, myAlarm) in the alarm clock stream of the audio playback;
  6. // The original English text is like this. What I know is what I commented above
  7. // I don't know much about it. I hope someone can help me
  8. // On Android, each audio stream that is played is associated with one
  9. //stream type as defined in android.media.AudioManager. For a talking
  10. //alarm clock, we would like our text to be played on the
  11. //AudioManager.STREAM_ALARM stream type so that it respects the alarm
  12. //settings user has chosen on the device.
  13. }

2. Use callback functions

  1. // Implement **in sayTts()..
  2. // Implement OnUtteranceCompletedListener.
  3. // For TTs reading
  4. private Toast toast; prvate Context context = getApplicationContext();
  5. private void sayTts(String text) { HashMap <String, String>myAlarm = new HashMap(); mTts.setOnUtteranceCompletedListener(this);
  6. myAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID "me too" );
  7. toast = Toast.makeText(context "me too" , Toast.LENGTH_LONG);
  8. // The simplest example is mTts.speak(text, TextToSpeech.QUEUE_FLUSH, myAlarm); }
  9. private void speakPlayBack(String str) { Log.d ( "result" , "playBack--&gt;" + str); toast.show();}

3, record TTS reading and save it to SD card

  1. // Save the reading result of TTS.
  2. private void sayTofile(String text) {
  3. HashMap <String, String>ttsRender = new HashMap <String, String>();
  4. String destFileName = "/sdcard/tts /" +text+ ".wav" ; ttsRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, text); mTts.synthesizeToFile(text, ttsRender, destFileName);
  5. }
  6. // In

you can see that the Text you just read is saved

4. Customize Text pronunciation

  1. // Customize text playback.
  2. private void sayTrue(String text) {
  3. String destFileName = "/sdcard/tts /" +text+ ".wav" ; mTts.addSpeech(text, destFileName);
  4. mTts.speak(text, TextToSpeech.QUEUE_FLUSH null ); }
  5. // Run the sayTrue() in the onclick method;

pro-Test, MP3 can also be played...

5. Destroy

  1. if(mTts! = null ) {
  2. mTts.stop();
  3. mTts.shutdown(); } }
Please read this disclaimer carefully before you start to use the service. By using the service, you acknowledge that you have agreed to and accepted the content of this disclaimer in full. You may choose not to use the service if you do not agree to this disclaimer. This document is automatically generated based on public content on the Internet captured by Machine Learning Platform for AI. The copyright of the information in this document, such as web pages, images, and data, belongs to their respective author and publisher. Such automatically generated content does not reflect the views or opinions of Alibaba Cloud. It is your responsibility to determine the legality, accuracy, authenticity, practicality, and completeness of the content. We recommend that you consult a professional if you have any doubt in this regard. Alibaba Cloud accepts no responsibility for any consequences on account of your use of the content without verification. If you have feedback or you find that this document uses some content in which you have rights and interests, please contact us through this link: https://www.alibabacloud.com/campaign/contact-us-feedback. We will handle the matter according to relevant regulations.
Selected, One-Stop Store for Enterprise Applications
Support various scenarios to meet companies' needs at different stages of development

Start Building Today with a Free Trial to 50+ Products

Learn and experience the power of Alibaba Cloud.

Sign Up Now