0a01-app-text-to-speech

Source: Internet
Author: User

Describe the API to read the text, test the knowledge on the phone to support English, the Chinese library should not be supported

 /* Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License" ); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by appli Cable law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warranties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. */package Com.example.android.apis.app;import Android.app.activity;import Android.os.bundle;import Android.speech.tts.texttospeech;import Android.util.log;import Android.view.view;import Android.widget.Button; Import Com.example.android.apis.r;import java.util.locale;import java.util.random;/** * <p>Demonstrates Text-to-Speech (TTS). Please note the following steps:</p> * * &LT;OL&GT  * <li>construct the Texttospeech object.</li> * <li>handle initialization callback in the OnInit method.  * The activity implements Texttospeech.oninitlistener for this purpose.</li> * <li>call texttospeech.speak to Synthesize speech.</li> * <li>shutdown Texttospeech in ondestroy.</li> * </ol> * <p>Docum Entation: * http://developer.android.com/reference/android/speech/tts/package-summary.html * </p> * <ul> */public class Texttospeechactivity extends Activity implements Texttospeech.oninitlistener {private static final Stri    ng TAG = "Texttospeechdemo";    Private Texttospeech mTts;    Private Button Magainbutton;        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.text_to_speech); Initialize Text-to-Speech.        This was an asynchronous operation. The Oninitlistener (second argument) is called after InitializAtion completes.        MTts = new Texttospeech (this, this//Texttospeech.oninitlistener);        The button is disabled in the layout.        It'll be enabled upon initialization of the TTS engine.        Magainbutton = (Button) Findviewbyid (R.id.again_button); Magainbutton.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {sayhe            Llo ();    }        });        } @Override public void OnDestroy () {//Don ' t forget to shutdown!            if (mTts! = null) {mtts.stop ();        Mtts.shutdown ();    } Super.ondestroy ();    }//Implements Texttospeech.oninitlistener.        @Override public void onInit (int status) {//status can either Texttospeech.success or Texttospeech.error.            if (status = = Texttospeech.success) {//Set preferred language to US 中文版. Note that a language is available, and the result will indicate this.            int result = Mtts.setlanguage (locale.us);            Try This someday for some interesting results.            int result Mtts.setlanguage (locale.france);                if (result = = Texttospeech.lang_missing_data | |                result = = texttospeech.lang_not_supported) {//Lanuage data is missing or the language is not supported.            LOG.E (TAG, "Language is not available.");                } else {//Check the documentation for possible result codes. For example, the language is available for the locale,/or not for the specified country and vari                Ant.                The TTS engine has been successfully initialized.                Allow the user to press the button for the app to speak again.                Magainbutton.setenabled (TRUE);                Greet the user.            SayHello ();  }} else {//initialization failed.          LOG.E (TAG, "Could not initialize Texttospeech.");    }} private static final random random = new random (); private static final string[] Hellos = {"Hello", "Salutations", "Greetings", "Howdy", "What" Cr    Ack-a-lackin? "," that explains the stench! "    };        private void SayHello () {//Select a random hello.        int hellolength = Hellos.length;        String Hello = Hellos[random.nextint (hellolength)];            Mtts.speak (Hello, Texttospeech.queue_flush,//Drop all pending entries in the playback QUEUE.    NULL); }}

Implementation steps:

1. Inheriting Texttospeech.oninitlistener

2. Implement the OnInit method here to load the language int result = Mtts.setlanguage (locale.us);

3. Initialize the instance Texttospeech mTts = MTts = new Texttospeech (This,
This//Texttospeech.oninitlistener
);

4. Call its Speak method private void SayHello () {
Select a random hello.
int hellolength = Hellos.length;
String Hello = Hellos[random.nextint (hellolength)];
Mtts.speak (Hello,
Texttospeech.queue_flush,//Drop all pending entries in the playback QUEUE.
NULL);
}

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.