Android TTS technology

Source: Internet
Author: User

AndroidTTS technology, that is, Text-to-speech technology. TTS technology converts text files in real time. The conversion time can be calculated in seconds. With its unique Intelligent Voice controller, the voice and audio laws of text output are smooth, making the listener feel natural when listening to information, and there is no indifference or cool sense of machine speech output. The TTS Speech Synthesis Technology will cover the first and second-level Chinese characters of the national standard. It has an English interface to automatically identify Chinese and English and supports mixed Chinese and English reading. All voices adopt real-person Mandarin as the standard pronunciation, achieving rapid speech synthesis of-Chinese characters/second, reading speed up to 3-4 Chinese characters/second, enables users to hear clear and pleasant sound quality and consistent and smooth tone.

Let's take a look at how this is implemented:

Step 1: Check whether TTS data is available:

Java code:

 
 
  1. // Check whether TTS data is installed and available
  2. Intent checkIntent = new Intent ();
  3. CheckIntent. setAction (TextToSpeech. Engine. ACTION_CHECK_TTS_DATA );
  4. StartActivityForResult (checkIntent, REQ_TTS_STATUS_CHECK );
  5. Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
  6. If (requestCode = REQ_TTS_STATUS_CHECK)
  7. {
  8. Switch (resultCode ){
  9. Case TextToSpeech. Engine. CHECK_VOICE_DATA_PASS:
  10. // The returned result indicates that TTS Engine can use
  11. {
  12. MTts = new TextToSpeech (this, this );
  13. Log. v (TAG, "TTS Engine is installed! ");
  14. }
  15. Break;
  16. Case TextToSpeech. Engine. CHECK_VOICE_DATA_BAD_DATA:
  17. // The required voice data is damaged.
  18. Case TextToSpeech. Engine. CHECK_VOICE_DATA_MISSING_DATA:
  19. // The voice data in the required language is missing
  20. Case TextToSpeech. Engine. CHECK_VOICE_DATA_MISSING_VOLUME:
  21. // The pronunciation data of the desired language is missing
  22. {
  23. // All three cases indicate that the data is incorrect. download the required data again.
  24. Log. v (TAG, "Need language stuff:" + resultCode );
  25. Intent dataIntent = new Intent ();
  26. DataIntent. setAction (TextToSpeech. Engine. ACTION_INSTALL_TTS_DATA );
  27. StartActivity (dataIntent );
  28. }
  29. Break;
  30. Case TextToSpeech. Engine. CHECK_VOICE_DATA_FAIL:
  31. // Check failed
  32. Default:
  33. Log. v (TAG, "Got a failure. TTS apparently not available ");
  34. Break;
  35. }
  36. }
  37. Else
  38. {
  39. // Other Intent returned results
  40. }
  41. }

Next, initialize TTS.

Java code:

 
 
  1. // Implement the TTS initialization Interface
  2. @ Override
  3. Public void onInit (int status ){
  4. // TODO Auto-generated method stub
  5. // TTS Engine Initialization is complete
  6. If (status = TextToSpeech. SUCCESS)
  7. {
  8. Int result = mTts. setLanguage (Locale. US );
  9. // Set the pronunciation Language
  10. If (result = TextToSpeech. LANG_MISSING_DATA | result = TextToSpeech. LANG_NOT_SUPPORTED)
  11. // Determine whether the language is available
  12. {
  13. Log. v (TAG, "Language is not available ");
  14. SpeakBtn. setEnabled (false );
  15. }
  16. Else
  17. {
  18. MTts. speak ("This is an example of speech synthesis.", TextToSpeech. QUEUE_ADD, null );
  19. SpeakBtn. setEnabled (true );
  20. }
  21. }
  22. }

Next, set the pronunciation Language

Java code:

 
 
  1. Public void onItemSelected (AdapterView <?> Parent, View view, int position, long id ){
  2. // TODO Auto-generated method stub
  3. Int pos = langSelect. getSelectedItemPosition ();
  4. Int result =-1;
  5. Switch (pos ){
  6. Case 0:
  7. {
  8. InputText. setText ("I love you ");
  9. Result = mTts. setLanguage (Locale. US );
  10. }
  11. Break;
  12. Case 1:
  13. {
  14. InputText. setText ("Je t 'aime ");
  15. Result = mTts. setLanguage (Locale. FRENCH );
  16. }
  17. Break;
  18. Case 2:
  19. {
  20. InputText. setText ("Ich liebe dich ");
  21. Result = mTts. setLanguage (Locale. GERMAN );
  22. }
  23. Break;
  24. Case 3:
  25. {
  26. InputText. setText ("Ti amo ");
  27. Result = mTts. setLanguage (Locale. ITALIAN );
  28. }
  29. Break;
  30. Case 4:
  31. {
  32. InputText. setText ("Te quiero ");
  33. Result = mTts. setLanguage (new Locale ("spa", "ESP "));
  34. }
  35. Break;
  36. Default:
  37. Break;
  38. }
  39. // Set the pronunciation Language
  40. If (result = TextToSpeech. LANG_MISSING_DATA | result = TextToSpeech. LANG_NOT_SUPPORTED)
  41. // Determine whether the language is available
  42. {
  43. Log. v (TAG, "Language is not available ");
  44. SpeakBtn. setEnabled (false );
  45. }
  46. Else
  47. {
  48. SpeakBtn. setEnabled (true );
  49. }
  50. }

Finally, click the Button to pronounce:

Java code:

 
 
  1. Public void onClick (View v ){
  2. // TODO Auto-generated method stub
  3. MTts. speak (inputText. getText (). toString (), TextToSpeech. QUEUE_ADD, null );
  4. // Read the content in the input box
  5. }

Android environment variable settings

Android easily implements Speech Recognition

Insert a new Activity in Android Development

Android development tour: Android Architecture

Use Internet Data in Android applications

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.