TextToSpeech.java revision 4a60d61887a20d349e5eb38900dfbcaab06630fc
1/* 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 */ 16package android.speech.tts; 17 18import android.speech.tts.ITts; 19import android.speech.tts.ITtsCallback; 20 21import android.annotation.SdkConstant; 22import android.annotation.SdkConstant.SdkConstantType; 23import android.content.ComponentName; 24import android.content.Context; 25import android.content.Intent; 26import android.content.ServiceConnection; 27import android.media.AudioManager; 28import android.os.IBinder; 29import android.os.RemoteException; 30import android.util.Log; 31 32import java.util.HashMap; 33import java.util.Locale; 34 35/** 36 * 37 * Synthesizes speech from text for immediate playback or to create a sound file. 38 * <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. 43 * 44 */ 45public class TextToSpeech { 46 47 /** 48 * Denotes a successful operation. 49 */ 50 public static final int SUCCESS = 0; 51 /** 52 * Denotes a generic operation failure. 53 */ 54 public static final int ERROR = -1; 55 56 /** 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 */ 60 public static final int QUEUE_FLUSH = 0; 61 /** 62 * Queue mode where the new entry is added at the end of the playback queue. 63 */ 64 public static final int QUEUE_ADD = 1; 65 66 67 /** 68 * Denotes the language is available exactly as specified by the locale. 69 */ 70 public static final int LANG_COUNTRY_VAR_AVAILABLE = 2; 71 72 73 /** 74 * Denotes the language is available for the language and country specified 75 * by the locale, but not the variant. 76 */ 77 public static final int LANG_COUNTRY_AVAILABLE = 1; 78 79 80 /** 81 * Denotes the language is available for the language by the locale, 82 * but not the country and variant. 83 */ 84 public static final int LANG_AVAILABLE = 0; 85 86 /** 87 * Denotes the language data is missing. 88 */ 89 public static final int LANG_MISSING_DATA = -1; 90 91 /** 92 * Denotes the language is not supported. 93 */ 94 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"; 104 105 106 /** 107 * Interface definition of a callback to be invoked indicating the completion of the 108 * TextToSpeech engine initialization. 109 */ 110 public interface OnInitListener { 111 /** 112 * Called to signal the completion of the TextToSpeech engine initialization. 113 * @param status {@link TextToSpeech#SUCCESS} or {@link TextToSpeech#ERROR}. 114 */ 115 public void onInit(int status); 116 } 117 118 /** 119 * Interface definition of a callback to be invoked indicating the TextToSpeech engine has 120 * completed synthesizing an utterance with an utterance ID set. 121 * 122 */ 123 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. 132 */ 133 public void onUtteranceCompleted(String utteranceId); 134 } 135 136 137 /** 138 * Internal constants for the TextToSpeech functionality 139 * 140 */ 141 public class Engine { 142 // default values for a TTS engine when settings are not found in the provider 143 /** 144 * {@hide} 145 */ 146 public static final int DEFAULT_RATE = 100; // 1x 147 /** 148 * {@hide} 149 */ 150 public static final int DEFAULT_PITCH = 100;// 1x 151 /** 152 * {@hide} 153 */ 154 public static final int USE_DEFAULTS = 0; // false 155 /** 156 * {@hide} 157 */ 158 public static final String DEFAULT_SYNTH = "com.svox.pico"; 159 160 // default values for rendering 161 /** 162 * Default audio stream used when playing synthesized speech. 163 */ 164 public static final int DEFAULT_STREAM = AudioManager.STREAM_MUSIC; 165 166 // return codes for a TTS engine's check data activity 167 /** 168 * Indicates success when checking the installation status of the resources used by the 169 * TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent. 170 */ 171 public static final int CHECK_VOICE_DATA_PASS = 1; 172 /** 173 * Indicates failure when checking the installation status of the resources used by the 174 * TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent. 175 */ 176 public static final int CHECK_VOICE_DATA_FAIL = 0; 177 /** 178 * Indicates erroneous data when checking the installation status of the resources used by 179 * the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent. 180 */ 181 public static final int CHECK_VOICE_DATA_BAD_DATA = -1; 182 /** 183 * Indicates missing resources when checking the installation status of the resources used 184 * by the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent. 185 */ 186 public static final int CHECK_VOICE_DATA_MISSING_DATA = -2; 187 /** 188 * Indicates missing storage volume when checking the installation status of the resources 189 * used by the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent. 190 */ 191 public static final int CHECK_VOICE_DATA_MISSING_VOLUME = -3; 192 193 // intents to ask engine to install data or check its data 194 /** 195 * Activity Action: Triggers the platform TextToSpeech engine to 196 * start the activity that installs the resource files on the device 197 * that are required for TTS to be operational. Since the installation 198 * of the data can be interrupted or declined by the user, the application 199 * shouldn't expect successful installation upon return from that intent, 200 * and if need be, should check installation status with 201 * {@link #ACTION_CHECK_TTS_DATA}. 202 */ 203 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) 204 public static final String ACTION_INSTALL_TTS_DATA = 205 "android.speech.tts.engine.INSTALL_TTS_DATA"; 206 207 /** 208 * Broadcast Action: broadcast to signal the completion of the installation of 209 * the data files used by the synthesis engine. Success or failure is indicated in the 210 * {@link #EXTRA_TTS_DATA_INSTALLED} extra. 211 */ 212 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 213 public static final String ACTION_TTS_DATA_INSTALLED = 214 "android.speech.tts.engine.TTS_DATA_INSTALLED"; 215 /** 216 * Activity Action: Starts the activity from the platform TextToSpeech 217 * engine to verify the proper installation and availability of the 218 * resource files on the system. Upon completion, the activity will 219 * return one of the following codes: 220 * {@link #CHECK_VOICE_DATA_PASS}, 221 * {@link #CHECK_VOICE_DATA_FAIL}, 222 * {@link #CHECK_VOICE_DATA_BAD_DATA}, 223 * {@link #CHECK_VOICE_DATA_MISSING_DATA}, or 224 * {@link #CHECK_VOICE_DATA_MISSING_VOLUME}. 225 * <p> Moreover, the data received in the activity result will contain the following 226 * fields: 227 * <ul> 228 * <li>{@link #EXTRA_VOICE_DATA_ROOT_DIRECTORY} which 229 * indicates the path to the location of the resource files,</li> 230 * <li>{@link #EXTRA_VOICE_DATA_FILES} which contains 231 * the list of all the resource files,</li> 232 * <li>and {@link #EXTRA_VOICE_DATA_FILES_INFO} which 233 * contains, for each resource file, the description of the language covered by 234 * the file in the xxx-YYY format, where xxx is the 3-letter ISO language code, 235 * and YYY is the 3-letter ISO country code.</li> 236 * </ul> 237 */ 238 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) 239 public static final String ACTION_CHECK_TTS_DATA = 240 "android.speech.tts.engine.CHECK_TTS_DATA"; 241 242 // extras for a TTS engine's check data activity 243 /** 244 * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where 245 * the TextToSpeech engine specifies the path to its resources. 246 */ 247 public static final String EXTRA_VOICE_DATA_ROOT_DIRECTORY = "dataRoot"; 248 /** 249 * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where 250 * the TextToSpeech engine specifies the file names of its resources under the 251 * resource path. 252 */ 253 public static final String EXTRA_VOICE_DATA_FILES = "dataFiles"; 254 /** 255 * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where 256 * the TextToSpeech engine specifies the locale associated with each resource file. 257 */ 258 public static final String EXTRA_VOICE_DATA_FILES_INFO = "dataFilesInfo"; 259 /** 260 * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where 261 * the TextToSpeech engine returns an ArrayList<String> of all the available voices. 262 * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are 263 * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE"). 264 */ 265 public static final String EXTRA_AVAILABLE_VOICES = "availableVoices"; 266 /** 267 * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where 268 * the TextToSpeech engine returns an ArrayList<String> of all the unavailable voices. 269 * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are 270 * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE"). 271 */ 272 public static final String EXTRA_UNAVAILABLE_VOICES = "unavailableVoices"; 273 /** 274 * Extra information sent with the {@link #ACTION_CHECK_TTS_DATA} intent where the 275 * caller indicates to the TextToSpeech engine which specific sets of voice data to 276 * check for by sending an ArrayList<String> of the voices that are of interest. 277 * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are 278 * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE"). 279 */ 280 public static final String EXTRA_CHECK_VOICE_DATA_FOR = "checkVoiceDataFor"; 281 282 // extras for a TTS engine's data installation 283 /** 284 * Extra information received with the {@link #ACTION_TTS_DATA_INSTALLED} intent. 285 * It indicates whether the data files for the synthesis engine were successfully 286 * installed. The installation was initiated with the {@link #ACTION_INSTALL_TTS_DATA} 287 * intent. The possible values for this extra are 288 * {@link TextToSpeech#SUCCESS} and {@link TextToSpeech#ERROR}. 289 */ 290 public static final String EXTRA_TTS_DATA_INSTALLED = "dataInstalled"; 291 292 // keys for the parameters passed with speak commands. Hidden keys are used internally 293 // to maintain engine state for each TextToSpeech instance. 294 /** 295 * {@hide} 296 */ 297 public static final String KEY_PARAM_RATE = "rate"; 298 /** 299 * {@hide} 300 */ 301 public static final String KEY_PARAM_LANGUAGE = "language"; 302 /** 303 * {@hide} 304 */ 305 public static final String KEY_PARAM_COUNTRY = "country"; 306 /** 307 * {@hide} 308 */ 309 public static final String KEY_PARAM_VARIANT = "variant"; 310 /** 311 * {@hide} 312 */ 313 public static final String KEY_PARAM_ENGINE = "engine"; 314 /** 315 * {@hide} 316 */ 317 public static final String KEY_PARAM_PITCH = "pitch"; 318 /** 319 * Parameter key to specify the audio stream type to be used when speaking text 320 * or playing back a file. 321 * @see TextToSpeech#speak(String, int, HashMap) 322 * @see TextToSpeech#playEarcon(String, int, HashMap) 323 */ 324 public static final String KEY_PARAM_STREAM = "streamType"; 325 /** 326 * Parameter key to identify an utterance in the 327 * {@link TextToSpeech.OnUtteranceCompletedListener} after text has been 328 * spoken, a file has been played back or a silence duration has elapsed. 329 * @see TextToSpeech#speak(String, int, HashMap) 330 * @see TextToSpeech#playEarcon(String, int, HashMap) 331 * @see TextToSpeech#synthesizeToFile(String, HashMap, String) 332 */ 333 public static final String KEY_PARAM_UTTERANCE_ID = "utteranceId"; 334 335 // key positions in the array of cached parameters 336 /** 337 * {@hide} 338 */ 339 protected static final int PARAM_POSITION_RATE = 0; 340 /** 341 * {@hide} 342 */ 343 protected static final int PARAM_POSITION_LANGUAGE = 2; 344 /** 345 * {@hide} 346 */ 347 protected static final int PARAM_POSITION_COUNTRY = 4; 348 /** 349 * {@hide} 350 */ 351 protected static final int PARAM_POSITION_VARIANT = 6; 352 /** 353 * {@hide} 354 */ 355 protected static final int PARAM_POSITION_STREAM = 8; 356 /** 357 * {@hide} 358 */ 359 protected static final int PARAM_POSITION_UTTERANCE_ID = 10; 360 361 /** 362 * {@hide} 363 */ 364 protected static final int PARAM_POSITION_ENGINE = 12; 365 366 /** 367 * {@hide} 368 */ 369 protected static final int PARAM_POSITION_PITCH = 14; 370 371 /** 372 * {@hide} 373 */ 374 protected static final int NB_CACHED_PARAMS = 8; 375 } 376 377 /** 378 * Connection needed for the TTS. 379 */ 380 private ServiceConnection mServiceConnection; 381 382 private ITts mITts = null; 383 private ITtsCallback mITtscallback = null; 384 private Context mContext = null; 385 private String mPackageName = ""; 386 private OnInitListener mInitListener = null; 387 private boolean mStarted = false; 388 private final Object mStartLock = new Object(); 389 /** 390 * Used to store the cached parameters sent along with each synthesis request to the 391 * TTS service. 392 */ 393 private String[] mCachedParams; 394 395 /** 396 * The constructor for the TextToSpeech class. 397 * This will also initialize the associated TextToSpeech engine if it isn't already running. 398 * 399 * @param context 400 * The context this instance is running in. 401 * @param listener 402 * The {@link TextToSpeech.OnInitListener} that will be called when the 403 * TextToSpeech engine has initialized. 404 */ 405 public TextToSpeech(Context context, OnInitListener listener) { 406 mContext = context; 407 mPackageName = mContext.getPackageName(); 408 mInitListener = listener; 409 410 mCachedParams = new String[2*Engine.NB_CACHED_PARAMS]; // store key and value 411 mCachedParams[Engine.PARAM_POSITION_RATE] = Engine.KEY_PARAM_RATE; 412 mCachedParams[Engine.PARAM_POSITION_LANGUAGE] = Engine.KEY_PARAM_LANGUAGE; 413 mCachedParams[Engine.PARAM_POSITION_COUNTRY] = Engine.KEY_PARAM_COUNTRY; 414 mCachedParams[Engine.PARAM_POSITION_VARIANT] = Engine.KEY_PARAM_VARIANT; 415 mCachedParams[Engine.PARAM_POSITION_STREAM] = Engine.KEY_PARAM_STREAM; 416 mCachedParams[Engine.PARAM_POSITION_UTTERANCE_ID] = Engine.KEY_PARAM_UTTERANCE_ID; 417 mCachedParams[Engine.PARAM_POSITION_ENGINE] = Engine.KEY_PARAM_ENGINE; 418 mCachedParams[Engine.PARAM_POSITION_PITCH] = Engine.KEY_PARAM_PITCH; 419 420 // Leave all defaults that are shown in Settings uninitialized/at the default 421 // so that the values set in Settings will take effect if the application does 422 // not try to change these settings itself. 423 mCachedParams[Engine.PARAM_POSITION_RATE + 1] = ""; 424 mCachedParams[Engine.PARAM_POSITION_LANGUAGE + 1] = ""; 425 mCachedParams[Engine.PARAM_POSITION_COUNTRY + 1] = ""; 426 mCachedParams[Engine.PARAM_POSITION_VARIANT + 1] = ""; 427 mCachedParams[Engine.PARAM_POSITION_STREAM + 1] = 428 String.valueOf(Engine.DEFAULT_STREAM); 429 mCachedParams[Engine.PARAM_POSITION_UTTERANCE_ID + 1] = ""; 430 mCachedParams[Engine.PARAM_POSITION_ENGINE + 1] = ""; 431 mCachedParams[Engine.PARAM_POSITION_PITCH + 1] = "100"; 432 433 initTts(); 434 } 435 436 437 private void initTts() { 438 mStarted = false; 439 440 // Initialize the TTS, run the callback after the binding is successful 441 mServiceConnection = new ServiceConnection() { 442 public void onServiceConnected(ComponentName name, IBinder service) { 443 synchronized(mStartLock) { 444 mITts = ITts.Stub.asInterface(service); 445 mStarted = true; 446 // Cache the default engine and current language 447 setEngineByPackageName(getDefaultEngine()); 448 setLanguage(getLanguage()); 449 if (mInitListener != null) { 450 // TODO manage failures and missing resources 451 mInitListener.onInit(SUCCESS); 452 } 453 } 454 } 455 456 public void onServiceDisconnected(ComponentName name) { 457 synchronized(mStartLock) { 458 mITts = null; 459 mInitListener = null; 460 mStarted = false; 461 } 462 } 463 }; 464 465 Intent intent = new Intent("android.intent.action.START_TTS_SERVICE"); 466 intent.addCategory("android.intent.category.TTS"); 467 mContext.bindService(intent, mServiceConnection, 468 Context.BIND_AUTO_CREATE); 469 // TODO handle case where the binding works (should always work) but 470 // the plugin fails 471 } 472 473 474 /** 475 * Releases the resources used by the TextToSpeech engine. 476 * It is good practice for instance to call this method in the onDestroy() method of an Activity 477 * so the TextToSpeech engine can be cleanly stopped. 478 */ 479 public void shutdown() { 480 try { 481 mContext.unbindService(mServiceConnection); 482 } catch (IllegalArgumentException e) { 483 // Do nothing and fail silently since an error here indicates that 484 // binding never succeeded in the first place. 485 } 486 } 487 488 489 /** 490 * Adds a mapping between a string of text and a sound resource in a 491 * package. After a call to this method, subsequent calls to 492 * {@link #speak(String, int, HashMap)} will play the specified sound resource 493 * if it is available, or synthesize the text it is missing. 494 * 495 * @param text 496 * The string of text. Example: <code>"south_south_east"</code> 497 * 498 * @param packagename 499 * Pass the packagename of the application that contains the 500 * resource. If the resource is in your own application (this is 501 * the most common case), then put the packagename of your 502 * application here.<br/> 503 * Example: <b>"com.google.marvin.compass"</b><br/> 504 * The packagename can be found in the AndroidManifest.xml of 505 * your application. 506 * <p> 507 * <code><manifest xmlns:android="..." 508 * package="<b>com.google.marvin.compass</b>"></code> 509 * </p> 510 * 511 * @param resourceId 512 * Example: <code>R.raw.south_south_east</code> 513 * 514 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. 515 */ 516 public int addSpeech(String text, String packagename, int resourceId) { 517 synchronized(mStartLock) { 518 if (!mStarted) { 519 return ERROR; 520 } 521 try { 522 mITts.addSpeech(mPackageName, text, packagename, resourceId); 523 return SUCCESS; 524 } catch (RemoteException e) { 525 // TTS died; restart it. 526 Log.e("TextToSpeech.java - addSpeech", "RemoteException"); 527 e.printStackTrace(); 528 mStarted = false; 529 initTts(); 530 } catch (NullPointerException e) { 531 // TTS died; restart it. 532 Log.e("TextToSpeech.java - addSpeech", "NullPointerException"); 533 e.printStackTrace(); 534 mStarted = false; 535 initTts(); 536 } catch (IllegalStateException e) { 537 // TTS died; restart it. 538 Log.e("TextToSpeech.java - addSpeech", "IllegalStateException"); 539 e.printStackTrace(); 540 mStarted = false; 541 initTts(); 542 } 543 return ERROR; 544 } 545 } 546 547 548 /** 549 * Adds a mapping between a string of text and a sound file. Using this, it 550 * is possible to add custom pronounciations for a string of text. 551 * After a call to this method, subsequent calls to {@link #speak(String, int, HashMap)} 552 * will play the specified sound resource if it is available, or synthesize the text it is 553 * missing. 554 * 555 * @param text 556 * The string of text. Example: <code>"south_south_east"</code> 557 * @param filename 558 * The full path to the sound file (for example: 559 * "/sdcard/mysounds/hello.wav") 560 * 561 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. 562 */ 563 public int addSpeech(String text, String filename) { 564 synchronized (mStartLock) { 565 if (!mStarted) { 566 return ERROR; 567 } 568 try { 569 mITts.addSpeechFile(mPackageName, text, filename); 570 return SUCCESS; 571 } catch (RemoteException e) { 572 // TTS died; restart it. 573 Log.e("TextToSpeech.java - addSpeech", "RemoteException"); 574 e.printStackTrace(); 575 mStarted = false; 576 initTts(); 577 } catch (NullPointerException e) { 578 // TTS died; restart it. 579 Log.e("TextToSpeech.java - addSpeech", "NullPointerException"); 580 e.printStackTrace(); 581 mStarted = false; 582 initTts(); 583 } catch (IllegalStateException e) { 584 // TTS died; restart it. 585 Log.e("TextToSpeech.java - addSpeech", "IllegalStateException"); 586 e.printStackTrace(); 587 mStarted = false; 588 initTts(); 589 } 590 return ERROR; 591 } 592 } 593 594 595 /** 596 * Adds a mapping between a string of text and a sound resource in a 597 * package. Use this to add custom earcons. 598 * 599 * @see #playEarcon(String, int, HashMap) 600 * 601 * @param earcon The name of the earcon. 602 * Example: <code>"[tick]"</code><br/> 603 * 604 * @param packagename 605 * the package name of the application that contains the 606 * resource. This can for instance be the package name of your own application. 607 * Example: <b>"com.google.marvin.compass"</b><br/> 608 * The package name can be found in the AndroidManifest.xml of 609 * the application containing the resource. 610 * <p> 611 * <code><manifest xmlns:android="..." 612 * package="<b>com.google.marvin.compass</b>"></code> 613 * </p> 614 * 615 * @param resourceId 616 * Example: <code>R.raw.tick_snd</code> 617 * 618 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. 619 */ 620 public int addEarcon(String earcon, String packagename, int resourceId) { 621 synchronized(mStartLock) { 622 if (!mStarted) { 623 return ERROR; 624 } 625 try { 626 mITts.addEarcon(mPackageName, earcon, packagename, resourceId); 627 return SUCCESS; 628 } catch (RemoteException e) { 629 // TTS died; restart it. 630 Log.e("TextToSpeech.java - addEarcon", "RemoteException"); 631 e.printStackTrace(); 632 mStarted = false; 633 initTts(); 634 } catch (NullPointerException e) { 635 // TTS died; restart it. 636 Log.e("TextToSpeech.java - addEarcon", "NullPointerException"); 637 e.printStackTrace(); 638 mStarted = false; 639 initTts(); 640 } catch (IllegalStateException e) { 641 // TTS died; restart it. 642 Log.e("TextToSpeech.java - addEarcon", "IllegalStateException"); 643 e.printStackTrace(); 644 mStarted = false; 645 initTts(); 646 } 647 return ERROR; 648 } 649 } 650 651 652 /** 653 * Adds a mapping between a string of text and a sound file. 654 * Use this to add custom earcons. 655 * 656 * @see #playEarcon(String, int, HashMap) 657 * 658 * @param earcon 659 * The name of the earcon. 660 * Example: <code>"[tick]"</code> 661 * @param filename 662 * The full path to the sound file (for example: 663 * "/sdcard/mysounds/tick.wav") 664 * 665 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. 666 */ 667 public int addEarcon(String earcon, String filename) { 668 synchronized (mStartLock) { 669 if (!mStarted) { 670 return ERROR; 671 } 672 try { 673 mITts.addEarconFile(mPackageName, earcon, filename); 674 return SUCCESS; 675 } catch (RemoteException e) { 676 // TTS died; restart it. 677 Log.e("TextToSpeech.java - addEarcon", "RemoteException"); 678 e.printStackTrace(); 679 mStarted = false; 680 initTts(); 681 } catch (NullPointerException e) { 682 // TTS died; restart it. 683 Log.e("TextToSpeech.java - addEarcon", "NullPointerException"); 684 e.printStackTrace(); 685 mStarted = false; 686 initTts(); 687 } catch (IllegalStateException e) { 688 // TTS died; restart it. 689 Log.e("TextToSpeech.java - addEarcon", "IllegalStateException"); 690 e.printStackTrace(); 691 mStarted = false; 692 initTts(); 693 } 694 return ERROR; 695 } 696 } 697 698 699 /** 700 * Speaks the string using the specified queuing strategy and speech 701 * parameters. 702 * 703 * @param text 704 * The string of text to be spoken. 705 * @param queueMode 706 * The queuing strategy to use. 707 * {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}. 708 * @param params 709 * The list of parameters to be used. Can be null if no parameters are given. 710 * They are specified using a (key, value) pair, where the key can be 711 * {@link Engine#KEY_PARAM_STREAM} or 712 * {@link Engine#KEY_PARAM_UTTERANCE_ID}. 713 * 714 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. 715 */ 716 public int speak(String text, int queueMode, HashMap<String,String> params) 717 { 718 synchronized (mStartLock) { 719 int result = ERROR; 720 Log.i("TTS received: ", text); 721 if (!mStarted) { 722 return result; 723 } 724 try { 725 if ((params != null) && (!params.isEmpty())) { 726 String extra = params.get(Engine.KEY_PARAM_STREAM); 727 if (extra != null) { 728 mCachedParams[Engine.PARAM_POSITION_STREAM + 1] = extra; 729 } 730 extra = params.get(Engine.KEY_PARAM_UTTERANCE_ID); 731 if (extra != null) { 732 mCachedParams[Engine.PARAM_POSITION_UTTERANCE_ID + 1] = extra; 733 } 734 extra = params.get(Engine.KEY_PARAM_ENGINE); 735 if (extra != null) { 736 mCachedParams[Engine.PARAM_POSITION_ENGINE + 1] = extra; 737 } 738 } 739 result = mITts.speak(mPackageName, text, queueMode, mCachedParams); 740 } catch (RemoteException e) { 741 // TTS died; restart it. 742 Log.e("TextToSpeech.java - speak", "RemoteException"); 743 e.printStackTrace(); 744 mStarted = false; 745 initTts(); 746 } catch (NullPointerException e) { 747 // TTS died; restart it. 748 Log.e("TextToSpeech.java - speak", "NullPointerException"); 749 e.printStackTrace(); 750 mStarted = false; 751 initTts(); 752 } catch (IllegalStateException e) { 753 // TTS died; restart it. 754 Log.e("TextToSpeech.java - speak", "IllegalStateException"); 755 e.printStackTrace(); 756 mStarted = false; 757 initTts(); 758 } finally { 759 resetCachedParams(); 760 return result; 761 } 762 } 763 } 764 765 766 /** 767 * Plays the earcon using the specified queueing mode and parameters. 768 * 769 * @param earcon 770 * The earcon that should be played 771 * @param queueMode 772 * {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}. 773 * @param params 774 * The list of parameters to be used. Can be null if no parameters are given. 775 * They are specified using a (key, value) pair, where the key can be 776 * {@link Engine#KEY_PARAM_STREAM} or 777 * {@link Engine#KEY_PARAM_UTTERANCE_ID}. 778 * 779 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. 780 */ 781 public int playEarcon(String earcon, int queueMode, 782 HashMap<String,String> params) { 783 synchronized (mStartLock) { 784 int result = ERROR; 785 if (!mStarted) { 786 return result; 787 } 788 try { 789 if ((params != null) && (!params.isEmpty())) { 790 String extra = params.get(Engine.KEY_PARAM_STREAM); 791 if (extra != null) { 792 mCachedParams[Engine.PARAM_POSITION_STREAM + 1] = extra; 793 } 794 extra = params.get(Engine.KEY_PARAM_UTTERANCE_ID); 795 if (extra != null) { 796 mCachedParams[Engine.PARAM_POSITION_UTTERANCE_ID + 1] = extra; 797 } 798 } 799 result = mITts.playEarcon(mPackageName, earcon, queueMode, null); 800 } catch (RemoteException e) { 801 // TTS died; restart it. 802 Log.e("TextToSpeech.java - playEarcon", "RemoteException"); 803 e.printStackTrace(); 804 mStarted = false; 805 initTts(); 806 } catch (NullPointerException e) { 807 // TTS died; restart it. 808 Log.e("TextToSpeech.java - playEarcon", "NullPointerException"); 809 e.printStackTrace(); 810 mStarted = false; 811 initTts(); 812 } catch (IllegalStateException e) { 813 // TTS died; restart it. 814 Log.e("TextToSpeech.java - playEarcon", "IllegalStateException"); 815 e.printStackTrace(); 816 mStarted = false; 817 initTts(); 818 } finally { 819 resetCachedParams(); 820 return result; 821 } 822 } 823 } 824 825 /** 826 * Plays silence for the specified amount of time using the specified 827 * queue mode. 828 * 829 * @param durationInMs 830 * A long that indicates how long the silence should last. 831 * @param queueMode 832 * {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}. 833 * @param params 834 * The list of parameters to be used. Can be null if no parameters are given. 835 * They are specified using a (key, value) pair, where the key can be 836 * {@link Engine#KEY_PARAM_UTTERANCE_ID}. 837 * 838 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. 839 */ 840 public int playSilence(long durationInMs, int queueMode, HashMap<String,String> params) { 841 synchronized (mStartLock) { 842 int result = ERROR; 843 if (!mStarted) { 844 return result; 845 } 846 try { 847 if ((params != null) && (!params.isEmpty())) { 848 String extra = params.get(Engine.KEY_PARAM_UTTERANCE_ID); 849 if (extra != null) { 850 mCachedParams[Engine.PARAM_POSITION_UTTERANCE_ID + 1] = extra; 851 } 852 } 853 result = mITts.playSilence(mPackageName, durationInMs, queueMode, mCachedParams); 854 } catch (RemoteException e) { 855 // TTS died; restart it. 856 Log.e("TextToSpeech.java - playSilence", "RemoteException"); 857 e.printStackTrace(); 858 mStarted = false; 859 initTts(); 860 } catch (NullPointerException e) { 861 // TTS died; restart it. 862 Log.e("TextToSpeech.java - playSilence", "NullPointerException"); 863 e.printStackTrace(); 864 mStarted = false; 865 initTts(); 866 } catch (IllegalStateException e) { 867 // TTS died; restart it. 868 Log.e("TextToSpeech.java - playSilence", "IllegalStateException"); 869 e.printStackTrace(); 870 mStarted = false; 871 initTts(); 872 } finally { 873 return result; 874 } 875 } 876 } 877 878 879 /** 880 * Returns whether or not the TextToSpeech engine is busy speaking. 881 * 882 * @return Whether or not the TextToSpeech engine is busy speaking. 883 */ 884 public boolean isSpeaking() { 885 synchronized (mStartLock) { 886 if (!mStarted) { 887 return false; 888 } 889 try { 890 return mITts.isSpeaking(); 891 } catch (RemoteException e) { 892 // TTS died; restart it. 893 Log.e("TextToSpeech.java - isSpeaking", "RemoteException"); 894 e.printStackTrace(); 895 mStarted = false; 896 initTts(); 897 } catch (NullPointerException e) { 898 // TTS died; restart it. 899 Log.e("TextToSpeech.java - isSpeaking", "NullPointerException"); 900 e.printStackTrace(); 901 mStarted = false; 902 initTts(); 903 } catch (IllegalStateException e) { 904 // TTS died; restart it. 905 Log.e("TextToSpeech.java - isSpeaking", "IllegalStateException"); 906 e.printStackTrace(); 907 mStarted = false; 908 initTts(); 909 } 910 return false; 911 } 912 } 913 914 915 /** 916 * Interrupts the current utterance (whether played or rendered to file) and discards other 917 * utterances in the queue. 918 * 919 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. 920 */ 921 public int stop() { 922 synchronized (mStartLock) { 923 int result = ERROR; 924 if (!mStarted) { 925 return result; 926 } 927 try { 928 result = mITts.stop(mPackageName); 929 } catch (RemoteException e) { 930 // TTS died; restart it. 931 Log.e("TextToSpeech.java - stop", "RemoteException"); 932 e.printStackTrace(); 933 mStarted = false; 934 initTts(); 935 } catch (NullPointerException e) { 936 // TTS died; restart it. 937 Log.e("TextToSpeech.java - stop", "NullPointerException"); 938 e.printStackTrace(); 939 mStarted = false; 940 initTts(); 941 } catch (IllegalStateException e) { 942 // TTS died; restart it. 943 Log.e("TextToSpeech.java - stop", "IllegalStateException"); 944 e.printStackTrace(); 945 mStarted = false; 946 initTts(); 947 } finally { 948 return result; 949 } 950 } 951 } 952 953 954 /** 955 * Sets the speech rate for the TextToSpeech engine. 956 * 957 * This has no effect on any pre-recorded speech. 958 * 959 * @param speechRate 960 * The speech rate for the TextToSpeech engine. 1 is the normal speed, 961 * lower values slow down the speech (0.5 is half the normal speech rate), 962 * greater values accelerate it (2 is twice the normal speech rate). 963 * 964 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. 965 */ 966 public int setSpeechRate(float speechRate) { 967 synchronized (mStartLock) { 968 int result = ERROR; 969 if (!mStarted) { 970 return result; 971 } 972 try { 973 if (speechRate > 0) { 974 int rate = (int)(speechRate*100); 975 mCachedParams[Engine.PARAM_POSITION_RATE + 1] = String.valueOf(rate); 976 // the rate is not set here, instead it is cached so it will be associated 977 // with all upcoming utterances. 978 if (speechRate > 0.0f) { 979 result = SUCCESS; 980 } else { 981 result = ERROR; 982 } 983 } 984 } catch (NullPointerException e) { 985 // TTS died; restart it. 986 Log.e("TextToSpeech.java - setSpeechRate", "NullPointerException"); 987 e.printStackTrace(); 988 mStarted = false; 989 initTts(); 990 } catch (IllegalStateException e) { 991 // TTS died; restart it. 992 Log.e("TextToSpeech.java - setSpeechRate", "IllegalStateException"); 993 e.printStackTrace(); 994 mStarted = false; 995 initTts(); 996 } finally { 997 return result; 998 } 999 } 1000 } 1001 1002 1003 /** 1004 * Sets the speech pitch for the TextToSpeech engine. 1005 * 1006 * This has no effect on any pre-recorded speech. 1007 * 1008 * @param pitch 1009 * The pitch for the TextToSpeech engine. 1 is the normal pitch, 1010 * lower values lower the tone of the synthesized voice, 1011 * greater values increase it. 1012 * 1013 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. 1014 */ 1015 public int setPitch(float pitch) { 1016 synchronized (mStartLock) { 1017 int result = ERROR; 1018 if (!mStarted) { 1019 return result; 1020 } 1021 try { 1022 // the pitch is not set here, instead it is cached so it will be associated 1023 // with all upcoming utterances. 1024 if (pitch > 0) { 1025 int p = (int)(pitch*100); 1026 mCachedParams[Engine.PARAM_POSITION_PITCH + 1] = String.valueOf(p); 1027 result = SUCCESS; 1028 } 1029 } catch (NullPointerException e) { 1030 // TTS died; restart it. 1031 Log.e("TextToSpeech.java - setPitch", "NullPointerException"); 1032 e.printStackTrace(); 1033 mStarted = false; 1034 initTts(); 1035 } catch (IllegalStateException e) { 1036 // TTS died; restart it. 1037 Log.e("TextToSpeech.java - setPitch", "IllegalStateException"); 1038 e.printStackTrace(); 1039 mStarted = false; 1040 initTts(); 1041 } finally { 1042 return result; 1043 } 1044 } 1045 } 1046 1047 1048 /** 1049 * Sets the language for the TextToSpeech engine. 1050 * The TextToSpeech engine will try to use the closest match to the specified 1051 * language as represented by the Locale, but there is no guarantee that the exact same Locale 1052 * will be used. Use {@link #isLanguageAvailable(Locale)} to check the level of support 1053 * before choosing the language to use for the next utterances. 1054 * 1055 * @param loc 1056 * The locale describing the language to be used. 1057 * 1058 * @return code indicating the support status for the locale. See {@link #LANG_AVAILABLE}, 1059 * {@link #LANG_COUNTRY_AVAILABLE}, {@link #LANG_COUNTRY_VAR_AVAILABLE}, 1060 * {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}. 1061 */ 1062 public int setLanguage(Locale loc) { 1063 synchronized (mStartLock) { 1064 int result = LANG_NOT_SUPPORTED; 1065 if (!mStarted) { 1066 return result; 1067 } 1068 if (loc == null) { 1069 return result; 1070 } 1071 try { 1072 String language = loc.getISO3Language(); 1073 String country = loc.getISO3Country(); 1074 String variant = loc.getVariant(); 1075 // Check if the language, country, variant are available, and cache 1076 // the available parts. 1077 // Note that the language is not actually set here, instead it is cached so it 1078 // will be associated with all upcoming utterances. 1079 result = mITts.isLanguageAvailable(language, country, variant, mCachedParams); 1080 if (result >= LANG_AVAILABLE){ 1081 mCachedParams[Engine.PARAM_POSITION_LANGUAGE + 1] = language; 1082 if (result >= LANG_COUNTRY_AVAILABLE){ 1083 mCachedParams[Engine.PARAM_POSITION_COUNTRY + 1] = country; 1084 } else { 1085 mCachedParams[Engine.PARAM_POSITION_COUNTRY + 1] = ""; 1086 } 1087 if (result >= LANG_COUNTRY_VAR_AVAILABLE){ 1088 mCachedParams[Engine.PARAM_POSITION_VARIANT + 1] = variant; 1089 } else { 1090 mCachedParams[Engine.PARAM_POSITION_VARIANT + 1] = ""; 1091 } 1092 } 1093 } catch (RemoteException e) { 1094 // TTS died; restart it. 1095 Log.e("TextToSpeech.java - setLanguage", "RemoteException"); 1096 e.printStackTrace(); 1097 mStarted = false; 1098 initTts(); 1099 } catch (NullPointerException e) { 1100 // TTS died; restart it. 1101 Log.e("TextToSpeech.java - setLanguage", "NullPointerException"); 1102 e.printStackTrace(); 1103 mStarted = false; 1104 initTts(); 1105 } catch (IllegalStateException e) { 1106 // TTS died; restart it. 1107 Log.e("TextToSpeech.java - setLanguage", "IllegalStateException"); 1108 e.printStackTrace(); 1109 mStarted = false; 1110 initTts(); 1111 } finally { 1112 return result; 1113 } 1114 } 1115 } 1116 1117 1118 /** 1119 * Returns a Locale instance describing the language currently being used by the TextToSpeech 1120 * engine. 1121 * @return language, country (if any) and variant (if any) used by the engine stored in a Locale 1122 * instance, or null is the TextToSpeech engine has failed. 1123 */ 1124 public Locale getLanguage() { 1125 synchronized (mStartLock) { 1126 if (!mStarted) { 1127 return null; 1128 } 1129 try { 1130 // Only do a call to the native synth if there is nothing in the cached params 1131 if (mCachedParams[Engine.PARAM_POSITION_LANGUAGE + 1].length() < 1){ 1132 String[] locStrings = mITts.getLanguage(); 1133 if ((locStrings != null) && (locStrings.length == 3)) { 1134 return new Locale(locStrings[0], locStrings[1], locStrings[2]); 1135 } else { 1136 return null; 1137 } 1138 } else { 1139 return new Locale(mCachedParams[Engine.PARAM_POSITION_LANGUAGE + 1], 1140 mCachedParams[Engine.PARAM_POSITION_COUNTRY + 1], 1141 mCachedParams[Engine.PARAM_POSITION_VARIANT + 1]); 1142 } 1143 } catch (RemoteException e) { 1144 // TTS died; restart it. 1145 Log.e("TextToSpeech.java - getLanguage", "RemoteException"); 1146 e.printStackTrace(); 1147 mStarted = false; 1148 initTts(); 1149 } catch (NullPointerException e) { 1150 // TTS died; restart it. 1151 Log.e("TextToSpeech.java - getLanguage", "NullPointerException"); 1152 e.printStackTrace(); 1153 mStarted = false; 1154 initTts(); 1155 } catch (IllegalStateException e) { 1156 // TTS died; restart it. 1157 Log.e("TextToSpeech.java - getLanguage", "IllegalStateException"); 1158 e.printStackTrace(); 1159 mStarted = false; 1160 initTts(); 1161 } 1162 return null; 1163 } 1164 } 1165 1166 /** 1167 * Checks if the specified language as represented by the Locale is available and supported. 1168 * 1169 * @param loc 1170 * The Locale describing the language to be used. 1171 * 1172 * @return code indicating the support status for the locale. See {@link #LANG_AVAILABLE}, 1173 * {@link #LANG_COUNTRY_AVAILABLE}, {@link #LANG_COUNTRY_VAR_AVAILABLE}, 1174 * {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}. 1175 */ 1176 public int isLanguageAvailable(Locale loc) { 1177 synchronized (mStartLock) { 1178 int result = LANG_NOT_SUPPORTED; 1179 if (!mStarted) { 1180 return result; 1181 } 1182 try { 1183 result = mITts.isLanguageAvailable(loc.getISO3Language(), 1184 loc.getISO3Country(), loc.getVariant(), mCachedParams); 1185 } catch (RemoteException e) { 1186 // TTS died; restart it. 1187 Log.e("TextToSpeech.java - isLanguageAvailable", "RemoteException"); 1188 e.printStackTrace(); 1189 mStarted = false; 1190 initTts(); 1191 } catch (NullPointerException e) { 1192 // TTS died; restart it. 1193 Log.e("TextToSpeech.java - isLanguageAvailable", "NullPointerException"); 1194 e.printStackTrace(); 1195 mStarted = false; 1196 initTts(); 1197 } catch (IllegalStateException e) { 1198 // TTS died; restart it. 1199 Log.e("TextToSpeech.java - isLanguageAvailable", "IllegalStateException"); 1200 e.printStackTrace(); 1201 mStarted = false; 1202 initTts(); 1203 } finally { 1204 return result; 1205 } 1206 } 1207 } 1208 1209 1210 /** 1211 * Synthesizes the given text to a file using the specified parameters. 1212 * 1213 * @param text 1214 * The String of text that should be synthesized 1215 * @param params 1216 * The list of parameters to be used. Can be null if no parameters are given. 1217 * They are specified using a (key, value) pair, where the key can be 1218 * {@link Engine#KEY_PARAM_UTTERANCE_ID}. 1219 * @param filename 1220 * The string that gives the full output filename; it should be 1221 * something like "/sdcard/myappsounds/mysound.wav". 1222 * 1223 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. 1224 */ 1225 public int synthesizeToFile(String text, HashMap<String,String> params, 1226 String filename) { 1227 synchronized (mStartLock) { 1228 int result = ERROR; 1229 if (!mStarted) { 1230 return result; 1231 } 1232 try { 1233 if ((params != null) && (!params.isEmpty())) { 1234 // no need to read the stream type here 1235 String extra = params.get(Engine.KEY_PARAM_UTTERANCE_ID); 1236 if (extra != null) { 1237 mCachedParams[Engine.PARAM_POSITION_UTTERANCE_ID + 1] = extra; 1238 } 1239 extra = params.get(Engine.KEY_PARAM_ENGINE); 1240 if (extra != null) { 1241 mCachedParams[Engine.PARAM_POSITION_ENGINE + 1] = extra; 1242 } 1243 } 1244 result = mITts.synthesizeToFile(mPackageName, text, mCachedParams, filename) ? 1245 SUCCESS : ERROR; 1246 } catch (RemoteException e) { 1247 // TTS died; restart it. 1248 Log.e("TextToSpeech.java - synthesizeToFile", "RemoteException"); 1249 e.printStackTrace(); 1250 mStarted = false; 1251 initTts(); 1252 } catch (NullPointerException e) { 1253 // TTS died; restart it. 1254 Log.e("TextToSpeech.java - synthesizeToFile", "NullPointerException"); 1255 e.printStackTrace(); 1256 mStarted = false; 1257 initTts(); 1258 } catch (IllegalStateException e) { 1259 // TTS died; restart it. 1260 Log.e("TextToSpeech.java - synthesizeToFile", "IllegalStateException"); 1261 e.printStackTrace(); 1262 mStarted = false; 1263 initTts(); 1264 } finally { 1265 resetCachedParams(); 1266 return result; 1267 } 1268 } 1269 } 1270 1271 1272 /** 1273 * Convenience method to reset the cached parameters to the current default values 1274 * if they are not persistent between calls to the service. 1275 */ 1276 private void resetCachedParams() { 1277 mCachedParams[Engine.PARAM_POSITION_STREAM + 1] = 1278 String.valueOf(Engine.DEFAULT_STREAM); 1279 mCachedParams[Engine.PARAM_POSITION_UTTERANCE_ID+ 1] = ""; 1280 } 1281 1282 /** 1283 * Sets the OnUtteranceCompletedListener that will fire when an utterance completes. 1284 * 1285 * @param listener 1286 * The OnUtteranceCompletedListener 1287 * 1288 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. 1289 */ 1290 public int setOnUtteranceCompletedListener( 1291 final OnUtteranceCompletedListener listener) { 1292 synchronized (mStartLock) { 1293 int result = ERROR; 1294 if (!mStarted) { 1295 return result; 1296 } 1297 mITtscallback = new ITtsCallback.Stub() { 1298 public void utteranceCompleted(String utteranceId) throws RemoteException { 1299 if (listener != null) { 1300 listener.onUtteranceCompleted(utteranceId); 1301 } 1302 } 1303 }; 1304 try { 1305 result = mITts.registerCallback(mPackageName, mITtscallback); 1306 } catch (RemoteException e) { 1307 // TTS died; restart it. 1308 Log.e("TextToSpeech.java - registerCallback", "RemoteException"); 1309 e.printStackTrace(); 1310 mStarted = false; 1311 initTts(); 1312 } catch (NullPointerException e) { 1313 // TTS died; restart it. 1314 Log.e("TextToSpeech.java - registerCallback", "NullPointerException"); 1315 e.printStackTrace(); 1316 mStarted = false; 1317 initTts(); 1318 } catch (IllegalStateException e) { 1319 // TTS died; restart it. 1320 Log.e("TextToSpeech.java - registerCallback", "IllegalStateException"); 1321 e.printStackTrace(); 1322 mStarted = false; 1323 initTts(); 1324 } finally { 1325 return result; 1326 } 1327 } 1328 } 1329 1330 /** 1331 * Sets the speech synthesis engine to be used by its packagename. 1332 * 1333 * @param enginePackageName 1334 * The packagename for the synthesis engine (ie, "com.svox.pico") 1335 * 1336 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. 1337 */ 1338 public int setEngineByPackageName(String enginePackageName) { 1339 synchronized (mStartLock) { 1340 int result = TextToSpeech.ERROR; 1341 if (!mStarted) { 1342 return result; 1343 } 1344 try { 1345 result = mITts.setEngineByPackageName(enginePackageName); 1346 if (result == TextToSpeech.SUCCESS){ 1347 mCachedParams[Engine.PARAM_POSITION_ENGINE + 1] = enginePackageName; 1348 } 1349 } catch (RemoteException e) { 1350 // TTS died; restart it. 1351 Log.e("TextToSpeech.java - setEngineByPackageName", "RemoteException"); 1352 e.printStackTrace(); 1353 mStarted = false; 1354 initTts(); 1355 } catch (NullPointerException e) { 1356 // TTS died; restart it. 1357 Log.e("TextToSpeech.java - setEngineByPackageName", "NullPointerException"); 1358 e.printStackTrace(); 1359 mStarted = false; 1360 initTts(); 1361 } catch (IllegalStateException e) { 1362 // TTS died; restart it. 1363 Log.e("TextToSpeech.java - setEngineByPackageName", "IllegalStateException"); 1364 e.printStackTrace(); 1365 mStarted = false; 1366 initTts(); 1367 } finally { 1368 return result; 1369 } 1370 } 1371 } 1372 1373 1374 /** 1375 * Gets the packagename of the default speech synthesis engine. 1376 * 1377 * @return Packagename of the TTS engine that the user has chosen as their default. 1378 */ 1379 public String getDefaultEngine() { 1380 synchronized (mStartLock) { 1381 String engineName = ""; 1382 if (!mStarted) { 1383 return engineName; 1384 } 1385 try { 1386 engineName = mITts.getDefaultEngine(); 1387 } catch (RemoteException e) { 1388 // TTS died; restart it. 1389 Log.e("TextToSpeech.java - setEngineByPackageName", "RemoteException"); 1390 e.printStackTrace(); 1391 mStarted = false; 1392 initTts(); 1393 } catch (NullPointerException e) { 1394 // TTS died; restart it. 1395 Log.e("TextToSpeech.java - setEngineByPackageName", "NullPointerException"); 1396 e.printStackTrace(); 1397 mStarted = false; 1398 initTts(); 1399 } catch (IllegalStateException e) { 1400 // TTS died; restart it. 1401 Log.e("TextToSpeech.java - setEngineByPackageName", "IllegalStateException"); 1402 e.printStackTrace(); 1403 mStarted = false; 1404 initTts(); 1405 } finally { 1406 return engineName; 1407 } 1408 } 1409 } 1410 1411 1412 /** 1413 * Returns whether or not the user is forcing their defaults to override the 1414 * Text-To-Speech settings set by applications. 1415 * 1416 * @return Whether or not defaults are enforced. 1417 */ 1418 public boolean areDefaultsEnforced() { 1419 synchronized (mStartLock) { 1420 boolean defaultsEnforced = false; 1421 if (!mStarted) { 1422 return defaultsEnforced; 1423 } 1424 try { 1425 defaultsEnforced = mITts.areDefaultsEnforced(); 1426 } catch (RemoteException e) { 1427 // TTS died; restart it. 1428 Log.e("TextToSpeech.java - areDefaultsEnforced", "RemoteException"); 1429 e.printStackTrace(); 1430 mStarted = false; 1431 initTts(); 1432 } catch (NullPointerException e) { 1433 // TTS died; restart it. 1434 Log.e("TextToSpeech.java - areDefaultsEnforced", "NullPointerException"); 1435 e.printStackTrace(); 1436 mStarted = false; 1437 initTts(); 1438 } catch (IllegalStateException e) { 1439 // TTS died; restart it. 1440 Log.e("TextToSpeech.java - areDefaultsEnforced", "IllegalStateException"); 1441 e.printStackTrace(); 1442 mStarted = false; 1443 initTts(); 1444 } finally { 1445 return defaultsEnforced; 1446 } 1447 } 1448 } 1449} 1450