Android Text to Speech

Source: Internet
Author: User

Android Text to Speech

Although visual feedback is usually the fastest way to provide users with information, this requires users to focus on the device. When a user cannot view a device, other communication methods are required. Android provides powerful Text-to-Speech and TTS APIs. This allows developers to add voice notifications and other voice feedback functions to their applications without requiring users to look at the screen.

 

The following code shows how to use the tts api:

Public class TextToSpeechDemo implements TextToSpeech. OnInitListener {
Private final TextToSpeech mTextToSpeech; // TTS object
Private final concurrent1_queue MBufferedMessages; // Message Queue
Private Context mContext;
Private boolean mIsReady; // identifier


Public TextToSpeechDemo (Context context ){
This. mContext = context; // obtain the context
This. mBufferedMessages = new concurrent1_queue (); // Instantiate a queue
This. mTextToSpeech = new TextToSpeech (this. mContext, this); // instantiate TTS
}

// Initialize the TTS Engine
@ Override
Public void onInit (int status ){
Log. I ("TextToSpeechDemo", String. valueOf (status ));
If (status = TextToSpeech. SUCCESS ){
Int result = this. mTextToSpeech. setLanguage (Locale. CHINA); // set the recognition speech to Chinese.
Synchronized (this ){
This. mIsReady = true; // set the identifier to true.
For (String bufferedMessage: this. mBufferedMessages ){
SpeakText (bufferedMessage); // read speech
}
This. mBufferedMessages. clear (); // clear the queue after reading
}
}
}
// Release resources
Public void release (){
Synchronized (this ){
This. mTextToSpeech. shutdown ();
This. mIsReady = false;
}
}

// Update the message queue or read the voice
Public void policynewmessage (String lanaugh ){
String message = lanaugh;
Synchronized (this ){
If (this. mIsReady ){
SpeakText (message );
} Else {
This. mBufferedMessages. add (message );
}
}
}

// Read speech processing
Private void speakText (String message ){
Log. I ("liyuanjinglyj", message );
HashMap Params = new HashMap ();
Params. put (TextToSpeech. Engine. KEY_PARAM_STREAM, "STREAM_NOTIFICATION"); // sets the playing type (audio stream type)
This. mTextToSpeech. speak (message, TextToSpeech. QUEUE_ADD, params); // after adding this pronunciation task to the current task
This. mTextToSpeech. playSilence (100, TextToSpeech. QUEUE_ADD, params); // Interval
}
}

Of course, mobile phones generally do not support Chinese characters. Baidu can download the TTS engine of xunfei and test the engine. See the final figure.

 

Because the initialization of the TTS engine is asynchronous, you need to put the message in the queue before executing the actual Text to Speech.

 

Multiple parameters can be sent to the TTS engine. The preceding figure shows how to determine the audio stream used by the spoken message. In this case, the same audio stream is used for the notification sound.

 

Finally, if you process multiple messages in a row, it is best to pause and play the next message after each message ends. This will clearly tell you the end and start of the message.

Related Article

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.