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:
- // Check whether TTS data is installed and available
- Intent checkIntent = new Intent ();
- CheckIntent. setAction (TextToSpeech. Engine. ACTION_CHECK_TTS_DATA );
- StartActivityForResult (checkIntent, REQ_TTS_STATUS_CHECK );
- Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
- If (requestCode = REQ_TTS_STATUS_CHECK)
- {
- Switch (resultCode ){
- Case TextToSpeech. Engine. CHECK_VOICE_DATA_PASS:
- // The returned result indicates that TTS Engine can use
- {
- MTts = new TextToSpeech (this, this );
- Log. v (TAG, "TTS Engine is installed! ");
- }
- Break;
- Case TextToSpeech. Engine. CHECK_VOICE_DATA_BAD_DATA:
- // The required voice data is damaged.
- Case TextToSpeech. Engine. CHECK_VOICE_DATA_MISSING_DATA:
- // The voice data in the required language is missing
- Case TextToSpeech. Engine. CHECK_VOICE_DATA_MISSING_VOLUME:
- // The pronunciation data of the desired language is missing
- {
- // All three cases indicate that the data is incorrect. download the required data again.
- Log. v (TAG, "Need language stuff:" + resultCode );
- Intent dataIntent = new Intent ();
- DataIntent. setAction (TextToSpeech. Engine. ACTION_INSTALL_TTS_DATA );
- StartActivity (dataIntent );
- }
- Break;
- Case TextToSpeech. Engine. CHECK_VOICE_DATA_FAIL:
- // Check failed
- Default:
- Log. v (TAG, "Got a failure. TTS apparently not available ");
- Break;
- }
- }
- Else
- {
- // Other Intent returned results
- }
- }
Next, initialize TTS.
Java code:
- // Implement the TTS initialization Interface
- @ Override
- Public void onInit (int status ){
- // TODO Auto-generated method stub
- // TTS Engine Initialization is complete
- If (status = TextToSpeech. SUCCESS)
- {
- Int result = mTts. setLanguage (Locale. US );
- // Set the pronunciation Language
- If (result = TextToSpeech. LANG_MISSING_DATA | result = TextToSpeech. LANG_NOT_SUPPORTED)
- // Determine whether the language is available
- {
- Log. v (TAG, "Language is not available ");
- SpeakBtn. setEnabled (false );
- }
- Else
- {
- MTts. speak ("This is an example of speech synthesis.", TextToSpeech. QUEUE_ADD, null );
- SpeakBtn. setEnabled (true );
- }
- }
- }
Next, set the pronunciation Language
Java code:
- Public void onItemSelected (AdapterView <?> Parent, View view, int position, long id ){
- // TODO Auto-generated method stub
- Int pos = langSelect. getSelectedItemPosition ();
- Int result =-1;
- Switch (pos ){
- Case 0:
- {
- InputText. setText ("I love you ");
- Result = mTts. setLanguage (Locale. US );
- }
- Break;
- Case 1:
- {
- InputText. setText ("Je t 'aime ");
- Result = mTts. setLanguage (Locale. FRENCH );
- }
- Break;
- Case 2:
- {
- InputText. setText ("Ich liebe dich ");
- Result = mTts. setLanguage (Locale. GERMAN );
- }
- Break;
- Case 3:
- {
- InputText. setText ("Ti amo ");
- Result = mTts. setLanguage (Locale. ITALIAN );
- }
- Break;
- Case 4:
- {
- InputText. setText ("Te quiero ");
- Result = mTts. setLanguage (new Locale ("spa", "ESP "));
- }
- Break;
- Default:
- Break;
- }
- // Set the pronunciation Language
- If (result = TextToSpeech. LANG_MISSING_DATA | result = TextToSpeech. LANG_NOT_SUPPORTED)
- // Determine whether the language is available
- {
- Log. v (TAG, "Language is not available ");
- SpeakBtn. setEnabled (false );
- }
- Else
- {
- SpeakBtn. setEnabled (true );
- }
- }
Finally, click the Button to pronounce:
Java code:
- Public void onClick (View v ){
- // TODO Auto-generated method stub
- MTts. speak (inputText. getText (). toString (), TextToSpeech. QUEUE_ADD, null );
- // Read the content in the input box
- }
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