Android advanced: Google's built-in voice playback function

Source: Internet
Author: User

In Android, you only need to use texttospeech to use the voice playback function. This class implements many speech-related functions. You must set the language for this class. Currently, five languages are supported, the cup holder does not support Chinese characters.

 

The implementation is simple, but you must first install the Language Pack. In settings, set the language input and output, and set the Text to Speech.

 

For example

 



 

I have installed the voice data in the figure on the left, so it is gray. If it is not installed, you can click here to make it gray.

 

After downloading and installing the Installation File 4.28 M, you can select the language. The five languages shown in the right figure do not have any Chinese characters.

 

The implementation below is very simple

 



 

The first is the layout file:

 

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <edittext <br/> Android: id = "@ + ID/edittext01" <br/> Android: layout_width = "fill_parent" <br/> Android: text = "I hope so, because it's time to wake up. "<br/> Android: layout_height =" wrap_content "<br/> <button <br/> Android: id = "@ + ID/button01" <br/> Android: text = "start playback" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> </linearlayout> <br/>

 

Is to play the content in edittext.

 

Acitivity

 

Import Java. util. locale; <br/> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. speech. TTS. texttospeech; <br/> Import android. speech. TTS. texttospeech. oninitlistener; <br/> Import android. util. log; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> Import android. widget. edittext; <br/> public class voisepla Ydemo extends activity {<br/> private texttospeech mspeech; <br/> private button BTN; <br/> private edittext medittext; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> BTN = (button) findviewbyid (R. id. button01); <br/> medittext = (edittext) findviewbyid (R. id. edittext01); <br/> BTN. setenabled (false ); <Br/> // create a texttospeech object <br/> mspeech = new texttospeech (this, new oninitlistener () {<br/> @ override <br/> Public void oninit (INT status) {<br/> If (status = texttospeech. success) {<br/> int result = mspeech. setlanguage (locale. US); <br/> If (result = texttospeech. lang_missing_data <br/> | result = texttospeech. lang_not_supported) {<br/> log. E ("BB", "not use"); <br/>} else {<br/> BTN. setenabled (tr UE); <br/>}< br/>}); <br/> BTN. setonclicklistener (New onclicklistener () {<br/> @ override <br/> Public void onclick (view v) {<br/> mspeech. speak (medittext. gettext (). tostring (), <br/> texttospeech. queue_flush, null); <br/>}< br/>}); <br/>}< br/> @ override <br/> protected void ondestroy () {<br/> If (mspeech! = NULL) {<br/> mspeech. stop (); <br/> mspeech. shutdown (); <br/>}< br/> super. ondestroy (); <br/>}< br/>

 

It is really easy to create a texttospeech class instance and determine whether the voice loading is successful in the oninit initialization method.

 

I just don't know when I can support Chinese.

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.