Android Text to Speech

Source: Internet
Author: User

Although visual feedback is often the quickest way to provide information to users, this requires the user to focus on the device. Some other means of communication are required when the user cannot view the device. Android provides a powerful text-to-speech Text-to-speech,tts API. Enables developers to add voice notifications and other voice feedback features to the app without requiring the user 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 concurrentlinkedqueue<string> mbufferedmessages;//Message Queuing
Private Context Mcontext;
Private Boolean misready;//identifier


Public Texttospeechdemo (Context context) {
this.mcontext=context;//Get Context
This.mbufferedmessages=new concurrentlinkedqueue<string> ();//instantiation of the queue
This.mtexttospeech=new Texttospeech (this.mcontext,this);//Instantiate TTS
}

Initializing 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 recognition speech to Chinese
Synchronized (this) {
this.misready=true;//Set Identifier to True
for (String bufferedMessage:this.mBufferedMessages) {
SpeakText (bufferedmessage);//Read Voice
}
This.mBufferedMessages.clear ();//empty queue after reading
}
}
}
Freeing resources
public void release () {
Synchronized (this) {
This.mTextToSpeech.shutdown ();
This.misready=false;
}
}

Update Message Queuing, or read speech
public void Notifynewmessage (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<string,string> params=new hashmap<string,string > ();
Params.put (TextToSpeech.Engine.KEY_PARAM_STREAM, "stream_notification");//Set playback type (audio stream type)
This.mTextToSpeech.speak (message, texttospeech.queue_add, params);//Add this pronunciation task after the current task
This.mTextToSpeech.playSilence (100,texttospeech.queue_add,params);//How long is the interval
}
}

Of course the general cell phone is not supported in Chinese can be Baidu download the TTS engine after the test. See final drawings.


Because the TTS engine is initialized asynchronously, the message needs to be put into the queue before the actual text-to-speech is performed.


Multiple parameters can be sent to the TTS engine. The previous shows how to determine the audio stream used by the spoken message. In this case, the notification sound also uses the same audio stream.


Finally, if more than one message is processed, it is best to pause one after each message ends to play the next message. Doing so will clearly tell the user the end and the beginning of the message.

Android Text to Speech

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.