TextToSpeech.java revision a1788af3440fef04d16067223a31e9dced3500b9
1/* 2 * Copyright (C) 2009 The Android Open Source Project 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.annotation.SdkConstant; 19import android.annotation.SdkConstant.SdkConstantType; 20import android.content.ComponentName; 21import android.content.ContentResolver; 22import android.content.Context; 23import android.content.Intent; 24import android.content.ServiceConnection; 25import android.media.AudioManager; 26import android.net.Uri; 27import android.os.AsyncTask; 28import android.os.Bundle; 29import android.os.IBinder; 30import android.os.ParcelFileDescriptor; 31import android.os.RemoteException; 32import android.provider.Settings; 33import android.text.TextUtils; 34import android.util.Log; 35 36import java.io.File; 37import java.io.FileNotFoundException; 38import java.io.IOException; 39import java.util.Collections; 40import java.util.HashMap; 41import java.util.HashSet; 42import java.util.List; 43import java.util.Locale; 44import java.util.Map; 45import java.util.MissingResourceException; 46import java.util.Set; 47 48/** 49 * 50 * Synthesizes speech from text for immediate playback or to create a sound file. 51 * <p>A TextToSpeech instance can only be used to synthesize text once it has completed its 52 * initialization. Implement the {@link TextToSpeech.OnInitListener} to be 53 * notified of the completion of the initialization.<br> 54 * When you are done using the TextToSpeech instance, call the {@link #shutdown()} method 55 * to release the native resources used by the TextToSpeech engine. 56 * 57 * @deprecated Use {@link TextToSpeechClient} instead 58 */ 59@Deprecated 60public class TextToSpeech { 61 62 private static final String TAG = "TextToSpeech"; 63 64 /** 65 * Denotes a successful operation. 66 */ 67 public static final int SUCCESS = 0; 68 /** 69 * Denotes a generic operation failure. 70 */ 71 public static final int ERROR = -1; 72 73 /** 74 * Queue mode where all entries in the playback queue (media to be played 75 * and text to be synthesized) are dropped and replaced by the new entry. 76 * Queues are flushed with respect to a given calling app. Entries in the queue 77 * from other callees are not discarded. 78 */ 79 public static final int QUEUE_FLUSH = 0; 80 /** 81 * Queue mode where the new entry is added at the end of the playback queue. 82 */ 83 public static final int QUEUE_ADD = 1; 84 /** 85 * Queue mode where the entire playback queue is purged. This is different 86 * from {@link #QUEUE_FLUSH} in that all entries are purged, not just entries 87 * from a given caller. 88 * 89 * @hide 90 */ 91 static final int QUEUE_DESTROY = 2; 92 93 /** 94 * Denotes the language is available exactly as specified by the locale. 95 */ 96 public static final int LANG_COUNTRY_VAR_AVAILABLE = 2; 97 98 /** 99 * Denotes the language is available for the language and country specified 100 * by the locale, but not the variant. 101 */ 102 public static final int LANG_COUNTRY_AVAILABLE = 1; 103 104 /** 105 * Denotes the language is available for the language by the locale, 106 * but not the country and variant. 107 */ 108 public static final int LANG_AVAILABLE = 0; 109 110 /** 111 * Denotes the language data is missing. 112 */ 113 public static final int LANG_MISSING_DATA = -1; 114 115 /** 116 * Denotes the language is not supported. 117 */ 118 public static final int LANG_NOT_SUPPORTED = -2; 119 120 /** 121 * Broadcast Action: The TextToSpeech synthesizer has completed processing 122 * of all the text in the speech queue. 123 * 124 * Note that this notifies callers when the <b>engine</b> has finished has 125 * processing text data. Audio playback might not have completed (or even started) 126 * at this point. If you wish to be notified when this happens, see 127 * {@link OnUtteranceCompletedListener}. 128 */ 129 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 130 public static final String ACTION_TTS_QUEUE_PROCESSING_COMPLETED = 131 "android.speech.tts.TTS_QUEUE_PROCESSING_COMPLETED"; 132 133 /** 134 * Interface definition of a callback to be invoked indicating the completion of the 135 * TextToSpeech engine initialization. 136 */ 137 public interface OnInitListener { 138 /** 139 * Called to signal the completion of the TextToSpeech engine initialization. 140 * 141 * @param status {@link TextToSpeech#SUCCESS} or {@link TextToSpeech#ERROR}. 142 */ 143 public void onInit(int status); 144 } 145 146 /** 147 * Listener that will be called when the TTS service has 148 * completed synthesizing an utterance. This is only called if the utterance 149 * has an utterance ID (see {@link TextToSpeech.Engine#KEY_PARAM_UTTERANCE_ID}). 150 * 151 * @deprecated Use {@link UtteranceProgressListener} instead. 152 */ 153 @Deprecated 154 public interface OnUtteranceCompletedListener { 155 /** 156 * Called when an utterance has been synthesized. 157 * 158 * @param utteranceId the identifier of the utterance. 159 */ 160 public void onUtteranceCompleted(String utteranceId); 161 } 162 163 /** 164 * Constants and parameter names for controlling text-to-speech. These include: 165 * 166 * <ul> 167 * <li> 168 * Intents to ask engine to install data or check its data and 169 * extras for a TTS engine's check data activity. 170 * </li> 171 * <li> 172 * Keys for the parameters passed with speak commands, e.g. 173 * {@link Engine#KEY_PARAM_UTTERANCE_ID}, {@link Engine#KEY_PARAM_STREAM}. 174 * </li> 175 * <li> 176 * A list of feature strings that engines might support, e.g 177 * {@link Engine#KEY_FEATURE_NETWORK_SYNTHESIS}). These values may be passed in to 178 * {@link TextToSpeech#speak} and {@link TextToSpeech#synthesizeToFile} to modify 179 * engine behaviour. The engine can be queried for the set of features it supports 180 * through {@link TextToSpeech#getFeatures(java.util.Locale)}. 181 * </li> 182 * </ul> 183 */ 184 public class Engine { 185 186 /** 187 * Default speech rate. 188 * @hide 189 */ 190 public static final int DEFAULT_RATE = 100; 191 192 /** 193 * Default pitch. 194 * @hide 195 */ 196 public static final int DEFAULT_PITCH = 100; 197 198 /** 199 * Default volume. 200 * @hide 201 */ 202 public static final float DEFAULT_VOLUME = 1.0f; 203 204 /** 205 * Default pan (centered). 206 * @hide 207 */ 208 public static final float DEFAULT_PAN = 0.0f; 209 210 /** 211 * Default value for {@link Settings.Secure#TTS_USE_DEFAULTS}. 212 * @hide 213 */ 214 public static final int USE_DEFAULTS = 0; // false 215 216 /** 217 * Package name of the default TTS engine. 218 * 219 * @hide 220 * @deprecated No longer in use, the default engine is determined by 221 * the sort order defined in {@link TtsEngines}. Note that 222 * this doesn't "break" anything because there is no guarantee that 223 * the engine specified below is installed on a given build, let 224 * alone be the default. 225 */ 226 @Deprecated 227 public static final String DEFAULT_ENGINE = "com.svox.pico"; 228 229 /** 230 * Default audio stream used when playing synthesized speech. 231 */ 232 public static final int DEFAULT_STREAM = AudioManager.STREAM_MUSIC; 233 234 /** 235 * Indicates success when checking the installation status of the resources used by the 236 * TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent. 237 */ 238 public static final int CHECK_VOICE_DATA_PASS = 1; 239 240 /** 241 * Indicates failure when checking the installation status of the resources used by the 242 * TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent. 243 */ 244 public static final int CHECK_VOICE_DATA_FAIL = 0; 245 246 /** 247 * Indicates erroneous data when checking the installation status of the resources used by 248 * the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent. 249 * 250 * @deprecated Use CHECK_VOICE_DATA_FAIL instead. 251 */ 252 @Deprecated 253 public static final int CHECK_VOICE_DATA_BAD_DATA = -1; 254 255 /** 256 * Indicates missing resources when checking the installation status of the resources used 257 * by the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent. 258 * 259 * @deprecated Use CHECK_VOICE_DATA_FAIL instead. 260 */ 261 @Deprecated 262 public static final int CHECK_VOICE_DATA_MISSING_DATA = -2; 263 264 /** 265 * Indicates missing storage volume when checking the installation status of the resources 266 * used by the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent. 267 * 268 * @deprecated Use CHECK_VOICE_DATA_FAIL instead. 269 */ 270 @Deprecated 271 public static final int CHECK_VOICE_DATA_MISSING_VOLUME = -3; 272 273 /** 274 * Intent for starting a TTS service. Services that handle this intent must 275 * extend {@link TextToSpeechService}. Normal applications should not use this intent 276 * directly, instead they should talk to the TTS service using the the methods in this 277 * class. 278 */ 279 @SdkConstant(SdkConstantType.SERVICE_ACTION) 280 public static final String INTENT_ACTION_TTS_SERVICE = 281 "android.intent.action.TTS_SERVICE"; 282 283 /** 284 * Name under which a text to speech engine publishes information about itself. 285 * This meta-data should reference an XML resource containing a 286 * <code><{@link android.R.styleable#TextToSpeechEngine tts-engine}></code> 287 * tag. 288 */ 289 public static final String SERVICE_META_DATA = "android.speech.tts"; 290 291 // intents to ask engine to install data or check its data 292 /** 293 * Activity Action: Triggers the platform TextToSpeech engine to 294 * start the activity that installs the resource files on the device 295 * that are required for TTS to be operational. Since the installation 296 * of the data can be interrupted or declined by the user, the application 297 * shouldn't expect successful installation upon return from that intent, 298 * and if need be, should check installation status with 299 * {@link #ACTION_CHECK_TTS_DATA}. 300 */ 301 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) 302 public static final String ACTION_INSTALL_TTS_DATA = 303 "android.speech.tts.engine.INSTALL_TTS_DATA"; 304 305 /** 306 * Broadcast Action: broadcast to signal the change in the list of available 307 * languages or/and their features. 308 */ 309 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 310 public static final String ACTION_TTS_DATA_INSTALLED = 311 "android.speech.tts.engine.TTS_DATA_INSTALLED"; 312 313 /** 314 * Activity Action: Starts the activity from the platform TextToSpeech 315 * engine to verify the proper installation and availability of the 316 * resource files on the system. Upon completion, the activity will 317 * return one of the following codes: 318 * {@link #CHECK_VOICE_DATA_PASS}, 319 * {@link #CHECK_VOICE_DATA_FAIL}, 320 * <p> Moreover, the data received in the activity result will contain the following 321 * fields: 322 * <ul> 323 * <li>{@link #EXTRA_AVAILABLE_VOICES} which contains an ArrayList<String> of all the 324 * available voices. The format of each voice is: lang-COUNTRY-variant where COUNTRY and 325 * variant are optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").</li> 326 * <li>{@link #EXTRA_UNAVAILABLE_VOICES} which contains an ArrayList<String> of all the 327 * unavailable voices (ones that user can install). The format of each voice is: 328 * lang-COUNTRY-variant where COUNTRY and variant are optional (ie, "eng" or 329 * "eng-USA" or "eng-USA-FEMALE").</li> 330 * </ul> 331 */ 332 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) 333 public static final String ACTION_CHECK_TTS_DATA = 334 "android.speech.tts.engine.CHECK_TTS_DATA"; 335 336 /** 337 * Activity intent for getting some sample text to use for demonstrating TTS. Specific 338 * locale have to be requested by passing following extra parameters: 339 * <ul> 340 * <li>language</li> 341 * <li>country</li> 342 * <li>variant</li> 343 * </ul> 344 * 345 * Upon completion, the activity result may contain the following fields: 346 * <ul> 347 * <li>{@link #EXTRA_SAMPLE_TEXT} which contains an String with sample text.</li> 348 * </ul> 349 */ 350 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) 351 public static final String ACTION_GET_SAMPLE_TEXT = 352 "android.speech.tts.engine.GET_SAMPLE_TEXT"; 353 354 /** 355 * Extra information received with the {@link #ACTION_GET_SAMPLE_TEXT} intent result where 356 * the TextToSpeech engine returns an String with sample text for requested voice 357 */ 358 public static final String EXTRA_SAMPLE_TEXT = "sampleText"; 359 360 361 // extras for a TTS engine's check data activity 362 /** 363 * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent result where 364 * the TextToSpeech engine returns an ArrayList<String> of all the available voices. 365 * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are 366 * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE"). 367 */ 368 public static final String EXTRA_AVAILABLE_VOICES = "availableVoices"; 369 370 /** 371 * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent result where 372 * the TextToSpeech engine returns an ArrayList<String> of all the unavailable voices. 373 * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are 374 * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE"). 375 */ 376 public static final String EXTRA_UNAVAILABLE_VOICES = "unavailableVoices"; 377 378 /** 379 * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent result where 380 * the TextToSpeech engine specifies the path to its resources. 381 * 382 * It may be used by language packages to find out where to put their data. 383 * 384 * @deprecated TTS engine implementation detail, this information has no use for 385 * text-to-speech API client. 386 */ 387 @Deprecated 388 public static final String EXTRA_VOICE_DATA_ROOT_DIRECTORY = "dataRoot"; 389 390 /** 391 * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent result where 392 * the TextToSpeech engine specifies the file names of its resources under the 393 * resource path. 394 * 395 * @deprecated TTS engine implementation detail, this information has no use for 396 * text-to-speech API client. 397 */ 398 @Deprecated 399 public static final String EXTRA_VOICE_DATA_FILES = "dataFiles"; 400 401 /** 402 * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent result where 403 * the TextToSpeech engine specifies the locale associated with each resource file. 404 * 405 * @deprecated TTS engine implementation detail, this information has no use for 406 * text-to-speech API client. 407 */ 408 @Deprecated 409 public static final String EXTRA_VOICE_DATA_FILES_INFO = "dataFilesInfo"; 410 411 /** 412 * Extra information sent with the {@link #ACTION_CHECK_TTS_DATA} intent where the 413 * caller indicates to the TextToSpeech engine which specific sets of voice data to 414 * check for by sending an ArrayList<String> of the voices that are of interest. 415 * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are 416 * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE"). 417 * 418 * @deprecated Redundant functionality, checking for existence of specific sets of voice 419 * data can be done on client side. 420 */ 421 @Deprecated 422 public static final String EXTRA_CHECK_VOICE_DATA_FOR = "checkVoiceDataFor"; 423 424 // extras for a TTS engine's data installation 425 /** 426 * Extra information received with the {@link #ACTION_TTS_DATA_INSTALLED} intent result. 427 * It indicates whether the data files for the synthesis engine were successfully 428 * installed. The installation was initiated with the {@link #ACTION_INSTALL_TTS_DATA} 429 * intent. The possible values for this extra are 430 * {@link TextToSpeech#SUCCESS} and {@link TextToSpeech#ERROR}. 431 * 432 * @deprecated No longer in use. If client ise interested in information about what 433 * changed, is should send ACTION_CHECK_TTS_DATA intent to discover available voices. 434 */ 435 @Deprecated 436 public static final String EXTRA_TTS_DATA_INSTALLED = "dataInstalled"; 437 438 // keys for the parameters passed with speak commands. Hidden keys are used internally 439 // to maintain engine state for each TextToSpeech instance. 440 /** 441 * @hide 442 */ 443 public static final String KEY_PARAM_RATE = "rate"; 444 445 /** 446 * @hide 447 */ 448 public static final String KEY_PARAM_LANGUAGE = "language"; 449 450 /** 451 * @hide 452 */ 453 public static final String KEY_PARAM_COUNTRY = "country"; 454 455 /** 456 * @hide 457 */ 458 public static final String KEY_PARAM_VARIANT = "variant"; 459 460 /** 461 * @hide 462 */ 463 public static final String KEY_PARAM_ENGINE = "engine"; 464 465 /** 466 * @hide 467 */ 468 public static final String KEY_PARAM_PITCH = "pitch"; 469 470 /** 471 * Parameter key to specify the audio stream type to be used when speaking text 472 * or playing back a file. The value should be one of the STREAM_ constants 473 * defined in {@link AudioManager}. 474 * 475 * @see TextToSpeech#speak(String, int, HashMap) 476 * @see TextToSpeech#playEarcon(String, int, HashMap) 477 */ 478 public static final String KEY_PARAM_STREAM = "streamType"; 479 480 /** 481 * Parameter key to identify an utterance in the 482 * {@link TextToSpeech.OnUtteranceCompletedListener} after text has been 483 * spoken, a file has been played back or a silence duration has elapsed. 484 * 485 * @see TextToSpeech#speak(String, int, HashMap) 486 * @see TextToSpeech#playEarcon(String, int, HashMap) 487 * @see TextToSpeech#synthesizeToFile(String, HashMap, String) 488 */ 489 public static final String KEY_PARAM_UTTERANCE_ID = "utteranceId"; 490 491 /** 492 * Parameter key to specify the speech volume relative to the current stream type 493 * volume used when speaking text. Volume is specified as a float ranging from 0 to 1 494 * where 0 is silence, and 1 is the maximum volume (the default behavior). 495 * 496 * @see TextToSpeech#speak(String, int, HashMap) 497 * @see TextToSpeech#playEarcon(String, int, HashMap) 498 */ 499 public static final String KEY_PARAM_VOLUME = "volume"; 500 501 /** 502 * Parameter key to specify how the speech is panned from left to right when speaking text. 503 * Pan is specified as a float ranging from -1 to +1 where -1 maps to a hard-left pan, 504 * 0 to center (the default behavior), and +1 to hard-right. 505 * 506 * @see TextToSpeech#speak(String, int, HashMap) 507 * @see TextToSpeech#playEarcon(String, int, HashMap) 508 */ 509 public static final String KEY_PARAM_PAN = "pan"; 510 511 /** 512 * Feature key for network synthesis. See {@link TextToSpeech#getFeatures(Locale)} 513 * for a description of how feature keys work. If set (and supported by the engine 514 * as per {@link TextToSpeech#getFeatures(Locale)}, the engine must 515 * use network based synthesis. 516 * 517 * @see TextToSpeech#speak(String, int, java.util.HashMap) 518 * @see TextToSpeech#synthesizeToFile(String, java.util.HashMap, String) 519 * @see TextToSpeech#getFeatures(java.util.Locale) 520 */ 521 public static final String KEY_FEATURE_NETWORK_SYNTHESIS = "networkTts"; 522 523 /** 524 * Feature key for embedded synthesis. See {@link TextToSpeech#getFeatures(Locale)} 525 * for a description of how feature keys work. If set and supported by the engine 526 * as per {@link TextToSpeech#getFeatures(Locale)}, the engine must synthesize 527 * text on-device (without making network requests). 528 * 529 * @see TextToSpeech#speak(String, int, java.util.HashMap) 530 * @see TextToSpeech#synthesizeToFile(String, java.util.HashMap, String) 531 * @see TextToSpeech#getFeatures(java.util.Locale) 532 */ 533 public static final String KEY_FEATURE_EMBEDDED_SYNTHESIS = "embeddedTts"; 534 } 535 536 private final Context mContext; 537 private Connection mConnectingServiceConnection; 538 private Connection mServiceConnection; 539 private OnInitListener mInitListener; 540 // Written from an unspecified application thread, read from 541 // a binder thread. 542 private volatile UtteranceProgressListener mUtteranceProgressListener; 543 private final Object mStartLock = new Object(); 544 545 private String mRequestedEngine; 546 // Whether to initialize this TTS object with the default engine, 547 // if the requested engine is not available. Valid only if mRequestedEngine 548 // is not null. Used only for testing, though potentially useful API wise 549 // too. 550 private final boolean mUseFallback; 551 private final Map<String, Uri> mEarcons; 552 private final Map<String, Uri> mUtterances; 553 private final Bundle mParams = new Bundle(); 554 private final TtsEngines mEnginesHelper; 555 private final String mPackageName; 556 private volatile String mCurrentEngine = null; 557 558 /** 559 * The constructor for the TextToSpeech class, using the default TTS engine. 560 * This will also initialize the associated TextToSpeech engine if it isn't already running. 561 * 562 * @param context 563 * The context this instance is running in. 564 * @param listener 565 * The {@link TextToSpeech.OnInitListener} that will be called when the 566 * TextToSpeech engine has initialized. In a case of a failure the listener 567 * may be called immediately, before TextToSpeech instance is fully constructed. 568 */ 569 public TextToSpeech(Context context, OnInitListener listener) { 570 this(context, listener, null); 571 } 572 573 /** 574 * The constructor for the TextToSpeech class, using the given TTS engine. 575 * This will also initialize the associated TextToSpeech engine if it isn't already running. 576 * 577 * @param context 578 * The context this instance is running in. 579 * @param listener 580 * The {@link TextToSpeech.OnInitListener} that will be called when the 581 * TextToSpeech engine has initialized. In a case of a failure the listener 582 * may be called immediately, before TextToSpeech instance is fully constructed. 583 * @param engine Package name of the TTS engine to use. 584 */ 585 public TextToSpeech(Context context, OnInitListener listener, String engine) { 586 this(context, listener, engine, null, true); 587 } 588 589 /** 590 * Used by the framework to instantiate TextToSpeech objects with a supplied 591 * package name, instead of using {@link android.content.Context#getPackageName()} 592 * 593 * @hide 594 */ 595 public TextToSpeech(Context context, OnInitListener listener, String engine, 596 String packageName, boolean useFallback) { 597 mContext = context; 598 mInitListener = listener; 599 mRequestedEngine = engine; 600 mUseFallback = useFallback; 601 602 mEarcons = new HashMap<String, Uri>(); 603 mUtterances = new HashMap<String, Uri>(); 604 mUtteranceProgressListener = null; 605 606 mEnginesHelper = new TtsEngines(mContext); 607 if (packageName != null) { 608 mPackageName = packageName; 609 } else { 610 mPackageName = mContext.getPackageName(); 611 } 612 initTts(); 613 } 614 615 private <R> R runActionNoReconnect(Action<R> action, R errorResult, String method, 616 boolean onlyEstablishedConnection) { 617 return runAction(action, errorResult, method, false, onlyEstablishedConnection); 618 } 619 620 private <R> R runAction(Action<R> action, R errorResult, String method) { 621 return runAction(action, errorResult, method, true, true); 622 } 623 624 private <R> R runAction(Action<R> action, R errorResult, String method, 625 boolean reconnect, boolean onlyEstablishedConnection) { 626 synchronized (mStartLock) { 627 if (mServiceConnection == null) { 628 Log.w(TAG, method + " failed: not bound to TTS engine"); 629 return errorResult; 630 } 631 return mServiceConnection.runAction(action, errorResult, method, reconnect, 632 onlyEstablishedConnection); 633 } 634 } 635 636 private int initTts() { 637 // Step 1: Try connecting to the engine that was requested. 638 if (mRequestedEngine != null) { 639 if (mEnginesHelper.isEngineInstalled(mRequestedEngine)) { 640 if (connectToEngine(mRequestedEngine)) { 641 mCurrentEngine = mRequestedEngine; 642 return SUCCESS; 643 } else if (!mUseFallback) { 644 mCurrentEngine = null; 645 dispatchOnInit(ERROR); 646 return ERROR; 647 } 648 } else if (!mUseFallback) { 649 Log.i(TAG, "Requested engine not installed: " + mRequestedEngine); 650 mCurrentEngine = null; 651 dispatchOnInit(ERROR); 652 return ERROR; 653 } 654 } 655 656 // Step 2: Try connecting to the user's default engine. 657 final String defaultEngine = getDefaultEngine(); 658 if (defaultEngine != null && !defaultEngine.equals(mRequestedEngine)) { 659 if (connectToEngine(defaultEngine)) { 660 mCurrentEngine = defaultEngine; 661 return SUCCESS; 662 } 663 } 664 665 // Step 3: Try connecting to the highest ranked engine in the 666 // system. 667 final String highestRanked = mEnginesHelper.getHighestRankedEngineName(); 668 if (highestRanked != null && !highestRanked.equals(mRequestedEngine) && 669 !highestRanked.equals(defaultEngine)) { 670 if (connectToEngine(highestRanked)) { 671 mCurrentEngine = highestRanked; 672 return SUCCESS; 673 } 674 } 675 676 // NOTE: The API currently does not allow the caller to query whether 677 // they are actually connected to any engine. This might fail for various 678 // reasons like if the user disables all her TTS engines. 679 680 mCurrentEngine = null; 681 dispatchOnInit(ERROR); 682 return ERROR; 683 } 684 685 private boolean connectToEngine(String engine) { 686 Connection connection = new Connection(); 687 Intent intent = new Intent(Engine.INTENT_ACTION_TTS_SERVICE); 688 intent.setPackage(engine); 689 boolean bound = mContext.bindService(intent, connection, Context.BIND_AUTO_CREATE); 690 if (!bound) { 691 Log.e(TAG, "Failed to bind to " + engine); 692 return false; 693 } else { 694 Log.i(TAG, "Sucessfully bound to " + engine); 695 mConnectingServiceConnection = connection; 696 return true; 697 } 698 } 699 700 private void dispatchOnInit(int result) { 701 synchronized (mStartLock) { 702 if (mInitListener != null) { 703 mInitListener.onInit(result); 704 mInitListener = null; 705 } 706 } 707 } 708 709 private IBinder getCallerIdentity() { 710 return mServiceConnection.getCallerIdentity(); 711 } 712 713 /** 714 * Releases the resources used by the TextToSpeech engine. 715 * It is good practice for instance to call this method in the onDestroy() method of an Activity 716 * so the TextToSpeech engine can be cleanly stopped. 717 */ 718 public void shutdown() { 719 // Special case, we are asked to shutdown connection that did finalize its connection. 720 synchronized (mStartLock) { 721 if (mConnectingServiceConnection != null) { 722 mContext.unbindService(mConnectingServiceConnection); 723 mConnectingServiceConnection = null; 724 return; 725 } 726 } 727 728 // Post connection case 729 runActionNoReconnect(new Action<Void>() { 730 @Override 731 public Void run(ITextToSpeechService service) throws RemoteException { 732 service.setCallback(getCallerIdentity(), null); 733 service.stop(getCallerIdentity()); 734 mServiceConnection.disconnect(); 735 // Context#unbindService does not result in a call to 736 // ServiceConnection#onServiceDisconnected. As a result, the 737 // service ends up being destroyed (if there are no other open 738 // connections to it) but the process lives on and the 739 // ServiceConnection continues to refer to the destroyed service. 740 // 741 // This leads to tons of log spam about SynthThread being dead. 742 mServiceConnection = null; 743 mCurrentEngine = null; 744 return null; 745 } 746 }, null, "shutdown", false); 747 } 748 749 /** 750 * Adds a mapping between a string of text and a sound resource in a 751 * package. After a call to this method, subsequent calls to 752 * {@link #speak(String, int, HashMap)} will play the specified sound resource 753 * if it is available, or synthesize the text it is missing. 754 * 755 * @param text 756 * The string of text. Example: <code>"south_south_east"</code> 757 * 758 * @param packagename 759 * Pass the packagename of the application that contains the 760 * resource. If the resource is in your own application (this is 761 * the most common case), then put the packagename of your 762 * application here.<br/> 763 * Example: <b>"com.google.marvin.compass"</b><br/> 764 * The packagename can be found in the AndroidManifest.xml of 765 * your application. 766 * <p> 767 * <code><manifest xmlns:android="..." 768 * package="<b>com.google.marvin.compass</b>"></code> 769 * </p> 770 * 771 * @param resourceId 772 * Example: <code>R.raw.south_south_east</code> 773 * 774 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. 775 */ 776 public int addSpeech(String text, String packagename, int resourceId) { 777 synchronized (mStartLock) { 778 mUtterances.put(text, makeResourceUri(packagename, resourceId)); 779 return SUCCESS; 780 } 781 } 782 783 /** 784 * Adds a mapping between a string of text and a sound file. Using this, it 785 * is possible to add custom pronounciations for a string of text. 786 * After a call to this method, subsequent calls to {@link #speak(String, int, HashMap)} 787 * will play the specified sound resource if it is available, or synthesize the text it is 788 * missing. 789 * 790 * @param text 791 * The string of text. Example: <code>"south_south_east"</code> 792 * @param filename 793 * The full path to the sound file (for example: 794 * "/sdcard/mysounds/hello.wav") 795 * 796 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. 797 */ 798 public int addSpeech(String text, String filename) { 799 synchronized (mStartLock) { 800 mUtterances.put(text, Uri.parse(filename)); 801 return SUCCESS; 802 } 803 } 804 805 806 /** 807 * Adds a mapping between a string of text and a sound resource in a 808 * package. Use this to add custom earcons. 809 * 810 * @see #playEarcon(String, int, HashMap) 811 * 812 * @param earcon The name of the earcon. 813 * Example: <code>"[tick]"</code><br/> 814 * 815 * @param packagename 816 * the package name of the application that contains the 817 * resource. This can for instance be the package name of your own application. 818 * Example: <b>"com.google.marvin.compass"</b><br/> 819 * The package name can be found in the AndroidManifest.xml of 820 * the application containing the resource. 821 * <p> 822 * <code><manifest xmlns:android="..." 823 * package="<b>com.google.marvin.compass</b>"></code> 824 * </p> 825 * 826 * @param resourceId 827 * Example: <code>R.raw.tick_snd</code> 828 * 829 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. 830 */ 831 public int addEarcon(String earcon, String packagename, int resourceId) { 832 synchronized(mStartLock) { 833 mEarcons.put(earcon, makeResourceUri(packagename, resourceId)); 834 return SUCCESS; 835 } 836 } 837 838 /** 839 * Adds a mapping between a string of text and a sound file. 840 * Use this to add custom earcons. 841 * 842 * @see #playEarcon(String, int, HashMap) 843 * 844 * @param earcon 845 * The name of the earcon. 846 * Example: <code>"[tick]"</code> 847 * @param filename 848 * The full path to the sound file (for example: 849 * "/sdcard/mysounds/tick.wav") 850 * 851 * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. 852 */ 853 public int addEarcon(String earcon, String filename) { 854 synchronized(mStartLock) { 855 mEarcons.put(earcon, Uri.parse(filename)); 856 return SUCCESS; 857 } 858 } 859 860 private Uri makeResourceUri(String packageName, int resourceId) { 861 return new Uri.Builder() 862 .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) 863 .encodedAuthority(packageName) 864 .appendEncodedPath(String.valueOf(resourceId)) 865 .build(); 866 } 867 868 /** 869 * Speaks the string using the specified queuing strategy and speech parameters. 870 * This method is asynchronous, i.e. the method just adds the request to the queue of TTS 871 * requests and then returns. The synthesis might not have finished (or even started!) at the 872 * time when this method returns. In order to reliably detect errors during synthesis, 873 * we recommend setting an utterance progress listener (see 874 * {@link #setOnUtteranceProgressListener}) and using the 875 * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter. 876 * 877 * @param text The string of text to be spoken. No longer than 878 * {@link #getMaxSpeechInputLength()} characters. 879 * @param queueMode The queuing strategy to use, {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}. 880 * @param params Parameters for the request. Can be null. 881 * Supported parameter names: 882 * {@link Engine#KEY_PARAM_STREAM}, 883 * {@link Engine#KEY_PARAM_UTTERANCE_ID}, 884 * {@link Engine#KEY_PARAM_VOLUME}, 885 * {@link Engine#KEY_PARAM_PAN}. 886 * Engine specific parameters may be passed in but the parameter keys 887 * must be prefixed by the name of the engine they are intended for. For example 888 * the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the 889 * engine named "com.svox.pico" if it is being used. 890 * 891 * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the speak operation. 892 */ 893 public int speak(final String text, final int queueMode, final HashMap<String, String> params) { 894 return runAction(new Action<Integer>() { 895 @Override 896 public Integer run(ITextToSpeechService service) throws RemoteException { 897 Uri utteranceUri = mUtterances.get(text); 898 if (utteranceUri != null) { 899 return service.playAudio(getCallerIdentity(), utteranceUri, queueMode, 900 getParams(params)); 901 } else { 902 return service.speak(getCallerIdentity(), text, queueMode, getParams(params)); 903 } 904 } 905 }, ERROR, "speak"); 906 } 907 908 /** 909 * Plays the earcon using the specified queueing mode and parameters. 910 * The earcon must already have been added with {@link #addEarcon(String, String)} or 911 * {@link #addEarcon(String, String, int)}. 912 * This method is asynchronous, i.e. the method just adds the request to the queue of TTS 913 * requests and then returns. The synthesis might not have finished (or even started!) at the 914 * time when this method returns. In order to reliably detect errors during synthesis, 915 * we recommend setting an utterance progress listener (see 916 * {@link #setOnUtteranceProgressListener}) and using the 917 * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter. 918 * 919 * @param earcon The earcon that should be played 920 * @param queueMode {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}. 921 * @param params Parameters for the request. Can be null. 922 * Supported parameter names: 923 * {@link Engine#KEY_PARAM_STREAM}, 924 * {@link Engine#KEY_PARAM_UTTERANCE_ID}. 925 * Engine specific parameters may be passed in but the parameter keys 926 * must be prefixed by the name of the engine they are intended for. For example 927 * the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the 928 * engine named "com.svox.pico" if it is being used. 929 * 930 * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the playEarcon operation. 931 */ 932 public int playEarcon(final String earcon, final int queueMode, 933 final HashMap<String, String> params) { 934 return runAction(new Action<Integer>() { 935 @Override 936 public Integer run(ITextToSpeechService service) throws RemoteException { 937 Uri earconUri = mEarcons.get(earcon); 938 if (earconUri == null) { 939 return ERROR; 940 } 941 return service.playAudio(getCallerIdentity(), earconUri, queueMode, 942 getParams(params)); 943 } 944 }, ERROR, "playEarcon"); 945 } 946 947 /** 948 * Plays silence for the specified amount of time using the specified 949 * queue mode. 950 * This method is asynchronous, i.e. the method just adds the request to the queue of TTS 951 * requests and then returns. The synthesis might not have finished (or even started!) at the 952 * time when this method returns. In order to reliably detect errors during synthesis, 953 * we recommend setting an utterance progress listener (see 954 * {@link #setOnUtteranceProgressListener}) and using the 955 * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter. 956 * 957 * @param durationInMs The duration of the silence. 958 * @param queueMode {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}. 959 * @param params Parameters for the request. Can be null. 960 * Supported parameter names: 961 * {@link Engine#KEY_PARAM_UTTERANCE_ID}. 962 * Engine specific parameters may be passed in but the parameter keys 963 * must be prefixed by the name of the engine they are intended for. For example 964 * the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the 965 * engine named "com.svox.pico" if it is being used. 966 * 967 * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the playSilence operation. 968 */ 969 public int playSilence(final long durationInMs, final int queueMode, 970 final HashMap<String, String> params) { 971 return runAction(new Action<Integer>() { 972 @Override 973 public Integer run(ITextToSpeechService service) throws RemoteException { 974 return service.playSilence(getCallerIdentity(), durationInMs, queueMode, 975 params == null ? null : params.get(Engine.KEY_PARAM_UTTERANCE_ID)); 976 } 977 }, ERROR, "playSilence"); 978 } 979 980 /** 981 * Queries the engine for the set of features it supports for a given locale. 982 * Features can either be framework defined, e.g. 983 * {@link TextToSpeech.Engine#KEY_FEATURE_NETWORK_SYNTHESIS} or engine specific. 984 * Engine specific keys must be prefixed by the name of the engine they 985 * are intended for. These keys can be used as parameters to 986 * {@link TextToSpeech#speak(String, int, java.util.HashMap)} and 987 * {@link TextToSpeech#synthesizeToFile(String, java.util.HashMap, String)}. 988 * 989 * Features are boolean flags, and their values in the synthesis parameters 990 * must be behave as per {@link Boolean#parseBoolean(String)}. 991 * 992 * @param locale The locale to query features for. 993 * @return Set instance. May return {@code null} on error. 994 */ 995 public Set<String> getFeatures(final Locale locale) { 996 return runAction(new Action<Set<String>>() { 997 @Override 998 public Set<String> run(ITextToSpeechService service) throws RemoteException { 999 String[] features = null; 1000 try { 1001 features = service.getFeaturesForLanguage( 1002 locale.getISO3Language(), locale.getISO3Country(), locale.getVariant()); 1003 } catch(MissingResourceException e) { 1004 Log.w(TAG, "Couldn't retrieve 3 letter ISO 639-2/T language and/or ISO 3166 " + 1005 "country code for locale: " + locale, e); 1006 return null; 1007 } 1008 1009 if (features != null) { 1010 final Set<String> featureSet = new HashSet<String>(); 1011 Collections.addAll(featureSet, features); 1012 return featureSet; 1013 } 1014 return null; 1015 } 1016 }, null, "getFeatures"); 1017 } 1018 1019 /** 1020 * Checks whether the TTS engine is busy speaking. Note that a speech item is 1021 * considered complete once it's audio data has been sent to the audio mixer, or 1022 * written to a file. There might be a finite lag between this point, and when 1023 * the audio hardware completes playback. 1024 * 1025 * @return {@code true} if the TTS engine is speaking. 1026 */ 1027 public boolean isSpeaking() { 1028 return runAction(new Action<Boolean>() { 1029 @Override 1030 public Boolean run(ITextToSpeechService service) throws RemoteException { 1031 return service.isSpeaking(); 1032 } 1033 }, false, "isSpeaking"); 1034 } 1035 1036 /** 1037 * Interrupts the current utterance (whether played or rendered to file) and discards other 1038 * utterances in the queue. 1039 * 1040 * @return {@link #ERROR} or {@link #SUCCESS}. 1041 */ 1042 public int stop() { 1043 return runAction(new Action<Integer>() { 1044 @Override 1045 public Integer run(ITextToSpeechService service) throws RemoteException { 1046 return service.stop(getCallerIdentity()); 1047 } 1048 }, ERROR, "stop"); 1049 } 1050 1051 /** 1052 * Sets the speech rate. 1053 * 1054 * This has no effect on any pre-recorded speech. 1055 * 1056 * @param speechRate Speech rate. {@code 1.0} is the normal speech rate, 1057 * lower values slow down the speech ({@code 0.5} is half the normal speech rate), 1058 * greater values accelerate it ({@code 2.0} is twice the normal speech rate). 1059 * 1060 * @return {@link #ERROR} or {@link #SUCCESS}. 1061 */ 1062 public int setSpeechRate(float speechRate) { 1063 if (speechRate > 0.0f) { 1064 int intRate = (int)(speechRate * 100); 1065 if (intRate > 0) { 1066 synchronized (mStartLock) { 1067 mParams.putInt(Engine.KEY_PARAM_RATE, intRate); 1068 } 1069 return SUCCESS; 1070 } 1071 } 1072 return ERROR; 1073 } 1074 1075 /** 1076 * Sets the speech pitch for the TextToSpeech engine. 1077 * 1078 * This has no effect on any pre-recorded speech. 1079 * 1080 * @param pitch Speech pitch. {@code 1.0} is the normal pitch, 1081 * lower values lower the tone of the synthesized voice, 1082 * greater values increase it. 1083 * 1084 * @return {@link #ERROR} or {@link #SUCCESS}. 1085 */ 1086 public int setPitch(float pitch) { 1087 if (pitch > 0.0f) { 1088 int intPitch = (int)(pitch * 100); 1089 if (intPitch > 0) { 1090 synchronized (mStartLock) { 1091 mParams.putInt(Engine.KEY_PARAM_PITCH, intPitch); 1092 } 1093 return SUCCESS; 1094 } 1095 } 1096 return ERROR; 1097 } 1098 1099 /** 1100 * @return the engine currently in use by this TextToSpeech instance. 1101 * @hide 1102 */ 1103 public String getCurrentEngine() { 1104 return mCurrentEngine; 1105 } 1106 1107 /** 1108 * Returns a Locale instance describing the language currently being used as the default 1109 * Text-to-speech language. 1110 * 1111 * @return language, country (if any) and variant (if any) used by the client stored in a 1112 * Locale instance, or {@code null} on error. 1113 */ 1114 public Locale getDefaultLanguage() { 1115 return runAction(new Action<Locale>() { 1116 @Override 1117 public Locale run(ITextToSpeechService service) throws RemoteException { 1118 String[] defaultLanguage = service.getClientDefaultLanguage(); 1119 1120 return new Locale(defaultLanguage[0], defaultLanguage[1], defaultLanguage[2]); 1121 } 1122 }, null, "getDefaultLanguage"); 1123 } 1124 1125 /** 1126 * Sets the text-to-speech language. 1127 * The TTS engine will try to use the closest match to the specified 1128 * language as represented by the Locale, but there is no guarantee that the exact same Locale 1129 * will be used. Use {@link #isLanguageAvailable(Locale)} to check the level of support 1130 * before choosing the language to use for the next utterances. 1131 * 1132 * @param loc The locale describing the language to be used. 1133 * 1134 * @return Code indicating the support status for the locale. See {@link #LANG_AVAILABLE}, 1135 * {@link #LANG_COUNTRY_AVAILABLE}, {@link #LANG_COUNTRY_VAR_AVAILABLE}, 1136 * {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}. 1137 */ 1138 public int setLanguage(final Locale loc) { 1139 return runAction(new Action<Integer>() { 1140 @Override 1141 public Integer run(ITextToSpeechService service) throws RemoteException { 1142 if (loc == null) { 1143 return LANG_NOT_SUPPORTED; 1144 } 1145 String language = null, country = null; 1146 try { 1147 language = loc.getISO3Language(); 1148 } catch (MissingResourceException e) { 1149 Log.w(TAG, "Couldn't retrieve ISO 639-2/T language code for locale: " + loc, e); 1150 return LANG_NOT_SUPPORTED; 1151 } 1152 1153 try { 1154 country = loc.getISO3Country(); 1155 } catch (MissingResourceException e) { 1156 Log.w(TAG, "Couldn't retrieve ISO 3166 country code for locale: " + loc, e); 1157 return LANG_NOT_SUPPORTED; 1158 } 1159 1160 String variant = loc.getVariant(); 1161 1162 // Check if the language, country, variant are available, and cache 1163 // the available parts. 1164 // Note that the language is not actually set here, instead it is cached so it 1165 // will be associated with all upcoming utterances. 1166 1167 int result = service.loadLanguage(getCallerIdentity(), language, country, variant); 1168 if (result >= LANG_AVAILABLE){ 1169 if (result < LANG_COUNTRY_VAR_AVAILABLE) { 1170 variant = ""; 1171 if (result < LANG_COUNTRY_AVAILABLE) { 1172 country = ""; 1173 } 1174 } 1175 mParams.putString(Engine.KEY_PARAM_LANGUAGE, language); 1176 mParams.putString(Engine.KEY_PARAM_COUNTRY, country); 1177 mParams.putString(Engine.KEY_PARAM_VARIANT, variant); 1178 } 1179 return result; 1180 } 1181 }, LANG_NOT_SUPPORTED, "setLanguage"); 1182 } 1183 1184 /** 1185 * Returns a Locale instance describing the language currently being used for synthesis 1186 * requests sent to the TextToSpeech engine. 1187 * 1188 * In Android 4.2 and before (API <= 17) this function returns the language that is currently 1189 * being used by the TTS engine. That is the last language set by this or any other 1190 * client by a {@link TextToSpeech#setLanguage} call to the same engine. 1191 * 1192 * In Android versions after 4.2 this function returns the language that is currently being 1193 * used for the synthesis requests sent from this client. That is the last language set 1194 * by a {@link TextToSpeech#setLanguage} call on this instance. 1195 * 1196 * @return language, country (if any) and variant (if any) used by the client stored in a 1197 * Locale instance, or {@code null} on error. 1198 */ 1199 public Locale getLanguage() { 1200 return runAction(new Action<Locale>() { 1201 @Override 1202 public Locale run(ITextToSpeechService service) { 1203 /* No service call, but we're accessing mParams, hence need for 1204 wrapping it as an Action instance */ 1205 String lang = mParams.getString(Engine.KEY_PARAM_LANGUAGE, ""); 1206 String country = mParams.getString(Engine.KEY_PARAM_COUNTRY, ""); 1207 String variant = mParams.getString(Engine.KEY_PARAM_VARIANT, ""); 1208 return new Locale(lang, country, variant); 1209 } 1210 }, null, "getLanguage"); 1211 } 1212 1213 /** 1214 * Checks if the specified language as represented by the Locale is available and supported. 1215 * 1216 * @param loc The Locale describing the language to be used. 1217 * 1218 * @return Code indicating the support status for the locale. See {@link #LANG_AVAILABLE}, 1219 * {@link #LANG_COUNTRY_AVAILABLE}, {@link #LANG_COUNTRY_VAR_AVAILABLE}, 1220 * {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}. 1221 */ 1222 public int isLanguageAvailable(final Locale loc) { 1223 return runAction(new Action<Integer>() { 1224 @Override 1225 public Integer run(ITextToSpeechService service) throws RemoteException { 1226 String language = null, country = null; 1227 1228 try { 1229 language = loc.getISO3Language(); 1230 } catch (MissingResourceException e) { 1231 Log.w(TAG, "Couldn't retrieve ISO 639-2/T language code for locale: " + loc, e); 1232 return LANG_NOT_SUPPORTED; 1233 } 1234 1235 try { 1236 country = loc.getISO3Country(); 1237 } catch (MissingResourceException e) { 1238 Log.w(TAG, "Couldn't retrieve ISO 3166 country code for locale: " + loc, e); 1239 return LANG_NOT_SUPPORTED; 1240 } 1241 1242 return service.isLanguageAvailable(language, country, loc.getVariant()); 1243 } 1244 }, LANG_NOT_SUPPORTED, "isLanguageAvailable"); 1245 } 1246 1247 /** 1248 * Synthesizes the given text to a file using the specified parameters. 1249 * This method is asynchronous, i.e. the method just adds the request to the queue of TTS 1250 * requests and then returns. The synthesis might not have finished (or even started!) at the 1251 * time when this method returns. In order to reliably detect errors during synthesis, 1252 * we recommend setting an utterance progress listener (see 1253 * {@link #setOnUtteranceProgressListener}) and using the 1254 * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter. 1255 * 1256 * @param text The text that should be synthesized. No longer than 1257 * {@link #getMaxSpeechInputLength()} characters. 1258 * @param params Parameters for the request. Can be null. 1259 * Supported parameter names: 1260 * {@link Engine#KEY_PARAM_UTTERANCE_ID}. 1261 * Engine specific parameters may be passed in but the parameter keys 1262 * must be prefixed by the name of the engine they are intended for. For example 1263 * the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the 1264 * engine named "com.svox.pico" if it is being used. 1265 * @param filename Absolute file filename to write the generated audio data to.It should be 1266 * something like "/sdcard/myappsounds/mysound.wav". 1267 * 1268 * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the synthesizeToFile operation. 1269 */ 1270 public int synthesizeToFile(final String text, final HashMap<String, String> params, 1271 final String filename) { 1272 return runAction(new Action<Integer>() { 1273 @Override 1274 public Integer run(ITextToSpeechService service) throws RemoteException { 1275 ParcelFileDescriptor fileDescriptor; 1276 int returnValue; 1277 try { 1278 File file = new File(filename); 1279 if(file.exists() && !file.canWrite()) { 1280 Log.e(TAG, "Can't write to " + filename); 1281 return ERROR; 1282 } 1283 fileDescriptor = ParcelFileDescriptor.open(file, 1284 ParcelFileDescriptor.MODE_WRITE_ONLY | 1285 ParcelFileDescriptor.MODE_CREATE | 1286 ParcelFileDescriptor.MODE_TRUNCATE); 1287 returnValue = service.synthesizeToFileDescriptor(getCallerIdentity(), text, 1288 fileDescriptor, getParams(params)); 1289 fileDescriptor.close(); 1290 return returnValue; 1291 } catch (FileNotFoundException e) { 1292 Log.e(TAG, "Opening file " + filename + " failed", e); 1293 return ERROR; 1294 } catch (IOException e) { 1295 Log.e(TAG, "Closing file " + filename + " failed", e); 1296 return ERROR; 1297 } 1298 } 1299 }, ERROR, "synthesizeToFile"); 1300 } 1301 1302 private Bundle getParams(HashMap<String, String> params) { 1303 if (params != null && !params.isEmpty()) { 1304 Bundle bundle = new Bundle(mParams); 1305 copyIntParam(bundle, params, Engine.KEY_PARAM_STREAM); 1306 copyStringParam(bundle, params, Engine.KEY_PARAM_UTTERANCE_ID); 1307 copyFloatParam(bundle, params, Engine.KEY_PARAM_VOLUME); 1308 copyFloatParam(bundle, params, Engine.KEY_PARAM_PAN); 1309 1310 // Copy feature strings defined by the framework. 1311 copyStringParam(bundle, params, Engine.KEY_FEATURE_NETWORK_SYNTHESIS); 1312 copyStringParam(bundle, params, Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS); 1313 1314 // Copy over all parameters that start with the name of the 1315 // engine that we are currently connected to. The engine is 1316 // free to interpret them as it chooses. 1317 if (!TextUtils.isEmpty(mCurrentEngine)) { 1318 for (Map.Entry<String, String> entry : params.entrySet()) { 1319 final String key = entry.getKey(); 1320 if (key != null && key.startsWith(mCurrentEngine)) { 1321 bundle.putString(key, entry.getValue()); 1322 } 1323 } 1324 } 1325 1326 return bundle; 1327 } else { 1328 return mParams; 1329 } 1330 } 1331 1332 private void copyStringParam(Bundle bundle, HashMap<String, String> params, String key) { 1333 String value = params.get(key); 1334 if (value != null) { 1335 bundle.putString(key, value); 1336 } 1337 } 1338 1339 private void copyIntParam(Bundle bundle, HashMap<String, String> params, String key) { 1340 String valueString = params.get(key); 1341 if (!TextUtils.isEmpty(valueString)) { 1342 try { 1343 int value = Integer.parseInt(valueString); 1344 bundle.putInt(key, value); 1345 } catch (NumberFormatException ex) { 1346 // don't set the value in the bundle 1347 } 1348 } 1349 } 1350 1351 private void copyFloatParam(Bundle bundle, HashMap<String, String> params, String key) { 1352 String valueString = params.get(key); 1353 if (!TextUtils.isEmpty(valueString)) { 1354 try { 1355 float value = Float.parseFloat(valueString); 1356 bundle.putFloat(key, value); 1357 } catch (NumberFormatException ex) { 1358 // don't set the value in the bundle 1359 } 1360 } 1361 } 1362 1363 /** 1364 * Sets the listener that will be notified when synthesis of an utterance completes. 1365 * 1366 * @param listener The listener to use. 1367 * 1368 * @return {@link #ERROR} or {@link #SUCCESS}. 1369 * 1370 * @deprecated Use {@link #setOnUtteranceProgressListener(UtteranceProgressListener)} 1371 * instead. 1372 */ 1373 @Deprecated 1374 public int setOnUtteranceCompletedListener(final OnUtteranceCompletedListener listener) { 1375 mUtteranceProgressListener = UtteranceProgressListener.from(listener); 1376 return TextToSpeech.SUCCESS; 1377 } 1378 1379 /** 1380 * Sets the listener that will be notified of various events related to the 1381 * synthesis of a given utterance. 1382 * 1383 * See {@link UtteranceProgressListener} and 1384 * {@link TextToSpeech.Engine#KEY_PARAM_UTTERANCE_ID}. 1385 * 1386 * @param listener the listener to use. 1387 * @return {@link #ERROR} or {@link #SUCCESS} 1388 */ 1389 public int setOnUtteranceProgressListener(UtteranceProgressListener listener) { 1390 mUtteranceProgressListener = listener; 1391 return TextToSpeech.SUCCESS; 1392 } 1393 1394 /** 1395 * Sets the TTS engine to use. 1396 * 1397 * @deprecated This doesn't inform callers when the TTS engine has been 1398 * initialized. {@link #TextToSpeech(Context, OnInitListener, String)} 1399 * can be used with the appropriate engine name. Also, there is no 1400 * guarantee that the engine specified will be loaded. If it isn't 1401 * installed or disabled, the user / system wide defaults will apply. 1402 * 1403 * @param enginePackageName The package name for the synthesis engine (e.g. "com.svox.pico") 1404 * 1405 * @return {@link #ERROR} or {@link #SUCCESS}. 1406 */ 1407 @Deprecated 1408 public int setEngineByPackageName(String enginePackageName) { 1409 mRequestedEngine = enginePackageName; 1410 return initTts(); 1411 } 1412 1413 /** 1414 * Gets the package name of the default speech synthesis engine. 1415 * 1416 * @return Package name of the TTS engine that the user has chosen 1417 * as their default. 1418 */ 1419 public String getDefaultEngine() { 1420 return mEnginesHelper.getDefaultEngine(); 1421 } 1422 1423 /** 1424 * Checks whether the user's settings should override settings requested 1425 * by the calling application. As of the Ice cream sandwich release, 1426 * user settings never forcibly override the app's settings. 1427 */ 1428 public boolean areDefaultsEnforced() { 1429 return false; 1430 } 1431 1432 /** 1433 * Gets a list of all installed TTS engines. 1434 * 1435 * @return A list of engine info objects. The list can be empty, but never {@code null}. 1436 */ 1437 public List<EngineInfo> getEngines() { 1438 return mEnginesHelper.getEngines(); 1439 } 1440 1441 private class Connection implements ServiceConnection { 1442 private ITextToSpeechService mService; 1443 1444 private SetupConnectionAsyncTask mOnSetupConnectionAsyncTask; 1445 1446 private boolean mEstablished; 1447 1448 private final ITextToSpeechCallback.Stub mCallback = new ITextToSpeechCallback.Stub() { 1449 public void onStop(String utteranceId) throws RemoteException { 1450 // do nothing 1451 }; 1452 1453 @Override 1454 public void onFallback(String utteranceId) throws RemoteException { 1455 // do nothing 1456 } 1457 1458 @Override 1459 public void onSuccess(String utteranceId) { 1460 UtteranceProgressListener listener = mUtteranceProgressListener; 1461 if (listener != null) { 1462 listener.onDone(utteranceId); 1463 } 1464 } 1465 1466 @Override 1467 public void onError(String utteranceId, int errorCode) { 1468 UtteranceProgressListener listener = mUtteranceProgressListener; 1469 if (listener != null) { 1470 listener.onError(utteranceId); 1471 } 1472 } 1473 1474 @Override 1475 public void onStart(String utteranceId) { 1476 UtteranceProgressListener listener = mUtteranceProgressListener; 1477 if (listener != null) { 1478 listener.onStart(utteranceId); 1479 } 1480 } 1481 1482 @Override 1483 public void onVoicesInfoChange(List<VoiceInfo> voicesInfo) throws RemoteException { 1484 // Ignore it 1485 } 1486 }; 1487 1488 private class SetupConnectionAsyncTask extends AsyncTask<Void, Void, Integer> { 1489 private final ComponentName mName; 1490 1491 public SetupConnectionAsyncTask(ComponentName name) { 1492 mName = name; 1493 } 1494 1495 @Override 1496 protected Integer doInBackground(Void... params) { 1497 synchronized(mStartLock) { 1498 if (isCancelled()) { 1499 return null; 1500 } 1501 1502 try { 1503 mService.setCallback(getCallerIdentity(), mCallback); 1504 1505 if (mParams.getString(Engine.KEY_PARAM_LANGUAGE) == null) { 1506 String[] defaultLanguage = mService.getClientDefaultLanguage(); 1507 mParams.putString(Engine.KEY_PARAM_LANGUAGE, defaultLanguage[0]); 1508 mParams.putString(Engine.KEY_PARAM_COUNTRY, defaultLanguage[1]); 1509 mParams.putString(Engine.KEY_PARAM_VARIANT, defaultLanguage[2]); 1510 } 1511 1512 Log.i(TAG, "Set up connection to " + mName); 1513 return SUCCESS; 1514 } catch (RemoteException re) { 1515 Log.e(TAG, "Error connecting to service, setCallback() failed"); 1516 return ERROR; 1517 } 1518 } 1519 } 1520 1521 @Override 1522 protected void onPostExecute(Integer result) { 1523 synchronized(mStartLock) { 1524 if (mOnSetupConnectionAsyncTask == this) { 1525 mOnSetupConnectionAsyncTask = null; 1526 } 1527 mEstablished = true; 1528 dispatchOnInit(result); 1529 } 1530 } 1531 } 1532 1533 @Override 1534 public void onServiceConnected(ComponentName name, IBinder service) { 1535 synchronized(mStartLock) { 1536 mConnectingServiceConnection = null; 1537 1538 Log.i(TAG, "Connected to " + name); 1539 1540 if (mOnSetupConnectionAsyncTask != null) { 1541 mOnSetupConnectionAsyncTask.cancel(false); 1542 } 1543 1544 mService = ITextToSpeechService.Stub.asInterface(service); 1545 mServiceConnection = Connection.this; 1546 1547 mEstablished = false; 1548 mOnSetupConnectionAsyncTask = new SetupConnectionAsyncTask(name); 1549 mOnSetupConnectionAsyncTask.execute(); 1550 } 1551 } 1552 1553 public IBinder getCallerIdentity() { 1554 return mCallback; 1555 } 1556 1557 /** 1558 * Clear connection related fields and cancel mOnServiceConnectedAsyncTask if set. 1559 * 1560 * @return true if we cancel mOnSetupConnectionAsyncTask in progress. 1561 */ 1562 private boolean clearServiceConnection() { 1563 synchronized(mStartLock) { 1564 boolean result = false; 1565 if (mOnSetupConnectionAsyncTask != null) { 1566 result = mOnSetupConnectionAsyncTask.cancel(false); 1567 mOnSetupConnectionAsyncTask = null; 1568 } 1569 1570 mService = null; 1571 // If this is the active connection, clear it 1572 if (mServiceConnection == this) { 1573 mServiceConnection = null; 1574 } 1575 return result; 1576 } 1577 } 1578 1579 @Override 1580 public void onServiceDisconnected(ComponentName name) { 1581 Log.i(TAG, "Asked to disconnect from " + name); 1582 if (clearServiceConnection()) { 1583 /* We need to protect against a rare case where engine 1584 * dies just after successful connection - and we process onServiceDisconnected 1585 * before OnServiceConnectedAsyncTask.onPostExecute. onServiceDisconnected cancels 1586 * OnServiceConnectedAsyncTask.onPostExecute and we don't call dispatchOnInit 1587 * with ERROR as argument. 1588 */ 1589 dispatchOnInit(ERROR); 1590 } 1591 } 1592 1593 public void disconnect() { 1594 mContext.unbindService(this); 1595 clearServiceConnection(); 1596 } 1597 1598 public boolean isEstablished() { 1599 return mService != null && mEstablished; 1600 } 1601 1602 public <R> R runAction(Action<R> action, R errorResult, String method, 1603 boolean reconnect, boolean onlyEstablishedConnection) { 1604 synchronized (mStartLock) { 1605 try { 1606 if (mService == null) { 1607 Log.w(TAG, method + " failed: not connected to TTS engine"); 1608 return errorResult; 1609 } 1610 if (onlyEstablishedConnection && !isEstablished()) { 1611 Log.w(TAG, method + " failed: TTS engine connection not fully set up"); 1612 return errorResult; 1613 } 1614 return action.run(mService); 1615 } catch (RemoteException ex) { 1616 Log.e(TAG, method + " failed", ex); 1617 if (reconnect) { 1618 disconnect(); 1619 initTts(); 1620 } 1621 return errorResult; 1622 } 1623 } 1624 } 1625 } 1626 1627 private interface Action<R> { 1628 R run(ITextToSpeechService service) throws RemoteException; 1629 } 1630 1631 /** 1632 * Information about an installed text-to-speech engine. 1633 * 1634 * @see TextToSpeech#getEngines 1635 */ 1636 public static class EngineInfo { 1637 /** 1638 * Engine package name.. 1639 */ 1640 public String name; 1641 /** 1642 * Localized label for the engine. 1643 */ 1644 public String label; 1645 /** 1646 * Icon for the engine. 1647 */ 1648 public int icon; 1649 /** 1650 * Whether this engine is a part of the system 1651 * image. 1652 * 1653 * @hide 1654 */ 1655 public boolean system; 1656 /** 1657 * The priority the engine declares for the the intent filter 1658 * {@code android.intent.action.TTS_SERVICE} 1659 * 1660 * @hide 1661 */ 1662 public int priority; 1663 1664 @Override 1665 public String toString() { 1666 return "EngineInfo{name=" + name + "}"; 1667 } 1668 1669 } 1670 1671 /** 1672 * Limit of length of input string passed to speak and synthesizeToFile. 1673 * 1674 * @see #speak 1675 * @see #synthesizeToFile 1676 */ 1677 public static int getMaxSpeechInputLength() { 1678 return 4000; 1679 } 1680} 1681