In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use TextToSpeech in Android. It is very detailed and has a certain reference value. Friends who are interested must finish it!
The first draft of APP is shown in the picture:
Realize
1. Initialize the voice. This is an asynchronous operation. Call oninitListener (the second parameter) when initialization is complete.
TextToSpeech mTts = new TextToSpeech (this, this)
two。 Implement TextToSpeech.OnInitListener
Note: the language may not be available.
/ / implement TextToSpeech.OnInitListener. Public void onInit (int status) {if (status = = TextToSpeech.SUCCESS) {/ / sets the preferred language to Chinese. Note that the language may not be available, and the result will indicate this int result = mTts.setLanguage (Locale.CHINA) If (result = = TextToSpeech.LANG_MISSING_DATA | | result = = TextToSpeech.LANG_NOT_SUPPORTED) {/ / language data is missing or the language is not supported. Log.e (TAG, "language data is missing or not supported");} else {/ / check for other possible result codes in the document. / / for example, the language may be available for locales, but not for the specified country and variant / / the TTS engine has been successfully initialized. / / allows the user to press the button to let the application speak again. MAgainButton.setEnabled (true);}} else {/ / initialization failed Log.e (TAG, "initialization failure");}}
3. Write a reading method that triggers when needed (e. G. click event)
The speak method of TextToSpeech has two overloads.
The method of performing reading aloud
Speak (CharSequence text,int queueMode,Bundle params,String utteranceId)
The second parameter, queueMode, is used to specify the pronunciation queue mode, and there are two modes to choose.
(1) TextToSpeech.QUEUE_FLUSH: when there is a new task, the current voice task will be cleared and the new voice task will be executed.
(2) TextToSpeech.QUEUE_ADD: in this mode, the new voice task will be placed after the voice task, and the new voice task will not be executed until the previous voice task is completed.
Record the sound of reading aloud as an audio file
The speak method of synthesizeToFile (CharSequence text,Bundle params,File file,String utteranceId); private void sayHello () {String hello = "Hellow"; / / TextToSpeech has two overloads. / / the method of performing reading aloud / / speak (CharSequence text,int queueMode,Bundle params,String utteranceId); / / recording the sound read aloud as an audio file / / synthesizeToFile (CharSequence text,Bundle params,File file,String utteranceId) / / the second parameter queueMode is used to specify the pronunciation queue mode. Select / / (1) TextToSpeech.QUEUE_FLUSH: this mode clears the current voice task and executes the new voice task / / (2) TextToSpeech.QUEUE_ADD: this mode puts the new voice task after the voice task. / / the new voice task mTts.speak (hello, TextToSpeech.QUEUE_FLUSH, null) will not be executed until the previous voice task has been executed. }
4. Remember to take advantage of Activity's life cycle to turn it off
@ Override public void onDestroy () {/ / end if (mTts! = null) {mTts.stop (); mTts.shutdown ();} super.onDestroy ();} source code in the life cycle
SpeechActivity.java
Public class SpeechActivity extends Activity implements TextToSpeech.OnInitListener {private static final String TAG = "SpeechDemo"; private TextToSpeech mTts; private Button mButton; @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.text_to_speech); / / initialize the voice. This is an asynchronous operation. Call oninitListener (the second parameter) when initialization is complete. MTts = new TextToSpeech (this, this); mButton = (Button) findViewById (R.id.again_button); / / trigger mButton.setOnClickListener (new View.OnClickListener () {public void onClick (View v) {sayHello ();}}) } @ Override public void onDestroy () {/ / end if (mTts! = null) {mTts.stop (); mTts.shutdown ();} super.onDestroy ();} / / implement TextToSpeech.OnInitListener. Public void onInit (int status) {if (status = = TextToSpeech.SUCCESS) {/ / sets the preferred language to Chinese. Note that the language may not be available, and the result will indicate this int result = mTts.setLanguage (Locale.CHINA) If (result = = TextToSpeech.LANG_MISSING_DATA | | result = = TextToSpeech.LANG_NOT_SUPPORTED) {/ / language data is missing or the language is not supported. Log.e (TAG, "language data is missing or not supported");} else {/ / check for other possible result codes in the document. / / for example, the language may be available for locales, but not for the specified country and variant / / the TTS engine has been successfully initialized. / / allows the user to press the button to let the application speak again. MAgainButton.setEnabled (true);}} else {/ / initialization failed Log.e (TAG, "initialization failure");}} private void sayHello () {String hello = "Don't eat fish"; / / TextToSpeech's speak method has two overloads. / / the method of performing reading aloud / / speak (CharSequence text,int queueMode,Bundle params,String utteranceId); / / recording the sound read aloud as an audio file / / synthesizeToFile (CharSequence text,Bundle params,File file,String utteranceId) / / the second parameter queueMode is used to specify the pronunciation queue mode. Select / / (1) TextToSpeech.QUEUE_FLUSH: this mode clears the current voice task and executes the new voice task / / (2) TextToSpeech.QUEUE_ADD: this mode puts the new voice task after the voice task. / / the new voice task mTts.speak (hello, TextToSpeech.QUEUE_FLUSH, null) will not be executed until the previous voice task has been executed. }}
Text_to_speech.xml
The above is all the content of the article "how to use TextToSpeech in Android". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.