1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.app; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.speech.tts.TextToSpeech; 22 import android.util.Log; 23 import android.view.View; 24 import android.widget.Button; 25 26 import com.example.android.apis.R; 27 28 import java.util.Locale; 29 import java.util.Random; 30 31 /** 32 * <p>Demonstrates text-to-speech (TTS). Please note the following steps:</p> 33 * 34 * <ol> 35 * <li>Construct the TextToSpeech object.</li> 36 * <li>Handle initialization callback in the onInit method. 37 * The activity implements TextToSpeech.OnInitListener for this purpose.</li> 38 * <li>Call TextToSpeech.speak to synthesize speech.</li> 39 * <li>Shutdown TextToSpeech in onDestroy.</li> 40 * </ol> 41 * 42 * <p>Documentation: 43 * http://developer.android.com/reference/android/speech/tts/package-summary.html 44 * </p> 45 * <ul> 46 */ 47 public class TextToSpeechActivity extends Activity implements TextToSpeech.OnInitListener { 48 49 private static final String TAG = "TextToSpeechDemo"; 50 51 private TextToSpeech mTts; 52 private Button mAgainButton; 53 54 @Override 55 public void onCreate(Bundle savedInstanceState) { 56 super.onCreate(savedInstanceState); 57 setContentView(R.layout.text_to_speech); 58 59 // Initialize text-to-speech. This is an asynchronous operation. 60 // The OnInitListener (second argument) is called after initialization completes. 61 mTts = new TextToSpeech(this, 62 this // TextToSpeech.OnInitListener 63 ); 64 65 // The button is disabled in the layout. 66 // It will be enabled upon initialization of the TTS engine. 67 mAgainButton = (Button) findViewById(R.id.again_button); 68 69 mAgainButton.setOnClickListener(new View.OnClickListener() { 70 public void onClick(View v) { 71 sayHello(); 72 } 73 }); 74 } 75 76 @Override 77 public void onDestroy() { 78 // Don't forget to shutdown! 79 if (mTts != null) { 80 mTts.stop(); 81 mTts.shutdown(); 82 } 83 84 super.onDestroy(); 85 } 86 87 // Implements TextToSpeech.OnInitListener. 88 public void onInit(int status) { 89 // status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR. 90 if (status == TextToSpeech.SUCCESS) { 91 // Set preferred language to US english. 92 // Note that a language may not be available, and the result will indicate this. 93 int result = mTts.setLanguage(Locale.US); 94 // Try this someday for some interesting results. 95 // int result mTts.setLanguage(Locale.FRANCE); 96 if (result == TextToSpeech.LANG_MISSING_DATA || 97 result == TextToSpeech.LANG_NOT_SUPPORTED) { 98 // Lanuage data is missing or the language is not supported. 99 Log.e(TAG, "Language is not available."); 100 } else { 101 // Check the documentation for other possible result codes. 102 // For example, the language may be available for the locale, 103 // but not for the specified country and variant. 104 105 // The TTS engine has been successfully initialized. 106 // Allow the user to press the button for the app to speak again. 107 mAgainButton.setEnabled(true); 108 // Greet the user. 109 sayHello(); 110 } 111 } else { 112 // Initialization failed. 113 Log.e(TAG, "Could not initialize TextToSpeech."); 114 } 115 } 116 117 private static final Random RANDOM = new Random(); 118 private static final String[] HELLOS = { 119 "Hello", 120 "Salutations", 121 "Greetings", 122 "Howdy", 123 "What's crack-a-lackin?", 124 "That explains the stench!" 125 }; 126 127 private void sayHello() { 128 // Select a random hello. 129 int helloLength = HELLOS.length; 130 String hello = HELLOS[RANDOM.nextInt(helloLength)]; 131 mTts.speak(hello, 132 TextToSpeech.QUEUE_FLUSH, // Drop all pending entries in the playback queue. 133 null); 134 } 135 136 } 137