blob: 186af70627514653b6ee60e6e8b2ddf3e1c31595 [file] [log] [blame]
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001/*
2 * Copyright (C) 2009 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
Jean-Michel Trivi21a6a6d2009-06-08 15:21:46 -070016package android.speech.tts;
17
Charles Chenf85aa5a2009-06-10 10:39:55 -070018import android.speech.tts.ITts;
19import android.speech.tts.ITtsCallback;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -070020
Jean-Michel Trivied065782009-07-28 14:31:48 -070021import android.annotation.SdkConstant;
22import android.annotation.SdkConstant.SdkConstantType;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -070023import android.content.ComponentName;
24import android.content.Context;
25import android.content.Intent;
26import android.content.ServiceConnection;
Jean-Michel Trivia9810132009-07-10 12:08:59 -070027import android.media.AudioManager;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -070028import android.os.IBinder;
29import android.os.RemoteException;
30import android.util.Log;
31
Jean-Michel Trivia8518c12009-06-10 17:33:34 -070032import java.util.HashMap;
Jean-Michel Trivi679d7282009-06-16 15:36:28 -070033import java.util.Locale;
Jean-Michel Trivia8518c12009-06-10 17:33:34 -070034
Jean-Michel Trivie74d5072009-05-26 10:43:08 -070035/**
Jean-Michel Trivie74d5072009-05-26 10:43:08 -070036 *
Jean-Michel Trivi62788e92009-07-02 16:29:30 -070037 * Synthesizes speech from text for immediate playback or to create a sound file.
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -070038 * <p>A TextToSpeech instance can only be used to synthesize text once it has completed its
39 * initialization. Implement the {@link TextToSpeech.OnInitListener} to be
40 * notified of the completion of the initialization.<br>
41 * When you are done using the TextToSpeech instance, call the {@link #shutdown()} method
42 * to release the native resources used by the TextToSpeech engine.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -070043 *
44 */
Jean-Michel Trivia8518c12009-06-10 17:33:34 -070045public class TextToSpeech {
Jean-Michel Trivie74d5072009-05-26 10:43:08 -070046
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -070047 /**
48 * Denotes a successful operation.
49 */
Jean-Michel Trivied065782009-07-28 14:31:48 -070050 public static final int SUCCESS = 0;
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -070051 /**
52 * Denotes a generic operation failure.
53 */
Jean-Michel Trivied065782009-07-28 14:31:48 -070054 public static final int ERROR = -1;
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -070055
Jean-Michel Trivi679d7282009-06-16 15:36:28 -070056 /**
57 * Queue mode where all entries in the playback queue (media to be played
58 * and text to be synthesized) are dropped and replaced by the new entry.
59 */
Jean-Michel Trivied065782009-07-28 14:31:48 -070060 public static final int QUEUE_FLUSH = 0;
Jean-Michel Trivi679d7282009-06-16 15:36:28 -070061 /**
62 * Queue mode where the new entry is added at the end of the playback queue.
63 */
Jean-Michel Trivied065782009-07-28 14:31:48 -070064 public static final int QUEUE_ADD = 1;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -070065
Charles Chenaaf842e2009-06-25 11:59:29 -070066
67 /**
Jean-Michel Trivi9f5eadd2009-08-14 15:44:31 -070068 * Denotes the language is available exactly as specified by the locale.
Charles Chenaaf842e2009-06-25 11:59:29 -070069 */
Jean-Michel Trivied065782009-07-28 14:31:48 -070070 public static final int LANG_COUNTRY_VAR_AVAILABLE = 2;
Charles Chenaaf842e2009-06-25 11:59:29 -070071
72
73 /**
74 * Denotes the language is available for the language and country specified
75 * by the locale, but not the variant.
76 */
Jean-Michel Trivied065782009-07-28 14:31:48 -070077 public static final int LANG_COUNTRY_AVAILABLE = 1;
Charles Chenaaf842e2009-06-25 11:59:29 -070078
79
80 /**
81 * Denotes the language is available for the language by the locale,
82 * but not the country and variant.
83 */
Jean-Michel Trivied065782009-07-28 14:31:48 -070084 public static final int LANG_AVAILABLE = 0;
Charles Chenaaf842e2009-06-25 11:59:29 -070085
86 /**
87 * Denotes the language data is missing.
88 */
Jean-Michel Trivied065782009-07-28 14:31:48 -070089 public static final int LANG_MISSING_DATA = -1;
Charles Chenaaf842e2009-06-25 11:59:29 -070090
91 /**
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -070092 * Denotes the language is not supported.
Charles Chenaaf842e2009-06-25 11:59:29 -070093 */
Jean-Michel Trivied065782009-07-28 14:31:48 -070094 public static final int LANG_NOT_SUPPORTED = -2;
95
96
97 /**
98 * Broadcast Action: The TextToSpeech synthesizer has completed processing
99 * of all the text in the speech queue.
100 */
101 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
102 public static final String ACTION_TTS_QUEUE_PROCESSING_COMPLETED =
103 "android.speech.tts.TTS_QUEUE_PROCESSING_COMPLETED";
Charles Chenaaf842e2009-06-25 11:59:29 -0700104
105
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700106 /**
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700107 * Interface definition of a callback to be invoked indicating the completion of the
108 * TextToSpeech engine initialization.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700109 */
110 public interface OnInitListener {
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700111 /**
112 * Called to signal the completion of the TextToSpeech engine initialization.
113 * @param status {@link TextToSpeech#SUCCESS} or {@link TextToSpeech#ERROR}.
114 */
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700115 public void onInit(int status);
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700116 }
117
118 /**
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700119 * Interface definition of a callback to be invoked indicating the TextToSpeech engine has
120 * completed synthesizing an utterance with an utterance ID set.
Charles Chen78c9d0d2009-07-13 16:22:41 -0700121 *
Charles Chen78c9d0d2009-07-13 16:22:41 -0700122 */
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700123 public interface OnUtteranceCompletedListener {
124 /**
125 * Called to signal the completion of the synthesis of the utterance that was identified
126 * with the string parameter. This identifier is the one originally passed in the
127 * parameter hashmap of the synthesis request in
128 * {@link TextToSpeech#speak(String, int, HashMap)} or
129 * {@link TextToSpeech#synthesizeToFile(String, HashMap, String)} with the
130 * {@link TextToSpeech.Engine#KEY_PARAM_UTTERANCE_ID} key.
131 * @param utteranceId the identifier of the utterance.
Charles Chen60dd3602010-01-07 18:56:24 -0800132 */
133 public void onUtteranceCompleted(String utteranceId);
Charles Chen78c9d0d2009-07-13 16:22:41 -0700134 }
135
136
137 /**
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700138 * Internal constants for the TextToSpeech functionality
Jean-Michel Trivid1468742009-06-18 14:41:41 -0700139 *
Jean-Michel Trivid1468742009-06-18 14:41:41 -0700140 */
141 public class Engine {
142 // default values for a TTS engine when settings are not found in the provider
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700143 /**
144 * {@hide}
145 */
Jean-Michel Trivied065782009-07-28 14:31:48 -0700146 public static final int DEFAULT_RATE = 100; // 1x
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700147 /**
148 * {@hide}
149 */
Jean-Michel Trivied065782009-07-28 14:31:48 -0700150 public static final int DEFAULT_PITCH = 100;// 1x
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700151 /**
152 * {@hide}
153 */
Jean-Michel Trivi9d2d26a2011-01-05 16:08:21 -0800154 public static final float DEFAULT_VOLUME = 1.0f;
155 /**
156 * {@hide}
157 */
158 protected static final String DEFAULT_VOLUME_STRING = "1.0";
159 /**
160 * {@hide}
161 */
162 public static final float DEFAULT_PAN = 0.0f;
163 /**
164 * {@hide}
165 */
166 protected static final String DEFAULT_PAN_STRING = "0.0";
167
168 /**
169 * {@hide}
170 */
Jean-Michel Trivied065782009-07-28 14:31:48 -0700171 public static final int USE_DEFAULTS = 0; // false
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700172 /**
173 * {@hide}
174 */
Jean-Michel Trivied065782009-07-28 14:31:48 -0700175 public static final String DEFAULT_SYNTH = "com.svox.pico";
Jean-Michel Trivid1468742009-06-18 14:41:41 -0700176
Jean-Michel Trivia9810132009-07-10 12:08:59 -0700177 // default values for rendering
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700178 /**
179 * Default audio stream used when playing synthesized speech.
180 */
Jean-Michel Trivied065782009-07-28 14:31:48 -0700181 public static final int DEFAULT_STREAM = AudioManager.STREAM_MUSIC;
Jean-Michel Trivia9810132009-07-10 12:08:59 -0700182
Jean-Michel Trivid1468742009-06-18 14:41:41 -0700183 // return codes for a TTS engine's check data activity
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700184 /**
185 * Indicates success when checking the installation status of the resources used by the
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700186 * TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700187 */
Jean-Michel Trivid1468742009-06-18 14:41:41 -0700188 public static final int CHECK_VOICE_DATA_PASS = 1;
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700189 /**
190 * Indicates failure when checking the installation status of the resources used by the
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700191 * TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700192 */
Jean-Michel Trivid1468742009-06-18 14:41:41 -0700193 public static final int CHECK_VOICE_DATA_FAIL = 0;
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700194 /**
195 * Indicates erroneous data when checking the installation status of the resources used by
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700196 * the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700197 */
Jean-Michel Trivid1468742009-06-18 14:41:41 -0700198 public static final int CHECK_VOICE_DATA_BAD_DATA = -1;
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700199 /**
200 * Indicates missing resources when checking the installation status of the resources used
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700201 * by the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700202 */
Jean-Michel Trivid1468742009-06-18 14:41:41 -0700203 public static final int CHECK_VOICE_DATA_MISSING_DATA = -2;
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700204 /**
205 * Indicates missing storage volume when checking the installation status of the resources
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700206 * used by the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700207 */
208 public static final int CHECK_VOICE_DATA_MISSING_VOLUME = -3;
Charles Chen99a0fee2009-07-02 10:41:51 -0700209
Jean-Michel Trivied065782009-07-28 14:31:48 -0700210 // intents to ask engine to install data or check its data
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700211 /**
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700212 * Activity Action: Triggers the platform TextToSpeech engine to
Jean-Michel Trivied065782009-07-28 14:31:48 -0700213 * start the activity that installs the resource files on the device
214 * that are required for TTS to be operational. Since the installation
215 * of the data can be interrupted or declined by the user, the application
216 * shouldn't expect successful installation upon return from that intent,
217 * and if need be, should check installation status with
218 * {@link #ACTION_CHECK_TTS_DATA}.
219 */
Jean-Michel Trivi9f5eadd2009-08-14 15:44:31 -0700220 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
Jean-Michel Trivied065782009-07-28 14:31:48 -0700221 public static final String ACTION_INSTALL_TTS_DATA =
222 "android.speech.tts.engine.INSTALL_TTS_DATA";
223
224 /**
Jean-Michel Trivi9f5eadd2009-08-14 15:44:31 -0700225 * Broadcast Action: broadcast to signal the completion of the installation of
226 * the data files used by the synthesis engine. Success or failure is indicated in the
227 * {@link #EXTRA_TTS_DATA_INSTALLED} extra.
Jean-Michel Trivi77a5d392009-08-07 17:26:36 -0700228 */
229 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
230 public static final String ACTION_TTS_DATA_INSTALLED =
231 "android.speech.tts.engine.TTS_DATA_INSTALLED";
232 /**
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700233 * Activity Action: Starts the activity from the platform TextToSpeech
Jean-Michel Trivied065782009-07-28 14:31:48 -0700234 * engine to verify the proper installation and availability of the
235 * resource files on the system. Upon completion, the activity will
236 * return one of the following codes:
237 * {@link #CHECK_VOICE_DATA_PASS},
238 * {@link #CHECK_VOICE_DATA_FAIL},
239 * {@link #CHECK_VOICE_DATA_BAD_DATA},
240 * {@link #CHECK_VOICE_DATA_MISSING_DATA}, or
241 * {@link #CHECK_VOICE_DATA_MISSING_VOLUME}.
242 * <p> Moreover, the data received in the activity result will contain the following
243 * fields:
244 * <ul>
245 * <li>{@link #EXTRA_VOICE_DATA_ROOT_DIRECTORY} which
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700246 * indicates the path to the location of the resource files,</li>
Jean-Michel Trivied065782009-07-28 14:31:48 -0700247 * <li>{@link #EXTRA_VOICE_DATA_FILES} which contains
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700248 * the list of all the resource files,</li>
Jean-Michel Trivied065782009-07-28 14:31:48 -0700249 * <li>and {@link #EXTRA_VOICE_DATA_FILES_INFO} which
250 * contains, for each resource file, the description of the language covered by
251 * the file in the xxx-YYY format, where xxx is the 3-letter ISO language code,
252 * and YYY is the 3-letter ISO country code.</li>
253 * </ul>
254 */
Jean-Michel Trivi9f5eadd2009-08-14 15:44:31 -0700255 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
Jean-Michel Trivied065782009-07-28 14:31:48 -0700256 public static final String ACTION_CHECK_TTS_DATA =
257 "android.speech.tts.engine.CHECK_TTS_DATA";
258
259 // extras for a TTS engine's check data activity
260 /**
261 * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700262 * the TextToSpeech engine specifies the path to its resources.
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700263 */
Jean-Michel Trivied065782009-07-28 14:31:48 -0700264 public static final String EXTRA_VOICE_DATA_ROOT_DIRECTORY = "dataRoot";
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700265 /**
Jean-Michel Trivied065782009-07-28 14:31:48 -0700266 * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700267 * the TextToSpeech engine specifies the file names of its resources under the
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700268 * resource path.
269 */
Jean-Michel Trivied065782009-07-28 14:31:48 -0700270 public static final String EXTRA_VOICE_DATA_FILES = "dataFiles";
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700271 /**
Jean-Michel Trivied065782009-07-28 14:31:48 -0700272 * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700273 * the TextToSpeech engine specifies the locale associated with each resource file.
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700274 */
Jean-Michel Trivied065782009-07-28 14:31:48 -0700275 public static final String EXTRA_VOICE_DATA_FILES_INFO = "dataFilesInfo";
Charles Chen76d9c3c2010-02-11 16:44:45 -0800276 /**
277 * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
278 * the TextToSpeech engine returns an ArrayList<String> of all the available voices.
279 * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are
280 * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").
281 */
282 public static final String EXTRA_AVAILABLE_VOICES = "availableVoices";
283 /**
284 * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
285 * the TextToSpeech engine returns an ArrayList<String> of all the unavailable voices.
286 * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are
287 * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").
288 */
289 public static final String EXTRA_UNAVAILABLE_VOICES = "unavailableVoices";
290 /**
291 * Extra information sent with the {@link #ACTION_CHECK_TTS_DATA} intent where the
292 * caller indicates to the TextToSpeech engine which specific sets of voice data to
293 * check for by sending an ArrayList<String> of the voices that are of interest.
294 * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are
295 * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").
296 */
297 public static final String EXTRA_CHECK_VOICE_DATA_FOR = "checkVoiceDataFor";
Charles Chen99a0fee2009-07-02 10:41:51 -0700298
Jean-Michel Trivi77a5d392009-08-07 17:26:36 -0700299 // extras for a TTS engine's data installation
300 /**
Jean-Michel Trivi9f5eadd2009-08-14 15:44:31 -0700301 * Extra information received with the {@link #ACTION_TTS_DATA_INSTALLED} intent.
302 * It indicates whether the data files for the synthesis engine were successfully
303 * installed. The installation was initiated with the {@link #ACTION_INSTALL_TTS_DATA}
304 * intent. The possible values for this extra are
305 * {@link TextToSpeech#SUCCESS} and {@link TextToSpeech#ERROR}.
Jean-Michel Trivi77a5d392009-08-07 17:26:36 -0700306 */
307 public static final String EXTRA_TTS_DATA_INSTALLED = "dataInstalled";
308
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700309 // keys for the parameters passed with speak commands. Hidden keys are used internally
310 // to maintain engine state for each TextToSpeech instance.
311 /**
312 * {@hide}
313 */
Jean-Michel Trivied065782009-07-28 14:31:48 -0700314 public static final String KEY_PARAM_RATE = "rate";
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700315 /**
316 * {@hide}
317 */
Jean-Michel Trivied065782009-07-28 14:31:48 -0700318 public static final String KEY_PARAM_LANGUAGE = "language";
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700319 /**
320 * {@hide}
321 */
Jean-Michel Trivied065782009-07-28 14:31:48 -0700322 public static final String KEY_PARAM_COUNTRY = "country";
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700323 /**
324 * {@hide}
325 */
Jean-Michel Trivied065782009-07-28 14:31:48 -0700326 public static final String KEY_PARAM_VARIANT = "variant";
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700327 /**
Charles Chen60dd3602010-01-07 18:56:24 -0800328 * {@hide}
329 */
330 public static final String KEY_PARAM_ENGINE = "engine";
331 /**
Charles Chen1a2712c2010-04-01 17:16:28 -0700332 * {@hide}
333 */
334 public static final String KEY_PARAM_PITCH = "pitch";
335 /**
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700336 * Parameter key to specify the audio stream type to be used when speaking text
337 * or playing back a file.
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700338 * @see TextToSpeech#speak(String, int, HashMap)
339 * @see TextToSpeech#playEarcon(String, int, HashMap)
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700340 */
Jean-Michel Trivied065782009-07-28 14:31:48 -0700341 public static final String KEY_PARAM_STREAM = "streamType";
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700342 /**
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700343 * Parameter key to identify an utterance in the
344 * {@link TextToSpeech.OnUtteranceCompletedListener} after text has been
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700345 * spoken, a file has been played back or a silence duration has elapsed.
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700346 * @see TextToSpeech#speak(String, int, HashMap)
347 * @see TextToSpeech#playEarcon(String, int, HashMap)
348 * @see TextToSpeech#synthesizeToFile(String, HashMap, String)
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700349 */
Jean-Michel Trivied065782009-07-28 14:31:48 -0700350 public static final String KEY_PARAM_UTTERANCE_ID = "utteranceId";
Jean-Michel Trivi9d2d26a2011-01-05 16:08:21 -0800351 /**
Jean-Michel Trivi9d2d26a2011-01-05 16:08:21 -0800352 * Parameter key to specify the speech volume relative to the current stream type
353 * volume used when speaking text. Volume is specified as a float ranging from 0 to 1
Jean-Michel Trivi9011ec82011-01-11 11:55:00 -0800354 * where 0 is silence, and 1 is the maximum volume (the default behavior).
Jean-Michel Trivi9d2d26a2011-01-05 16:08:21 -0800355 * @see TextToSpeech#speak(String, int, HashMap)
356 * @see TextToSpeech#playEarcon(String, int, HashMap)
357 */
358 public static final String KEY_PARAM_VOLUME = "volume";
359 /**
Jean-Michel Trivi9d2d26a2011-01-05 16:08:21 -0800360 * Parameter key to specify how the speech is panned from left to right when speaking text.
361 * Pan is specified as a float ranging from -1 to +1 where -1 maps to a hard-left pan,
Jean-Michel Trivi9011ec82011-01-11 11:55:00 -0800362 * 0 to center (the default behavior), and +1 to hard-right.
Jean-Michel Trivi9d2d26a2011-01-05 16:08:21 -0800363 * @see TextToSpeech#speak(String, int, HashMap)
364 * @see TextToSpeech#playEarcon(String, int, HashMap)
365 */
366 public static final String KEY_PARAM_PAN = "pan";
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700367
368 // key positions in the array of cached parameters
369 /**
370 * {@hide}
371 */
Jean-Michel Trivied065782009-07-28 14:31:48 -0700372 protected static final int PARAM_POSITION_RATE = 0;
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700373 /**
374 * {@hide}
375 */
Jean-Michel Trivied065782009-07-28 14:31:48 -0700376 protected static final int PARAM_POSITION_LANGUAGE = 2;
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700377 /**
378 * {@hide}
379 */
Jean-Michel Trivied065782009-07-28 14:31:48 -0700380 protected static final int PARAM_POSITION_COUNTRY = 4;
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700381 /**
382 * {@hide}
383 */
Jean-Michel Trivied065782009-07-28 14:31:48 -0700384 protected static final int PARAM_POSITION_VARIANT = 6;
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700385 /**
386 * {@hide}
387 */
Jean-Michel Trivied065782009-07-28 14:31:48 -0700388 protected static final int PARAM_POSITION_STREAM = 8;
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700389 /**
390 * {@hide}
391 */
Jean-Michel Trivied065782009-07-28 14:31:48 -0700392 protected static final int PARAM_POSITION_UTTERANCE_ID = 10;
Charles Chen60dd3602010-01-07 18:56:24 -0800393
Jean-Michel Trivi62253a32009-07-14 13:55:19 -0700394 /**
395 * {@hide}
396 */
Charles Chen60dd3602010-01-07 18:56:24 -0800397 protected static final int PARAM_POSITION_ENGINE = 12;
398
399 /**
400 * {@hide}
401 */
Charles Chen1a2712c2010-04-01 17:16:28 -0700402 protected static final int PARAM_POSITION_PITCH = 14;
403
404 /**
405 * {@hide}
406 */
Jean-Michel Trivi9d2d26a2011-01-05 16:08:21 -0800407 protected static final int PARAM_POSITION_VOLUME = 16;
408
409 /**
410 * {@hide}
411 */
412 protected static final int PARAM_POSITION_PAN = 18;
413
414
415 /**
416 * {@hide}
Jean-Michel Trivi708b47d2011-03-03 18:55:19 -0800417 * Total number of cached speech parameters.
418 * This number should be equal to (max param position/2) + 1.
Jean-Michel Trivi9d2d26a2011-01-05 16:08:21 -0800419 */
Jean-Michel Trivi708b47d2011-03-03 18:55:19 -0800420 protected static final int NB_CACHED_PARAMS = 10;
Jean-Michel Trivid1468742009-06-18 14:41:41 -0700421 }
422
423 /**
Jean-Michel Trivi679d7282009-06-16 15:36:28 -0700424 * Connection needed for the TTS.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700425 */
Jean-Michel Trivi679d7282009-06-16 15:36:28 -0700426 private ServiceConnection mServiceConnection;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700427
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700428 private ITts mITts = null;
Charles Chen78c9d0d2009-07-13 16:22:41 -0700429 private ITtsCallback mITtscallback = null;
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700430 private Context mContext = null;
Charles Chena9c5e4b2009-07-09 20:27:15 -0700431 private String mPackageName = "";
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700432 private OnInitListener mInitListener = null;
433 private boolean mStarted = false;
434 private final Object mStartLock = new Object();
Jean-Michel Trivida7681e2009-07-07 15:12:23 -0700435 /**
436 * Used to store the cached parameters sent along with each synthesis request to the
437 * TTS service.
438 */
Jean-Michel Trivi87c96842009-06-25 14:29:15 -0700439 private String[] mCachedParams;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700440
441 /**
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700442 * The constructor for the TextToSpeech class.
443 * This will also initialize the associated TextToSpeech engine if it isn't already running.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700444 *
445 * @param context
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700446 * The context this instance is running in.
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700447 * @param listener
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700448 * The {@link TextToSpeech.OnInitListener} that will be called when the
449 * TextToSpeech engine has initialized.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700450 */
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700451 public TextToSpeech(Context context, OnInitListener listener) {
452 mContext = context;
Charles Chena9c5e4b2009-07-09 20:27:15 -0700453 mPackageName = mContext.getPackageName();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700454 mInitListener = listener;
Jean-Michel Trivi87c96842009-06-25 14:29:15 -0700455
Jean-Michel Trivied065782009-07-28 14:31:48 -0700456 mCachedParams = new String[2*Engine.NB_CACHED_PARAMS]; // store key and value
457 mCachedParams[Engine.PARAM_POSITION_RATE] = Engine.KEY_PARAM_RATE;
458 mCachedParams[Engine.PARAM_POSITION_LANGUAGE] = Engine.KEY_PARAM_LANGUAGE;
459 mCachedParams[Engine.PARAM_POSITION_COUNTRY] = Engine.KEY_PARAM_COUNTRY;
460 mCachedParams[Engine.PARAM_POSITION_VARIANT] = Engine.KEY_PARAM_VARIANT;
461 mCachedParams[Engine.PARAM_POSITION_STREAM] = Engine.KEY_PARAM_STREAM;
462 mCachedParams[Engine.PARAM_POSITION_UTTERANCE_ID] = Engine.KEY_PARAM_UTTERANCE_ID;
Charles Chen60dd3602010-01-07 18:56:24 -0800463 mCachedParams[Engine.PARAM_POSITION_ENGINE] = Engine.KEY_PARAM_ENGINE;
Charles Chen1a2712c2010-04-01 17:16:28 -0700464 mCachedParams[Engine.PARAM_POSITION_PITCH] = Engine.KEY_PARAM_PITCH;
Jean-Michel Trivi9d2d26a2011-01-05 16:08:21 -0800465 mCachedParams[Engine.PARAM_POSITION_VOLUME] = Engine.KEY_PARAM_VOLUME;
466 mCachedParams[Engine.PARAM_POSITION_PAN] = Engine.KEY_PARAM_PAN;
Jean-Michel Trivida7681e2009-07-07 15:12:23 -0700467
Charles Chen1a2712c2010-04-01 17:16:28 -0700468 // Leave all defaults that are shown in Settings uninitialized/at the default
469 // so that the values set in Settings will take effect if the application does
Charles Chen6a8b73b2010-03-01 18:52:41 -0800470 // not try to change these settings itself.
471 mCachedParams[Engine.PARAM_POSITION_RATE + 1] = "";
472 mCachedParams[Engine.PARAM_POSITION_LANGUAGE + 1] = "";
473 mCachedParams[Engine.PARAM_POSITION_COUNTRY + 1] = "";
474 mCachedParams[Engine.PARAM_POSITION_VARIANT + 1] = "";
Jean-Michel Trivied065782009-07-28 14:31:48 -0700475 mCachedParams[Engine.PARAM_POSITION_STREAM + 1] =
476 String.valueOf(Engine.DEFAULT_STREAM);
477 mCachedParams[Engine.PARAM_POSITION_UTTERANCE_ID + 1] = "";
Charles Chen6a8b73b2010-03-01 18:52:41 -0800478 mCachedParams[Engine.PARAM_POSITION_ENGINE + 1] = "";
Charles Chen1a2712c2010-04-01 17:16:28 -0700479 mCachedParams[Engine.PARAM_POSITION_PITCH + 1] = "100";
Jean-Michel Trivi9d2d26a2011-01-05 16:08:21 -0800480 mCachedParams[Engine.PARAM_POSITION_VOLUME + 1] = Engine.DEFAULT_VOLUME_STRING;
481 mCachedParams[Engine.PARAM_POSITION_PAN + 1] = Engine.DEFAULT_PAN_STRING;
Jean-Michel Trivia9810132009-07-10 12:08:59 -0700482
Jean-Michel Trivia8518c12009-06-10 17:33:34 -0700483 initTts();
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700484 }
485
486
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700487 private void initTts() {
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700488 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700489
490 // Initialize the TTS, run the callback after the binding is successful
Jean-Michel Trivi679d7282009-06-16 15:36:28 -0700491 mServiceConnection = new ServiceConnection() {
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700492 public void onServiceConnected(ComponentName name, IBinder service) {
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700493 synchronized(mStartLock) {
494 mITts = ITts.Stub.asInterface(service);
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700495 mStarted = true;
Charles Chen1a2712c2010-04-01 17:16:28 -0700496 // Cache the default engine and current language
497 setEngineByPackageName(getDefaultEngine());
498 setLanguage(getLanguage());
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700499 if (mInitListener != null) {
500 // TODO manage failures and missing resources
Jean-Michel Trivied065782009-07-28 14:31:48 -0700501 mInitListener.onInit(SUCCESS);
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700502 }
503 }
504 }
505
506 public void onServiceDisconnected(ComponentName name) {
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700507 synchronized(mStartLock) {
508 mITts = null;
509 mInitListener = null;
510 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700511 }
512 }
513 };
514
Charles Chen52ae0652009-07-06 16:10:32 -0700515 Intent intent = new Intent("android.intent.action.START_TTS_SERVICE");
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700516 intent.addCategory("android.intent.category.TTS");
Jean-Michel Trivi1e13a022011-01-20 17:25:48 -0800517 boolean bound = mContext.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
518 if (!bound) {
519 Log.e("TextToSpeech.java", "initTts() failed to bind to service");
520 if (mInitListener != null) {
521 mInitListener.onInit(ERROR);
522 }
523 } else {
524 // initialization listener will be called inside ServiceConnection
525 Log.i("TextToSpeech.java", "initTts() successfully bound to service");
526 }
527 // TODO handle plugin failures
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700528 }
529
530
531 /**
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700532 * Releases the resources used by the TextToSpeech engine.
533 * It is good practice for instance to call this method in the onDestroy() method of an Activity
534 * so the TextToSpeech engine can be cleanly stopped.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700535 */
536 public void shutdown() {
537 try {
Jean-Michel Trivi679d7282009-06-16 15:36:28 -0700538 mContext.unbindService(mServiceConnection);
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700539 } catch (IllegalArgumentException e) {
540 // Do nothing and fail silently since an error here indicates that
541 // binding never succeeded in the first place.
542 }
543 }
544
545
546 /**
547 * Adds a mapping between a string of text and a sound resource in a
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700548 * package. After a call to this method, subsequent calls to
549 * {@link #speak(String, int, HashMap)} will play the specified sound resource
550 * if it is available, or synthesize the text it is missing.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700551 *
552 * @param text
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700553 * The string of text. Example: <code>"south_south_east"</code>
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700554 *
555 * @param packagename
556 * Pass the packagename of the application that contains the
557 * resource. If the resource is in your own application (this is
558 * the most common case), then put the packagename of your
559 * application here.<br/>
560 * Example: <b>"com.google.marvin.compass"</b><br/>
561 * The packagename can be found in the AndroidManifest.xml of
562 * your application.
563 * <p>
564 * <code>&lt;manifest xmlns:android=&quot;...&quot;
565 * package=&quot;<b>com.google.marvin.compass</b>&quot;&gt;</code>
566 * </p>
567 *
568 * @param resourceId
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700569 * Example: <code>R.raw.south_south_east</code>
Charles Chen5c22f512009-06-29 15:52:47 -0700570 *
Jean-Michel Trivied065782009-07-28 14:31:48 -0700571 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700572 */
Charles Chen5c22f512009-06-29 15:52:47 -0700573 public int addSpeech(String text, String packagename, int resourceId) {
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700574 synchronized(mStartLock) {
575 if (!mStarted) {
Jean-Michel Trivied065782009-07-28 14:31:48 -0700576 return ERROR;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700577 }
578 try {
Charles Chena9c5e4b2009-07-09 20:27:15 -0700579 mITts.addSpeech(mPackageName, text, packagename, resourceId);
Jean-Michel Trivied065782009-07-28 14:31:48 -0700580 return SUCCESS;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700581 } catch (RemoteException e) {
582 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -0700583 Log.e("TextToSpeech.java - addSpeech", "RemoteException");
584 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700585 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700586 initTts();
587 } catch (NullPointerException e) {
588 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -0700589 Log.e("TextToSpeech.java - addSpeech", "NullPointerException");
590 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700591 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700592 initTts();
593 } catch (IllegalStateException e) {
594 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -0700595 Log.e("TextToSpeech.java - addSpeech", "IllegalStateException");
596 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700597 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700598 initTts();
599 }
Jean-Michel Trivied065782009-07-28 14:31:48 -0700600 return ERROR;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700601 }
602 }
603
604
605 /**
606 * Adds a mapping between a string of text and a sound file. Using this, it
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700607 * is possible to add custom pronounciations for a string of text.
608 * After a call to this method, subsequent calls to {@link #speak(String, int, HashMap)}
609 * will play the specified sound resource if it is available, or synthesize the text it is
610 * missing.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700611 *
612 * @param text
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700613 * The string of text. Example: <code>"south_south_east"</code>
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700614 * @param filename
615 * The full path to the sound file (for example:
616 * "/sdcard/mysounds/hello.wav")
Charles Chen5c22f512009-06-29 15:52:47 -0700617 *
Jean-Michel Trivied065782009-07-28 14:31:48 -0700618 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700619 */
Charles Chen5c22f512009-06-29 15:52:47 -0700620 public int addSpeech(String text, String filename) {
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700621 synchronized (mStartLock) {
622 if (!mStarted) {
Jean-Michel Trivied065782009-07-28 14:31:48 -0700623 return ERROR;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700624 }
625 try {
Charles Chena9c5e4b2009-07-09 20:27:15 -0700626 mITts.addSpeechFile(mPackageName, text, filename);
Jean-Michel Trivied065782009-07-28 14:31:48 -0700627 return SUCCESS;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700628 } catch (RemoteException e) {
629 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -0700630 Log.e("TextToSpeech.java - addSpeech", "RemoteException");
631 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700632 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700633 initTts();
634 } catch (NullPointerException e) {
635 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -0700636 Log.e("TextToSpeech.java - addSpeech", "NullPointerException");
637 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700638 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700639 initTts();
640 } catch (IllegalStateException e) {
641 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -0700642 Log.e("TextToSpeech.java - addSpeech", "IllegalStateException");
643 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700644 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700645 initTts();
646 }
Jean-Michel Trivied065782009-07-28 14:31:48 -0700647 return ERROR;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700648 }
649 }
650
651
652 /**
Charles Chen904dfa52009-07-15 10:44:41 -0700653 * Adds a mapping between a string of text and a sound resource in a
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700654 * package. Use this to add custom earcons.
Charles Chen904dfa52009-07-15 10:44:41 -0700655 *
Jean-Michel Trivied065782009-07-28 14:31:48 -0700656 * @see #playEarcon(String, int, HashMap)
Charles Chen904dfa52009-07-15 10:44:41 -0700657 *
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700658 * @param earcon The name of the earcon.
659 * Example: <code>"[tick]"</code><br/>
Charles Chen904dfa52009-07-15 10:44:41 -0700660 *
661 * @param packagename
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700662 * the package name of the application that contains the
663 * resource. This can for instance be the package name of your own application.
Charles Chen904dfa52009-07-15 10:44:41 -0700664 * Example: <b>"com.google.marvin.compass"</b><br/>
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700665 * The package name can be found in the AndroidManifest.xml of
666 * the application containing the resource.
Charles Chen904dfa52009-07-15 10:44:41 -0700667 * <p>
668 * <code>&lt;manifest xmlns:android=&quot;...&quot;
669 * package=&quot;<b>com.google.marvin.compass</b>&quot;&gt;</code>
670 * </p>
671 *
672 * @param resourceId
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700673 * Example: <code>R.raw.tick_snd</code>
Charles Chen904dfa52009-07-15 10:44:41 -0700674 *
Jean-Michel Trivied065782009-07-28 14:31:48 -0700675 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
Charles Chen904dfa52009-07-15 10:44:41 -0700676 */
677 public int addEarcon(String earcon, String packagename, int resourceId) {
678 synchronized(mStartLock) {
679 if (!mStarted) {
Jean-Michel Trivied065782009-07-28 14:31:48 -0700680 return ERROR;
Charles Chen904dfa52009-07-15 10:44:41 -0700681 }
682 try {
683 mITts.addEarcon(mPackageName, earcon, packagename, resourceId);
Jean-Michel Trivied065782009-07-28 14:31:48 -0700684 return SUCCESS;
Charles Chen904dfa52009-07-15 10:44:41 -0700685 } catch (RemoteException e) {
686 // TTS died; restart it.
687 Log.e("TextToSpeech.java - addEarcon", "RemoteException");
688 e.printStackTrace();
689 mStarted = false;
690 initTts();
691 } catch (NullPointerException e) {
692 // TTS died; restart it.
693 Log.e("TextToSpeech.java - addEarcon", "NullPointerException");
694 e.printStackTrace();
695 mStarted = false;
696 initTts();
697 } catch (IllegalStateException e) {
698 // TTS died; restart it.
699 Log.e("TextToSpeech.java - addEarcon", "IllegalStateException");
700 e.printStackTrace();
701 mStarted = false;
702 initTts();
703 }
Jean-Michel Trivied065782009-07-28 14:31:48 -0700704 return ERROR;
Charles Chen904dfa52009-07-15 10:44:41 -0700705 }
706 }
707
708
709 /**
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700710 * Adds a mapping between a string of text and a sound file.
711 * Use this to add custom earcons.
712 *
713 * @see #playEarcon(String, int, HashMap)
Charles Chen904dfa52009-07-15 10:44:41 -0700714 *
715 * @param earcon
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700716 * The name of the earcon.
717 * Example: <code>"[tick]"</code>
Charles Chen904dfa52009-07-15 10:44:41 -0700718 * @param filename
719 * The full path to the sound file (for example:
720 * "/sdcard/mysounds/tick.wav")
721 *
Jean-Michel Trivied065782009-07-28 14:31:48 -0700722 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
Charles Chen904dfa52009-07-15 10:44:41 -0700723 */
724 public int addEarcon(String earcon, String filename) {
725 synchronized (mStartLock) {
726 if (!mStarted) {
Jean-Michel Trivied065782009-07-28 14:31:48 -0700727 return ERROR;
Charles Chen904dfa52009-07-15 10:44:41 -0700728 }
729 try {
730 mITts.addEarconFile(mPackageName, earcon, filename);
Jean-Michel Trivied065782009-07-28 14:31:48 -0700731 return SUCCESS;
Charles Chen904dfa52009-07-15 10:44:41 -0700732 } catch (RemoteException e) {
733 // TTS died; restart it.
734 Log.e("TextToSpeech.java - addEarcon", "RemoteException");
735 e.printStackTrace();
736 mStarted = false;
737 initTts();
738 } catch (NullPointerException e) {
739 // TTS died; restart it.
740 Log.e("TextToSpeech.java - addEarcon", "NullPointerException");
741 e.printStackTrace();
742 mStarted = false;
743 initTts();
744 } catch (IllegalStateException e) {
745 // TTS died; restart it.
746 Log.e("TextToSpeech.java - addEarcon", "IllegalStateException");
747 e.printStackTrace();
748 mStarted = false;
749 initTts();
750 }
Jean-Michel Trivied065782009-07-28 14:31:48 -0700751 return ERROR;
Charles Chen904dfa52009-07-15 10:44:41 -0700752 }
753 }
754
755
756 /**
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700757 * Speaks the string using the specified queuing strategy and speech
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700758 * parameters.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700759 *
760 * @param text
761 * The string of text to be spoken.
762 * @param queueMode
Jean-Michel Trivi679d7282009-06-16 15:36:28 -0700763 * The queuing strategy to use.
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700764 * {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700765 * @param params
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700766 * The list of parameters to be used. Can be null if no parameters are given.
767 * They are specified using a (key, value) pair, where the key can be
768 * {@link Engine#KEY_PARAM_STREAM} or
769 * {@link Engine#KEY_PARAM_UTTERANCE_ID}.
Charles Chen5c22f512009-06-29 15:52:47 -0700770 *
Jean-Michel Trivied065782009-07-28 14:31:48 -0700771 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700772 */
Charles Chen5c22f512009-06-29 15:52:47 -0700773 public int speak(String text, int queueMode, HashMap<String,String> params)
Jean-Michel Trivia8518c12009-06-10 17:33:34 -0700774 {
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700775 synchronized (mStartLock) {
Jean-Michel Trivied065782009-07-28 14:31:48 -0700776 int result = ERROR;
Jean-Michel Trivi1e13a022011-01-20 17:25:48 -0800777 Log.i("TextToSpeech.java - speak", "speak text of length " + text.length());
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700778 if (!mStarted) {
Jean-Michel Trivi1e13a022011-01-20 17:25:48 -0800779 Log.e("TextToSpeech.java - speak", "service isn't started");
Charles Chenc8ba3b52009-07-07 11:38:48 -0700780 return result;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700781 }
782 try {
Charles Chen630a8de2009-07-10 20:58:15 -0700783 if ((params != null) && (!params.isEmpty())) {
Jean-Michel Trivi9d2d26a2011-01-05 16:08:21 -0800784 setCachedParam(params, Engine.KEY_PARAM_STREAM, Engine.PARAM_POSITION_STREAM);
785 setCachedParam(params, Engine.KEY_PARAM_UTTERANCE_ID,
786 Engine.PARAM_POSITION_UTTERANCE_ID);
787 setCachedParam(params, Engine.KEY_PARAM_ENGINE, Engine.PARAM_POSITION_ENGINE);
788 setCachedParam(params, Engine.KEY_PARAM_VOLUME, Engine.PARAM_POSITION_VOLUME);
789 setCachedParam(params, Engine.KEY_PARAM_PAN, Engine.PARAM_POSITION_PAN);
Jean-Michel Trivia9810132009-07-10 12:08:59 -0700790 }
Charles Chena9c5e4b2009-07-09 20:27:15 -0700791 result = mITts.speak(mPackageName, text, queueMode, mCachedParams);
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700792 } catch (RemoteException e) {
793 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -0700794 Log.e("TextToSpeech.java - speak", "RemoteException");
795 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700796 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700797 initTts();
798 } catch (NullPointerException e) {
799 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -0700800 Log.e("TextToSpeech.java - speak", "NullPointerException");
801 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700802 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700803 initTts();
804 } catch (IllegalStateException e) {
805 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -0700806 Log.e("TextToSpeech.java - speak", "IllegalStateException");
807 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700808 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700809 initTts();
Charles Chenc8ba3b52009-07-07 11:38:48 -0700810 } finally {
Jean-Michel Trivia9810132009-07-10 12:08:59 -0700811 resetCachedParams();
812 return result;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700813 }
814 }
815 }
816
817
818 /**
819 * Plays the earcon using the specified queueing mode and parameters.
820 *
821 * @param earcon
822 * The earcon that should be played
823 * @param queueMode
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700824 * {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700825 * @param params
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700826 * The list of parameters to be used. Can be null if no parameters are given.
827 * They are specified using a (key, value) pair, where the key can be
828 * {@link Engine#KEY_PARAM_STREAM} or
829 * {@link Engine#KEY_PARAM_UTTERANCE_ID}.
Charles Chen5c22f512009-06-29 15:52:47 -0700830 *
Jean-Michel Trivied065782009-07-28 14:31:48 -0700831 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700832 */
Charles Chen5c22f512009-06-29 15:52:47 -0700833 public int playEarcon(String earcon, int queueMode,
Jean-Michel Trivia8518c12009-06-10 17:33:34 -0700834 HashMap<String,String> params) {
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700835 synchronized (mStartLock) {
Jean-Michel Trivied065782009-07-28 14:31:48 -0700836 int result = ERROR;
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700837 if (!mStarted) {
Charles Chenc8ba3b52009-07-07 11:38:48 -0700838 return result;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700839 }
840 try {
Jean-Michel Trivia9810132009-07-10 12:08:59 -0700841 if ((params != null) && (!params.isEmpty())) {
Jean-Michel Trivied065782009-07-28 14:31:48 -0700842 String extra = params.get(Engine.KEY_PARAM_STREAM);
Jean-Michel Trivia9810132009-07-10 12:08:59 -0700843 if (extra != null) {
Jean-Michel Trivied065782009-07-28 14:31:48 -0700844 mCachedParams[Engine.PARAM_POSITION_STREAM + 1] = extra;
Jean-Michel Trivia9810132009-07-10 12:08:59 -0700845 }
Jean-Michel Trivi9d2d26a2011-01-05 16:08:21 -0800846 setCachedParam(params, Engine.KEY_PARAM_STREAM, Engine.PARAM_POSITION_STREAM);
847 setCachedParam(params, Engine.KEY_PARAM_UTTERANCE_ID,
848 Engine.PARAM_POSITION_UTTERANCE_ID);
Jean-Michel Trivia9810132009-07-10 12:08:59 -0700849 }
Charles Chena9c5e4b2009-07-09 20:27:15 -0700850 result = mITts.playEarcon(mPackageName, earcon, queueMode, null);
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700851 } catch (RemoteException e) {
852 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -0700853 Log.e("TextToSpeech.java - playEarcon", "RemoteException");
854 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700855 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700856 initTts();
857 } catch (NullPointerException e) {
858 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -0700859 Log.e("TextToSpeech.java - playEarcon", "NullPointerException");
860 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700861 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700862 initTts();
863 } catch (IllegalStateException e) {
864 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -0700865 Log.e("TextToSpeech.java - playEarcon", "IllegalStateException");
866 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700867 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700868 initTts();
Charles Chenc8ba3b52009-07-07 11:38:48 -0700869 } finally {
Jean-Michel Trivia9810132009-07-10 12:08:59 -0700870 resetCachedParams();
871 return result;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700872 }
873 }
874 }
Jean-Michel Trivi679d7282009-06-16 15:36:28 -0700875
Charles Chen5c22f512009-06-29 15:52:47 -0700876 /**
877 * Plays silence for the specified amount of time using the specified
878 * queue mode.
879 *
880 * @param durationInMs
881 * A long that indicates how long the silence should last.
882 * @param queueMode
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700883 * {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
884 * @param params
885 * The list of parameters to be used. Can be null if no parameters are given.
886 * They are specified using a (key, value) pair, where the key can be
887 * {@link Engine#KEY_PARAM_UTTERANCE_ID}.
Charles Chen5c22f512009-06-29 15:52:47 -0700888 *
Jean-Michel Trivied065782009-07-28 14:31:48 -0700889 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
Charles Chen5c22f512009-06-29 15:52:47 -0700890 */
Charles Chen78c9d0d2009-07-13 16:22:41 -0700891 public int playSilence(long durationInMs, int queueMode, HashMap<String,String> params) {
Charles Chenf032bc72009-06-26 14:41:55 -0700892 synchronized (mStartLock) {
Jean-Michel Trivied065782009-07-28 14:31:48 -0700893 int result = ERROR;
Charles Chenf032bc72009-06-26 14:41:55 -0700894 if (!mStarted) {
Charles Chenc8ba3b52009-07-07 11:38:48 -0700895 return result;
Charles Chenf032bc72009-06-26 14:41:55 -0700896 }
897 try {
Charles Chen78c9d0d2009-07-13 16:22:41 -0700898 if ((params != null) && (!params.isEmpty())) {
Jean-Michel Trivi9d2d26a2011-01-05 16:08:21 -0800899 setCachedParam(params, Engine.KEY_PARAM_UTTERANCE_ID,
900 Engine.PARAM_POSITION_UTTERANCE_ID);
Charles Chen78c9d0d2009-07-13 16:22:41 -0700901 }
Charles Chena9c5e4b2009-07-09 20:27:15 -0700902 result = mITts.playSilence(mPackageName, durationInMs, queueMode, mCachedParams);
Charles Chenf032bc72009-06-26 14:41:55 -0700903 } catch (RemoteException e) {
904 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -0700905 Log.e("TextToSpeech.java - playSilence", "RemoteException");
906 e.printStackTrace();
Charles Chenf032bc72009-06-26 14:41:55 -0700907 mStarted = false;
908 initTts();
909 } catch (NullPointerException e) {
910 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -0700911 Log.e("TextToSpeech.java - playSilence", "NullPointerException");
912 e.printStackTrace();
Charles Chenf032bc72009-06-26 14:41:55 -0700913 mStarted = false;
914 initTts();
915 } catch (IllegalStateException e) {
916 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -0700917 Log.e("TextToSpeech.java - playSilence", "IllegalStateException");
918 e.printStackTrace();
Charles Chenf032bc72009-06-26 14:41:55 -0700919 mStarted = false;
920 initTts();
Charles Chenc8ba3b52009-07-07 11:38:48 -0700921 } finally {
Jean-Michel Trivi9d2d26a2011-01-05 16:08:21 -0800922 resetCachedParams();
Charles Chen60dd3602010-01-07 18:56:24 -0800923 return result;
Charles Chenf032bc72009-06-26 14:41:55 -0700924 }
925 }
Jean-Michel Trivia8518c12009-06-10 17:33:34 -0700926 }
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700927
928
929 /**
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700930 * Returns whether or not the TextToSpeech engine is busy speaking.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700931 *
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700932 * @return Whether or not the TextToSpeech engine is busy speaking.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700933 */
934 public boolean isSpeaking() {
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700935 synchronized (mStartLock) {
936 if (!mStarted) {
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700937 return false;
938 }
939 try {
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700940 return mITts.isSpeaking();
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700941 } catch (RemoteException e) {
942 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -0700943 Log.e("TextToSpeech.java - isSpeaking", "RemoteException");
944 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700945 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700946 initTts();
947 } catch (NullPointerException e) {
948 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -0700949 Log.e("TextToSpeech.java - isSpeaking", "NullPointerException");
950 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700951 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700952 initTts();
953 } catch (IllegalStateException e) {
954 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -0700955 Log.e("TextToSpeech.java - isSpeaking", "IllegalStateException");
956 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700957 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700958 initTts();
959 }
960 return false;
961 }
962 }
963
964
965 /**
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -0700966 * Interrupts the current utterance (whether played or rendered to file) and discards other
967 * utterances in the queue.
Charles Chen5c22f512009-06-29 15:52:47 -0700968 *
Jean-Michel Trivied065782009-07-28 14:31:48 -0700969 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700970 */
Charles Chen5c22f512009-06-29 15:52:47 -0700971 public int stop() {
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700972 synchronized (mStartLock) {
Jean-Michel Trivied065782009-07-28 14:31:48 -0700973 int result = ERROR;
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700974 if (!mStarted) {
Charles Chenc8ba3b52009-07-07 11:38:48 -0700975 return result;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700976 }
977 try {
Charles Chena9c5e4b2009-07-09 20:27:15 -0700978 result = mITts.stop(mPackageName);
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700979 } catch (RemoteException e) {
980 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -0700981 Log.e("TextToSpeech.java - stop", "RemoteException");
982 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700983 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700984 initTts();
985 } catch (NullPointerException e) {
986 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -0700987 Log.e("TextToSpeech.java - stop", "NullPointerException");
988 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700989 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700990 initTts();
991 } catch (IllegalStateException e) {
992 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -0700993 Log.e("TextToSpeech.java - stop", "IllegalStateException");
994 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -0700995 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700996 initTts();
Charles Chenc8ba3b52009-07-07 11:38:48 -0700997 } finally {
Charles Chen60dd3602010-01-07 18:56:24 -0800998 return result;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -0700999 }
1000 }
1001 }
1002
1003
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001004 /**
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -07001005 * Sets the speech rate for the TextToSpeech engine.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001006 *
Jean-Michel Trivi679d7282009-06-16 15:36:28 -07001007 * This has no effect on any pre-recorded speech.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001008 *
1009 * @param speechRate
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -07001010 * The speech rate for the TextToSpeech engine. 1 is the normal speed,
Jean-Michel Trivi679d7282009-06-16 15:36:28 -07001011 * lower values slow down the speech (0.5 is half the normal speech rate),
1012 * greater values accelerate it (2 is twice the normal speech rate).
Charles Chen5c22f512009-06-29 15:52:47 -07001013 *
Jean-Michel Trivied065782009-07-28 14:31:48 -07001014 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001015 */
Charles Chen5c22f512009-06-29 15:52:47 -07001016 public int setSpeechRate(float speechRate) {
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -07001017 synchronized (mStartLock) {
Jean-Michel Trivied065782009-07-28 14:31:48 -07001018 int result = ERROR;
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -07001019 if (!mStarted) {
Charles Chenc8ba3b52009-07-07 11:38:48 -07001020 return result;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001021 }
1022 try {
Jean-Michel Trivi679d7282009-06-16 15:36:28 -07001023 if (speechRate > 0) {
Jean-Michel Trivida7681e2009-07-07 15:12:23 -07001024 int rate = (int)(speechRate*100);
Jean-Michel Trivied065782009-07-28 14:31:48 -07001025 mCachedParams[Engine.PARAM_POSITION_RATE + 1] = String.valueOf(rate);
Jean-Michel Trivi5e11a6a2009-07-20 14:05:33 -07001026 // the rate is not set here, instead it is cached so it will be associated
1027 // with all upcoming utterances.
1028 if (speechRate > 0.0f) {
Jean-Michel Trivied065782009-07-28 14:31:48 -07001029 result = SUCCESS;
Jean-Michel Trivi5e11a6a2009-07-20 14:05:33 -07001030 } else {
Jean-Michel Trivied065782009-07-28 14:31:48 -07001031 result = ERROR;
Jean-Michel Trivi5e11a6a2009-07-20 14:05:33 -07001032 }
Jean-Michel Trivi679d7282009-06-16 15:36:28 -07001033 }
Charles Chen630a8de2009-07-10 20:58:15 -07001034 } catch (NullPointerException e) {
1035 // TTS died; restart it.
1036 Log.e("TextToSpeech.java - setSpeechRate", "NullPointerException");
1037 e.printStackTrace();
1038 mStarted = false;
1039 initTts();
1040 } catch (IllegalStateException e) {
1041 // TTS died; restart it.
1042 Log.e("TextToSpeech.java - setSpeechRate", "IllegalStateException");
1043 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -07001044 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001045 initTts();
Charles Chenc8ba3b52009-07-07 11:38:48 -07001046 } finally {
Charles Chen60dd3602010-01-07 18:56:24 -08001047 return result;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001048 }
1049 }
1050 }
1051
1052
1053 /**
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -07001054 * Sets the speech pitch for the TextToSpeech engine.
Jean-Michel Trivi2ea53492009-06-23 13:44:40 -07001055 *
Jean-Michel Trivi2ea53492009-06-23 13:44:40 -07001056 * This has no effect on any pre-recorded speech.
1057 *
1058 * @param pitch
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -07001059 * The pitch for the TextToSpeech engine. 1 is the normal pitch,
Jean-Michel Trivi2ea53492009-06-23 13:44:40 -07001060 * lower values lower the tone of the synthesized voice,
1061 * greater values increase it.
Charles Chen5c22f512009-06-29 15:52:47 -07001062 *
Jean-Michel Trivied065782009-07-28 14:31:48 -07001063 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
Jean-Michel Trivi2ea53492009-06-23 13:44:40 -07001064 */
Charles Chen5c22f512009-06-29 15:52:47 -07001065 public int setPitch(float pitch) {
Jean-Michel Trivi2ea53492009-06-23 13:44:40 -07001066 synchronized (mStartLock) {
Jean-Michel Trivied065782009-07-28 14:31:48 -07001067 int result = ERROR;
Jean-Michel Trivi2ea53492009-06-23 13:44:40 -07001068 if (!mStarted) {
Charles Chenc8ba3b52009-07-07 11:38:48 -07001069 return result;
Jean-Michel Trivi2ea53492009-06-23 13:44:40 -07001070 }
1071 try {
Charles Chen1a2712c2010-04-01 17:16:28 -07001072 // the pitch is not set here, instead it is cached so it will be associated
1073 // with all upcoming utterances.
Jean-Michel Trivi2ea53492009-06-23 13:44:40 -07001074 if (pitch > 0) {
Charles Chen1a2712c2010-04-01 17:16:28 -07001075 int p = (int)(pitch*100);
1076 mCachedParams[Engine.PARAM_POSITION_PITCH + 1] = String.valueOf(p);
1077 result = SUCCESS;
Jean-Michel Trivi2ea53492009-06-23 13:44:40 -07001078 }
Charles Chen630a8de2009-07-10 20:58:15 -07001079 } catch (NullPointerException e) {
1080 // TTS died; restart it.
1081 Log.e("TextToSpeech.java - setPitch", "NullPointerException");
1082 e.printStackTrace();
1083 mStarted = false;
1084 initTts();
1085 } catch (IllegalStateException e) {
1086 // TTS died; restart it.
1087 Log.e("TextToSpeech.java - setPitch", "IllegalStateException");
1088 e.printStackTrace();
Jean-Michel Trivi2ea53492009-06-23 13:44:40 -07001089 mStarted = false;
1090 initTts();
Charles Chenc8ba3b52009-07-07 11:38:48 -07001091 } finally {
Charles Chen60dd3602010-01-07 18:56:24 -08001092 return result;
Jean-Michel Trivi2ea53492009-06-23 13:44:40 -07001093 }
1094 }
1095 }
1096
1097
1098 /**
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -07001099 * Sets the language for the TextToSpeech engine.
1100 * The TextToSpeech engine will try to use the closest match to the specified
1101 * language as represented by the Locale, but there is no guarantee that the exact same Locale
1102 * will be used. Use {@link #isLanguageAvailable(Locale)} to check the level of support
1103 * before choosing the language to use for the next utterances.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001104 *
Jean-Michel Trivi679d7282009-06-16 15:36:28 -07001105 * @param loc
1106 * The locale describing the language to be used.
Charles Chen5c22f512009-06-29 15:52:47 -07001107 *
Jean-Michel Trivied065782009-07-28 14:31:48 -07001108 * @return code indicating the support status for the locale. See {@link #LANG_AVAILABLE},
1109 * {@link #LANG_COUNTRY_AVAILABLE}, {@link #LANG_COUNTRY_VAR_AVAILABLE},
1110 * {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001111 */
Charles Chen5c22f512009-06-29 15:52:47 -07001112 public int setLanguage(Locale loc) {
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -07001113 synchronized (mStartLock) {
Jean-Michel Trivied065782009-07-28 14:31:48 -07001114 int result = LANG_NOT_SUPPORTED;
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -07001115 if (!mStarted) {
Charles Chenc8ba3b52009-07-07 11:38:48 -07001116 return result;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001117 }
Jean-Michel Trivi4a60d612010-07-26 16:11:58 -07001118 if (loc == null) {
1119 return result;
1120 }
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001121 try {
Charles Chen1a2712c2010-04-01 17:16:28 -07001122 String language = loc.getISO3Language();
1123 String country = loc.getISO3Country();
1124 String variant = loc.getVariant();
1125 // Check if the language, country, variant are available, and cache
1126 // the available parts.
1127 // Note that the language is not actually set here, instead it is cached so it
1128 // will be associated with all upcoming utterances.
1129 result = mITts.isLanguageAvailable(language, country, variant, mCachedParams);
1130 if (result >= LANG_AVAILABLE){
1131 mCachedParams[Engine.PARAM_POSITION_LANGUAGE + 1] = language;
1132 if (result >= LANG_COUNTRY_AVAILABLE){
1133 mCachedParams[Engine.PARAM_POSITION_COUNTRY + 1] = country;
1134 } else {
1135 mCachedParams[Engine.PARAM_POSITION_COUNTRY + 1] = "";
1136 }
1137 if (result >= LANG_COUNTRY_VAR_AVAILABLE){
1138 mCachedParams[Engine.PARAM_POSITION_VARIANT + 1] = variant;
1139 } else {
1140 mCachedParams[Engine.PARAM_POSITION_VARIANT + 1] = "";
1141 }
1142 }
Charles Chen748efcc2009-07-08 16:12:29 -07001143 } catch (RemoteException e) {
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001144 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -07001145 Log.e("TextToSpeech.java - setLanguage", "RemoteException");
1146 e.printStackTrace();
1147 mStarted = false;
1148 initTts();
1149 } catch (NullPointerException e) {
1150 // TTS died; restart it.
1151 Log.e("TextToSpeech.java - setLanguage", "NullPointerException");
1152 e.printStackTrace();
1153 mStarted = false;
1154 initTts();
1155 } catch (IllegalStateException e) {
1156 // TTS died; restart it.
1157 Log.e("TextToSpeech.java - setLanguage", "IllegalStateException");
1158 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -07001159 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001160 initTts();
Charles Chenc8ba3b52009-07-07 11:38:48 -07001161 } finally {
Charles Chen60dd3602010-01-07 18:56:24 -08001162 return result;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001163 }
1164 }
1165 }
1166
Charles Chen5c22f512009-06-29 15:52:47 -07001167
Charles Chenaaf842e2009-06-25 11:59:29 -07001168 /**
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -07001169 * Returns a Locale instance describing the language currently being used by the TextToSpeech
1170 * engine.
Jean-Michel Trividdb0a802009-06-29 16:38:47 -07001171 * @return language, country (if any) and variant (if any) used by the engine stored in a Locale
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -07001172 * instance, or null is the TextToSpeech engine has failed.
Jean-Michel Trividdb0a802009-06-29 16:38:47 -07001173 */
1174 public Locale getLanguage() {
1175 synchronized (mStartLock) {
1176 if (!mStarted) {
1177 return null;
1178 }
1179 try {
Charles Chen1a2712c2010-04-01 17:16:28 -07001180 // Only do a call to the native synth if there is nothing in the cached params
1181 if (mCachedParams[Engine.PARAM_POSITION_LANGUAGE + 1].length() < 1){
1182 String[] locStrings = mITts.getLanguage();
1183 if ((locStrings != null) && (locStrings.length == 3)) {
1184 return new Locale(locStrings[0], locStrings[1], locStrings[2]);
1185 } else {
1186 return null;
1187 }
Jean-Michel Trividdb0a802009-06-29 16:38:47 -07001188 } else {
Charles Chen1a2712c2010-04-01 17:16:28 -07001189 return new Locale(mCachedParams[Engine.PARAM_POSITION_LANGUAGE + 1],
1190 mCachedParams[Engine.PARAM_POSITION_COUNTRY + 1],
1191 mCachedParams[Engine.PARAM_POSITION_VARIANT + 1]);
Jean-Michel Trividdb0a802009-06-29 16:38:47 -07001192 }
1193 } catch (RemoteException e) {
1194 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -07001195 Log.e("TextToSpeech.java - getLanguage", "RemoteException");
1196 e.printStackTrace();
1197 mStarted = false;
1198 initTts();
1199 } catch (NullPointerException e) {
1200 // TTS died; restart it.
1201 Log.e("TextToSpeech.java - getLanguage", "NullPointerException");
1202 e.printStackTrace();
1203 mStarted = false;
1204 initTts();
1205 } catch (IllegalStateException e) {
1206 // TTS died; restart it.
1207 Log.e("TextToSpeech.java - getLanguage", "IllegalStateException");
1208 e.printStackTrace();
Jean-Michel Trividdb0a802009-06-29 16:38:47 -07001209 mStarted = false;
1210 initTts();
1211 }
1212 return null;
1213 }
1214 }
1215
1216 /**
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -07001217 * Checks if the specified language as represented by the Locale is available and supported.
Charles Chenaaf842e2009-06-25 11:59:29 -07001218 *
1219 * @param loc
Jean-Michel Trividdb0a802009-06-29 16:38:47 -07001220 * The Locale describing the language to be used.
Charles Chen5c22f512009-06-29 15:52:47 -07001221 *
Jean-Michel Trivied065782009-07-28 14:31:48 -07001222 * @return code indicating the support status for the locale. See {@link #LANG_AVAILABLE},
1223 * {@link #LANG_COUNTRY_AVAILABLE}, {@link #LANG_COUNTRY_VAR_AVAILABLE},
1224 * {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}.
Charles Chenaaf842e2009-06-25 11:59:29 -07001225 */
1226 public int isLanguageAvailable(Locale loc) {
Jean-Michel Trividdb0a802009-06-29 16:38:47 -07001227 synchronized (mStartLock) {
Jean-Michel Trivied065782009-07-28 14:31:48 -07001228 int result = LANG_NOT_SUPPORTED;
Jean-Michel Trividdb0a802009-06-29 16:38:47 -07001229 if (!mStarted) {
Charles Chenc8ba3b52009-07-07 11:38:48 -07001230 return result;
Jean-Michel Trividdb0a802009-06-29 16:38:47 -07001231 }
1232 try {
Charles Chenc8ba3b52009-07-07 11:38:48 -07001233 result = mITts.isLanguageAvailable(loc.getISO3Language(),
Charles Chen1a2712c2010-04-01 17:16:28 -07001234 loc.getISO3Country(), loc.getVariant(), mCachedParams);
Jean-Michel Trividdb0a802009-06-29 16:38:47 -07001235 } catch (RemoteException e) {
1236 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -07001237 Log.e("TextToSpeech.java - isLanguageAvailable", "RemoteException");
1238 e.printStackTrace();
1239 mStarted = false;
1240 initTts();
1241 } catch (NullPointerException e) {
1242 // TTS died; restart it.
1243 Log.e("TextToSpeech.java - isLanguageAvailable", "NullPointerException");
1244 e.printStackTrace();
1245 mStarted = false;
1246 initTts();
1247 } catch (IllegalStateException e) {
1248 // TTS died; restart it.
1249 Log.e("TextToSpeech.java - isLanguageAvailable", "IllegalStateException");
1250 e.printStackTrace();
Jean-Michel Trividdb0a802009-06-29 16:38:47 -07001251 mStarted = false;
1252 initTts();
Charles Chenc8ba3b52009-07-07 11:38:48 -07001253 } finally {
Charles Chen60dd3602010-01-07 18:56:24 -08001254 return result;
Jean-Michel Trividdb0a802009-06-29 16:38:47 -07001255 }
Jean-Michel Trividdb0a802009-06-29 16:38:47 -07001256 }
Charles Chenaaf842e2009-06-25 11:59:29 -07001257 }
1258
1259
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001260 /**
Charles Chend4989092009-06-26 15:32:46 -07001261 * Synthesizes the given text to a file using the specified parameters.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001262 *
1263 * @param text
1264 * The String of text that should be synthesized
1265 * @param params
Jean-Michel Trivia9b417e2009-09-11 18:34:02 -07001266 * The list of parameters to be used. Can be null if no parameters are given.
1267 * They are specified using a (key, value) pair, where the key can be
1268 * {@link Engine#KEY_PARAM_UTTERANCE_ID}.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001269 * @param filename
1270 * The string that gives the full output filename; it should be
1271 * something like "/sdcard/myappsounds/mysound.wav".
Charles Chen5c22f512009-06-29 15:52:47 -07001272 *
Jean-Michel Trivied065782009-07-28 14:31:48 -07001273 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001274 */
Charles Chen5c22f512009-06-29 15:52:47 -07001275 public int synthesizeToFile(String text, HashMap<String,String> params,
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001276 String filename) {
Jean-Michel Trividc871fd2011-01-24 09:35:57 -08001277 Log.i("TextToSpeech.java", "synthesizeToFile()");
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -07001278 synchronized (mStartLock) {
Jean-Michel Trivied065782009-07-28 14:31:48 -07001279 int result = ERROR;
Jean-Michel Trivi1e13a022011-01-20 17:25:48 -08001280 Log.i("TextToSpeech.java - synthesizeToFile", "synthesizeToFile text of length "
1281 + text.length());
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -07001282 if (!mStarted) {
Jean-Michel Trivi1e13a022011-01-20 17:25:48 -08001283 Log.e("TextToSpeech.java - synthesizeToFile", "service isn't started");
Charles Chenc8ba3b52009-07-07 11:38:48 -07001284 return result;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001285 }
1286 try {
Charles Chen630a8de2009-07-10 20:58:15 -07001287 if ((params != null) && (!params.isEmpty())) {
1288 // no need to read the stream type here
Jean-Michel Trivi9d2d26a2011-01-05 16:08:21 -08001289 setCachedParam(params, Engine.KEY_PARAM_UTTERANCE_ID,
1290 Engine.PARAM_POSITION_UTTERANCE_ID);
1291 setCachedParam(params, Engine.KEY_PARAM_ENGINE, Engine.PARAM_POSITION_ENGINE);
Jean-Michel Trivia9810132009-07-10 12:08:59 -07001292 }
Jean-Michel Trivib21651c12010-03-04 12:12:56 -08001293 result = mITts.synthesizeToFile(mPackageName, text, mCachedParams, filename) ?
1294 SUCCESS : ERROR;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001295 } catch (RemoteException e) {
1296 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -07001297 Log.e("TextToSpeech.java - synthesizeToFile", "RemoteException");
1298 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -07001299 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001300 initTts();
1301 } catch (NullPointerException e) {
1302 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -07001303 Log.e("TextToSpeech.java - synthesizeToFile", "NullPointerException");
1304 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -07001305 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001306 initTts();
1307 } catch (IllegalStateException e) {
1308 // TTS died; restart it.
Charles Chen630a8de2009-07-10 20:58:15 -07001309 Log.e("TextToSpeech.java - synthesizeToFile", "IllegalStateException");
1310 e.printStackTrace();
Jean-Michel Trivi91bf30a2009-06-11 14:35:48 -07001311 mStarted = false;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001312 initTts();
Charles Chenc8ba3b52009-07-07 11:38:48 -07001313 } finally {
Jean-Michel Trivia9810132009-07-10 12:08:59 -07001314 resetCachedParams();
1315 return result;
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001316 }
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001317 }
1318 }
1319
Jean-Michel Trivia9810132009-07-10 12:08:59 -07001320
1321 /**
1322 * Convenience method to reset the cached parameters to the current default values
1323 * if they are not persistent between calls to the service.
1324 */
1325 private void resetCachedParams() {
Jean-Michel Trivied065782009-07-28 14:31:48 -07001326 mCachedParams[Engine.PARAM_POSITION_STREAM + 1] =
1327 String.valueOf(Engine.DEFAULT_STREAM);
1328 mCachedParams[Engine.PARAM_POSITION_UTTERANCE_ID+ 1] = "";
Jean-Michel Trivi9d2d26a2011-01-05 16:08:21 -08001329 mCachedParams[Engine.PARAM_POSITION_VOLUME + 1] = Engine.DEFAULT_VOLUME_STRING;
1330 mCachedParams[Engine.PARAM_POSITION_PAN + 1] = Engine.DEFAULT_PAN_STRING;
1331 }
1332
1333 /**
1334 * Convenience method to save a parameter in the cached parameter array, at the given index,
1335 * for a property saved in the given hashmap.
1336 */
1337 private void setCachedParam(HashMap<String,String> params, String key, int keyIndex) {
1338 String extra = params.get(key);
1339 if (extra != null) {
1340 mCachedParams[keyIndex+1] = extra;
1341 }
Jean-Michel Trivia9810132009-07-10 12:08:59 -07001342 }
1343
Charles Chen78c9d0d2009-07-13 16:22:41 -07001344 /**
1345 * Sets the OnUtteranceCompletedListener that will fire when an utterance completes.
1346 *
1347 * @param listener
1348 * The OnUtteranceCompletedListener
1349 *
Jean-Michel Trivied065782009-07-28 14:31:48 -07001350 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
Charles Chen78c9d0d2009-07-13 16:22:41 -07001351 */
Charles Chen60dd3602010-01-07 18:56:24 -08001352 public int setOnUtteranceCompletedListener(
1353 final OnUtteranceCompletedListener listener) {
Charles Chen78c9d0d2009-07-13 16:22:41 -07001354 synchronized (mStartLock) {
Jean-Michel Trivied065782009-07-28 14:31:48 -07001355 int result = ERROR;
Charles Chen78c9d0d2009-07-13 16:22:41 -07001356 if (!mStarted) {
1357 return result;
1358 }
1359 mITtscallback = new ITtsCallback.Stub() {
Charles Chen60dd3602010-01-07 18:56:24 -08001360 public void utteranceCompleted(String utteranceId) throws RemoteException {
1361 if (listener != null) {
1362 listener.onUtteranceCompleted(utteranceId);
1363 }
1364 }
Charles Chen78c9d0d2009-07-13 16:22:41 -07001365 };
1366 try {
1367 result = mITts.registerCallback(mPackageName, mITtscallback);
1368 } catch (RemoteException e) {
1369 // TTS died; restart it.
1370 Log.e("TextToSpeech.java - registerCallback", "RemoteException");
1371 e.printStackTrace();
1372 mStarted = false;
1373 initTts();
1374 } catch (NullPointerException e) {
1375 // TTS died; restart it.
1376 Log.e("TextToSpeech.java - registerCallback", "NullPointerException");
1377 e.printStackTrace();
1378 mStarted = false;
1379 initTts();
1380 } catch (IllegalStateException e) {
1381 // TTS died; restart it.
1382 Log.e("TextToSpeech.java - registerCallback", "IllegalStateException");
1383 e.printStackTrace();
1384 mStarted = false;
1385 initTts();
1386 } finally {
1387 return result;
1388 }
Charles Chen60dd3602010-01-07 18:56:24 -08001389 }
Charles Chen78c9d0d2009-07-13 16:22:41 -07001390 }
1391
Charles Chenb4fbe762009-11-18 16:34:32 -08001392 /**
1393 * Sets the speech synthesis engine to be used by its packagename.
1394 *
1395 * @param enginePackageName
1396 * The packagename for the synthesis engine (ie, "com.svox.pico")
1397 *
1398 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
1399 */
Charles Chen60dd3602010-01-07 18:56:24 -08001400 public int setEngineByPackageName(String enginePackageName) {
1401 synchronized (mStartLock) {
1402 int result = TextToSpeech.ERROR;
1403 if (!mStarted) {
1404 return result;
1405 }
1406 try {
1407 result = mITts.setEngineByPackageName(enginePackageName);
1408 if (result == TextToSpeech.SUCCESS){
1409 mCachedParams[Engine.PARAM_POSITION_ENGINE + 1] = enginePackageName;
1410 }
1411 } catch (RemoteException e) {
1412 // TTS died; restart it.
1413 Log.e("TextToSpeech.java - setEngineByPackageName", "RemoteException");
1414 e.printStackTrace();
1415 mStarted = false;
1416 initTts();
1417 } catch (NullPointerException e) {
1418 // TTS died; restart it.
1419 Log.e("TextToSpeech.java - setEngineByPackageName", "NullPointerException");
1420 e.printStackTrace();
1421 mStarted = false;
1422 initTts();
1423 } catch (IllegalStateException e) {
1424 // TTS died; restart it.
1425 Log.e("TextToSpeech.java - setEngineByPackageName", "IllegalStateException");
1426 e.printStackTrace();
1427 mStarted = false;
1428 initTts();
1429 } finally {
1430 return result;
1431 }
1432 }
Charles Chenb4fbe762009-11-18 16:34:32 -08001433 }
1434
Charles Chendef71852010-03-25 19:59:50 -07001435
1436 /**
1437 * Gets the packagename of the default speech synthesis engine.
1438 *
1439 * @return Packagename of the TTS engine that the user has chosen as their default.
Charles Chendef71852010-03-25 19:59:50 -07001440 */
1441 public String getDefaultEngine() {
1442 synchronized (mStartLock) {
1443 String engineName = "";
1444 if (!mStarted) {
1445 return engineName;
1446 }
1447 try {
1448 engineName = mITts.getDefaultEngine();
1449 } catch (RemoteException e) {
1450 // TTS died; restart it.
1451 Log.e("TextToSpeech.java - setEngineByPackageName", "RemoteException");
1452 e.printStackTrace();
1453 mStarted = false;
1454 initTts();
1455 } catch (NullPointerException e) {
1456 // TTS died; restart it.
1457 Log.e("TextToSpeech.java - setEngineByPackageName", "NullPointerException");
1458 e.printStackTrace();
1459 mStarted = false;
1460 initTts();
1461 } catch (IllegalStateException e) {
1462 // TTS died; restart it.
1463 Log.e("TextToSpeech.java - setEngineByPackageName", "IllegalStateException");
1464 e.printStackTrace();
1465 mStarted = false;
1466 initTts();
1467 } finally {
1468 return engineName;
1469 }
1470 }
1471 }
Charles Chen42229252010-03-29 18:30:30 -07001472
1473
1474 /**
1475 * Returns whether or not the user is forcing their defaults to override the
1476 * Text-To-Speech settings set by applications.
1477 *
1478 * @return Whether or not defaults are enforced.
Charles Chen42229252010-03-29 18:30:30 -07001479 */
1480 public boolean areDefaultsEnforced() {
1481 synchronized (mStartLock) {
1482 boolean defaultsEnforced = false;
1483 if (!mStarted) {
1484 return defaultsEnforced;
1485 }
1486 try {
1487 defaultsEnforced = mITts.areDefaultsEnforced();
1488 } catch (RemoteException e) {
1489 // TTS died; restart it.
1490 Log.e("TextToSpeech.java - areDefaultsEnforced", "RemoteException");
1491 e.printStackTrace();
1492 mStarted = false;
1493 initTts();
1494 } catch (NullPointerException e) {
1495 // TTS died; restart it.
1496 Log.e("TextToSpeech.java - areDefaultsEnforced", "NullPointerException");
1497 e.printStackTrace();
1498 mStarted = false;
1499 initTts();
1500 } catch (IllegalStateException e) {
1501 // TTS died; restart it.
1502 Log.e("TextToSpeech.java - areDefaultsEnforced", "IllegalStateException");
1503 e.printStackTrace();
1504 mStarted = false;
1505 initTts();
1506 } finally {
1507 return defaultsEnforced;
1508 }
1509 }
1510 }
Jean-Michel Trivie74d5072009-05-26 10:43:08 -07001511}