TextToSpeech.java revision c3edf2a01a2cf2123a3de17ec1da11a3b6c459f0
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.Bundle;
28import android.os.IBinder;
29import android.os.RemoteException;
30import android.provider.Settings;
31import android.text.TextUtils;
32import android.util.Log;
33
34import java.util.HashMap;
35import java.util.List;
36import java.util.Locale;
37import java.util.Map;
38
39/**
40 *
41 * Synthesizes speech from text for immediate playback or to create a sound file.
42 * <p>A TextToSpeech instance can only be used to synthesize text once it has completed its
43 * initialization. Implement the {@link TextToSpeech.OnInitListener} to be
44 * notified of the completion of the initialization.<br>
45 * When you are done using the TextToSpeech instance, call the {@link #shutdown()} method
46 * to release the native resources used by the TextToSpeech engine.
47 *
48 */
49public class TextToSpeech {
50
51    private static final String TAG = "TextToSpeech";
52
53    /**
54     * Denotes a successful operation.
55     */
56    public static final int SUCCESS = 0;
57    /**
58     * Denotes a generic operation failure.
59     */
60    public static final int ERROR = -1;
61
62    /**
63     * Queue mode where all entries in the playback queue (media to be played
64     * and text to be synthesized) are dropped and replaced by the new entry.
65     * Queues are flushed with respect to a given calling app. Entries in the queue
66     * from other callees are not discarded.
67     */
68    public static final int QUEUE_FLUSH = 0;
69    /**
70     * Queue mode where the new entry is added at the end of the playback queue.
71     */
72    public static final int QUEUE_ADD = 1;
73    /**
74     * Queue mode where the entire playback queue is purged. This is different
75     * from {@link #QUEUE_FLUSH} in that all entries are purged, not just entries
76     * from a given caller.
77     *
78     * @hide
79     */
80    static final int QUEUE_DESTROY = 2;
81
82    /**
83     * Denotes the language is available exactly as specified by the locale.
84     */
85    public static final int LANG_COUNTRY_VAR_AVAILABLE = 2;
86
87    /**
88     * Denotes the language is available for the language and country specified
89     * by the locale, but not the variant.
90     */
91    public static final int LANG_COUNTRY_AVAILABLE = 1;
92
93    /**
94     * Denotes the language is available for the language by the locale,
95     * but not the country and variant.
96     */
97    public static final int LANG_AVAILABLE = 0;
98
99    /**
100     * Denotes the language data is missing.
101     */
102    public static final int LANG_MISSING_DATA = -1;
103
104    /**
105     * Denotes the language is not supported.
106     */
107    public static final int LANG_NOT_SUPPORTED = -2;
108
109    /**
110     * Broadcast Action: The TextToSpeech synthesizer has completed processing
111     * of all the text in the speech queue.
112     */
113    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
114    public static final String ACTION_TTS_QUEUE_PROCESSING_COMPLETED =
115            "android.speech.tts.TTS_QUEUE_PROCESSING_COMPLETED";
116
117    /**
118     * Interface definition of a callback to be invoked indicating the completion of the
119     * TextToSpeech engine initialization.
120     */
121    public interface OnInitListener {
122        /**
123         * Called to signal the completion of the TextToSpeech engine initialization.
124         *
125         * @param status {@link TextToSpeech#SUCCESS} or {@link TextToSpeech#ERROR}.
126         */
127        public void onInit(int status);
128    }
129
130    /**
131     * Listener that will be called when the TTS service has
132     * completed synthesizing an utterance. This is only called if the utterance
133     * has an utterance ID (see {@link TextToSpeech.Engine#KEY_PARAM_UTTERANCE_ID}).
134     */
135    public interface OnUtteranceCompletedListener {
136        /**
137         * Called when an utterance has been synthesized.
138         *
139         * @param utteranceId the identifier of the utterance.
140         */
141        public void onUtteranceCompleted(String utteranceId);
142    }
143
144    /**
145     * Constants and parameter names for controlling text-to-speech.
146     */
147    public class Engine {
148
149        /**
150         * Default speech rate.
151         * @hide
152         */
153        public static final int DEFAULT_RATE = 100;
154
155        /**
156         * Default pitch.
157         * @hide
158         */
159        public static final int DEFAULT_PITCH = 100;
160
161        /**
162         * Default volume.
163         * @hide
164         */
165        public static final float DEFAULT_VOLUME = 1.0f;
166
167        /**
168         * Default pan (centered).
169         * @hide
170         */
171        public static final float DEFAULT_PAN = 0.0f;
172
173        /**
174         * Default value for {@link Settings.Secure#TTS_USE_DEFAULTS}.
175         * @hide
176         */
177        public static final int USE_DEFAULTS = 0; // false
178
179        /**
180         * Package name of the default TTS engine.
181         *
182         * @hide
183         * @deprecated No longer in use, the default engine is determined by
184         *         the sort order defined in {@link EngineInfoComparator}. Note that
185         *         this doesn't "break" anything because there is no guarantee that
186         *         the engine specified below is installed on a given build, let
187         *         alone be the default.
188         */
189        @Deprecated
190        public static final String DEFAULT_ENGINE = "com.svox.pico";
191
192        /**
193         * Default audio stream used when playing synthesized speech.
194         */
195        public static final int DEFAULT_STREAM = AudioManager.STREAM_MUSIC;
196
197        /**
198         * Indicates success when checking the installation status of the resources used by the
199         * TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
200         */
201        public static final int CHECK_VOICE_DATA_PASS = 1;
202
203        /**
204         * Indicates failure when checking the installation status of the resources used by the
205         * TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
206         */
207        public static final int CHECK_VOICE_DATA_FAIL = 0;
208
209        /**
210         * Indicates erroneous data when checking the installation status of the resources used by
211         * the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
212         */
213        public static final int CHECK_VOICE_DATA_BAD_DATA = -1;
214
215        /**
216         * Indicates missing resources when checking the installation status of the resources used
217         * by the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
218         */
219        public static final int CHECK_VOICE_DATA_MISSING_DATA = -2;
220
221        /**
222         * Indicates missing storage volume when checking the installation status of the resources
223         * used by the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent.
224         */
225        public static final int CHECK_VOICE_DATA_MISSING_VOLUME = -3;
226
227        /**
228         * Intent for starting a TTS service. Services that handle this intent must
229         * extend {@link TextToSpeechService}. Normal applications should not use this intent
230         * directly, instead they should talk to the TTS service using the the methods in this
231         * class.
232         */
233        @SdkConstant(SdkConstantType.SERVICE_ACTION)
234        public static final String INTENT_ACTION_TTS_SERVICE =
235                "android.intent.action.TTS_SERVICE";
236
237        // intents to ask engine to install data or check its data
238        /**
239         * Activity Action: Triggers the platform TextToSpeech engine to
240         * start the activity that installs the resource files on the device
241         * that are required for TTS to be operational. Since the installation
242         * of the data can be interrupted or declined by the user, the application
243         * shouldn't expect successful installation upon return from that intent,
244         * and if need be, should check installation status with
245         * {@link #ACTION_CHECK_TTS_DATA}.
246         */
247        @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
248        public static final String ACTION_INSTALL_TTS_DATA =
249                "android.speech.tts.engine.INSTALL_TTS_DATA";
250
251        /**
252         * Broadcast Action: broadcast to signal the completion of the installation of
253         * the data files used by the synthesis engine. Success or failure is indicated in the
254         * {@link #EXTRA_TTS_DATA_INSTALLED} extra.
255         */
256        @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
257        public static final String ACTION_TTS_DATA_INSTALLED =
258                "android.speech.tts.engine.TTS_DATA_INSTALLED";
259
260        /**
261         * Activity Action: Starts the activity from the platform TextToSpeech
262         * engine to verify the proper installation and availability of the
263         * resource files on the system. Upon completion, the activity will
264         * return one of the following codes:
265         * {@link #CHECK_VOICE_DATA_PASS},
266         * {@link #CHECK_VOICE_DATA_FAIL},
267         * {@link #CHECK_VOICE_DATA_BAD_DATA},
268         * {@link #CHECK_VOICE_DATA_MISSING_DATA}, or
269         * {@link #CHECK_VOICE_DATA_MISSING_VOLUME}.
270         * <p> Moreover, the data received in the activity result will contain the following
271         * fields:
272         * <ul>
273         *   <li>{@link #EXTRA_VOICE_DATA_ROOT_DIRECTORY} which
274         *       indicates the path to the location of the resource files,</li>
275         *   <li>{@link #EXTRA_VOICE_DATA_FILES} which contains
276         *       the list of all the resource files,</li>
277         *   <li>and {@link #EXTRA_VOICE_DATA_FILES_INFO} which
278         *       contains, for each resource file, the description of the language covered by
279         *       the file in the xxx-YYY format, where xxx is the 3-letter ISO language code,
280         *       and YYY is the 3-letter ISO country code.</li>
281         * </ul>
282         */
283        @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
284        public static final String ACTION_CHECK_TTS_DATA =
285                "android.speech.tts.engine.CHECK_TTS_DATA";
286
287        /**
288         * Activity intent for getting some sample text to use for demonstrating TTS.
289         *
290         * @hide This intent was used by engines written against the old API.
291         * Not sure if it should be exposed.
292         */
293        @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
294        public static final String ACTION_GET_SAMPLE_TEXT =
295                "android.speech.tts.engine.GET_SAMPLE_TEXT";
296
297        // extras for a TTS engine's check data activity
298        /**
299         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
300         * the TextToSpeech engine specifies the path to its resources.
301         */
302        public static final String EXTRA_VOICE_DATA_ROOT_DIRECTORY = "dataRoot";
303
304        /**
305         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
306         * the TextToSpeech engine specifies the file names of its resources under the
307         * resource path.
308         */
309        public static final String EXTRA_VOICE_DATA_FILES = "dataFiles";
310
311        /**
312         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
313         * the TextToSpeech engine specifies the locale associated with each resource file.
314         */
315        public static final String EXTRA_VOICE_DATA_FILES_INFO = "dataFilesInfo";
316
317        /**
318         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
319         * the TextToSpeech engine returns an ArrayList<String> of all the available voices.
320         * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are
321         * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").
322         */
323        public static final String EXTRA_AVAILABLE_VOICES = "availableVoices";
324
325        /**
326         * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent where
327         * the TextToSpeech engine returns an ArrayList<String> of all the unavailable voices.
328         * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are
329         * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").
330         */
331        public static final String EXTRA_UNAVAILABLE_VOICES = "unavailableVoices";
332
333        /**
334         * Extra information sent with the {@link #ACTION_CHECK_TTS_DATA} intent where the
335         * caller indicates to the TextToSpeech engine which specific sets of voice data to
336         * check for by sending an ArrayList<String> of the voices that are of interest.
337         * The format of each voice is: lang-COUNTRY-variant where COUNTRY and variant are
338         * optional (ie, "eng" or "eng-USA" or "eng-USA-FEMALE").
339         */
340        public static final String EXTRA_CHECK_VOICE_DATA_FOR = "checkVoiceDataFor";
341
342        // extras for a TTS engine's data installation
343        /**
344         * Extra information received with the {@link #ACTION_TTS_DATA_INSTALLED} intent.
345         * It indicates whether the data files for the synthesis engine were successfully
346         * installed. The installation was initiated with the  {@link #ACTION_INSTALL_TTS_DATA}
347         * intent. The possible values for this extra are
348         * {@link TextToSpeech#SUCCESS} and {@link TextToSpeech#ERROR}.
349         */
350        public static final String EXTRA_TTS_DATA_INSTALLED = "dataInstalled";
351
352        // keys for the parameters passed with speak commands. Hidden keys are used internally
353        // to maintain engine state for each TextToSpeech instance.
354        /**
355         * @hide
356         */
357        public static final String KEY_PARAM_RATE = "rate";
358
359        /**
360         * @hide
361         */
362        public static final String KEY_PARAM_LANGUAGE = "language";
363
364        /**
365         * @hide
366         */
367        public static final String KEY_PARAM_COUNTRY = "country";
368
369        /**
370         * @hide
371         */
372        public static final String KEY_PARAM_VARIANT = "variant";
373
374        /**
375         * @hide
376         */
377        public static final String KEY_PARAM_ENGINE = "engine";
378
379        /**
380         * @hide
381         */
382        public static final String KEY_PARAM_PITCH = "pitch";
383
384        /**
385         * Parameter key to specify the audio stream type to be used when speaking text
386         * or playing back a file. The value should be one of the STREAM_ constants
387         * defined in {@link AudioManager}.
388         *
389         * @see TextToSpeech#speak(String, int, HashMap)
390         * @see TextToSpeech#playEarcon(String, int, HashMap)
391         */
392        public static final String KEY_PARAM_STREAM = "streamType";
393
394        /**
395         * Parameter key to identify an utterance in the
396         * {@link TextToSpeech.OnUtteranceCompletedListener} after text has been
397         * spoken, a file has been played back or a silence duration has elapsed.
398         *
399         * @see TextToSpeech#speak(String, int, HashMap)
400         * @see TextToSpeech#playEarcon(String, int, HashMap)
401         * @see TextToSpeech#synthesizeToFile(String, HashMap, String)
402         */
403        public static final String KEY_PARAM_UTTERANCE_ID = "utteranceId";
404
405        /**
406         * Parameter key to specify the speech volume relative to the current stream type
407         * volume used when speaking text. Volume is specified as a float ranging from 0 to 1
408         * where 0 is silence, and 1 is the maximum volume (the default behavior).
409         *
410         * @see TextToSpeech#speak(String, int, HashMap)
411         * @see TextToSpeech#playEarcon(String, int, HashMap)
412         */
413        public static final String KEY_PARAM_VOLUME = "volume";
414
415        /**
416         * Parameter key to specify how the speech is panned from left to right when speaking text.
417         * Pan is specified as a float ranging from -1 to +1 where -1 maps to a hard-left pan,
418         * 0 to center (the default behavior), and +1 to hard-right.
419         *
420         * @see TextToSpeech#speak(String, int, HashMap)
421         * @see TextToSpeech#playEarcon(String, int, HashMap)
422         */
423        public static final String KEY_PARAM_PAN = "pan";
424
425    }
426
427    private final Context mContext;
428    private Connection mServiceConnection;
429    private OnInitListener mInitListener;
430    private final Object mStartLock = new Object();
431
432    private String mRequestedEngine;
433    private final Map<String, Uri> mEarcons;
434    private final Map<String, Uri> mUtterances;
435    private final Bundle mParams = new Bundle();
436    private final TtsEngines mEnginesHelper;
437    private String mCurrentEngine = null;
438
439    /**
440     * The constructor for the TextToSpeech class, using the default TTS engine.
441     * This will also initialize the associated TextToSpeech engine if it isn't already running.
442     *
443     * @param context
444     *            The context this instance is running in.
445     * @param listener
446     *            The {@link TextToSpeech.OnInitListener} that will be called when the
447     *            TextToSpeech engine has initialized.
448     */
449    public TextToSpeech(Context context, OnInitListener listener) {
450        this(context, listener, null);
451    }
452
453    /**
454     * The constructor for the TextToSpeech class, using the given TTS engine.
455     * This will also initialize the associated TextToSpeech engine if it isn't already running.
456     *
457     * @param context
458     *            The context this instance is running in.
459     * @param listener
460     *            The {@link TextToSpeech.OnInitListener} that will be called when the
461     *            TextToSpeech engine has initialized.
462     * @param engine Package name of the TTS engine to use.
463     */
464    public TextToSpeech(Context context, OnInitListener listener, String engine) {
465        mContext = context;
466        mInitListener = listener;
467        mRequestedEngine = engine;
468
469        mEarcons = new HashMap<String, Uri>();
470        mUtterances = new HashMap<String, Uri>();
471
472        mEnginesHelper = new TtsEngines(mContext);
473        initTts();
474    }
475
476    private String getPackageName() {
477        return mContext.getPackageName();
478    }
479
480    private <R> R runActionNoReconnect(Action<R> action, R errorResult, String method) {
481        return runAction(action, errorResult, method, false);
482    }
483
484    private <R> R runAction(Action<R> action, R errorResult, String method) {
485        return runAction(action, errorResult, method, true);
486    }
487
488    private <R> R runAction(Action<R> action, R errorResult, String method, boolean reconnect) {
489        synchronized (mStartLock) {
490            if (mServiceConnection == null) {
491                Log.w(TAG, method + " failed: not bound to TTS engine");
492                return errorResult;
493            }
494            return mServiceConnection.runAction(action, errorResult, method, reconnect);
495        }
496    }
497
498    private int initTts() {
499        String defaultEngine = getDefaultEngine();
500        String engine = defaultEngine;
501        if (mEnginesHelper.isEngineInstalled(mRequestedEngine)) {
502            engine = mRequestedEngine;
503        }
504
505        // Try requested engine
506        if (connectToEngine(engine)) {
507            mCurrentEngine = engine;
508            return SUCCESS;
509        }
510
511        // Fall back to user's default engine if different from the already tested one
512        if (!engine.equals(defaultEngine)) {
513            if (connectToEngine(defaultEngine)) {
514                mCurrentEngine = engine;
515                return SUCCESS;
516            }
517        }
518
519        final String highestRanked = mEnginesHelper.getHighestRankedEngineName();
520        // Fall back to the hardcoded default if different from the two above
521        if (!defaultEngine.equals(highestRanked)
522                && !engine.equals(highestRanked)) {
523            if (connectToEngine(highestRanked)) {
524                mCurrentEngine = engine;
525                return SUCCESS;
526            }
527        }
528
529        dispatchOnInit(ERROR);
530        return ERROR;
531    }
532
533    private boolean connectToEngine(String engine) {
534        Connection connection = new Connection();
535        Intent intent = new Intent(Engine.INTENT_ACTION_TTS_SERVICE);
536        intent.setPackage(engine);
537        boolean bound = mContext.bindService(intent, connection, Context.BIND_AUTO_CREATE);
538        if (!bound) {
539            Log.e(TAG, "Failed to bind to " + engine);
540            return false;
541        } else {
542            Log.i(TAG, "Sucessfully bound to " + engine);
543            return true;
544        }
545    }
546
547    private void dispatchOnInit(int result) {
548        synchronized (mStartLock) {
549            if (mInitListener != null) {
550                mInitListener.onInit(result);
551                mInitListener = null;
552            }
553        }
554    }
555
556    /**
557     * Releases the resources used by the TextToSpeech engine.
558     * It is good practice for instance to call this method in the onDestroy() method of an Activity
559     * so the TextToSpeech engine can be cleanly stopped.
560     */
561    public void shutdown() {
562        runActionNoReconnect(new Action<Void>() {
563            @Override
564            public Void run(ITextToSpeechService service) throws RemoteException {
565                service.setCallback(getPackageName(), null);
566                service.stop(getPackageName());
567                mServiceConnection.disconnect();
568                return null;
569            }
570        }, null, "shutdown");
571    }
572
573    /**
574     * Adds a mapping between a string of text and a sound resource in a
575     * package. After a call to this method, subsequent calls to
576     * {@link #speak(String, int, HashMap)} will play the specified sound resource
577     * if it is available, or synthesize the text it is missing.
578     *
579     * @param text
580     *            The string of text. Example: <code>"south_south_east"</code>
581     *
582     * @param packagename
583     *            Pass the packagename of the application that contains the
584     *            resource. If the resource is in your own application (this is
585     *            the most common case), then put the packagename of your
586     *            application here.<br/>
587     *            Example: <b>"com.google.marvin.compass"</b><br/>
588     *            The packagename can be found in the AndroidManifest.xml of
589     *            your application.
590     *            <p>
591     *            <code>&lt;manifest xmlns:android=&quot;...&quot;
592     *      package=&quot;<b>com.google.marvin.compass</b>&quot;&gt;</code>
593     *            </p>
594     *
595     * @param resourceId
596     *            Example: <code>R.raw.south_south_east</code>
597     *
598     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
599     */
600    public int addSpeech(String text, String packagename, int resourceId) {
601        synchronized (mStartLock) {
602            mUtterances.put(text, makeResourceUri(packagename, resourceId));
603            return SUCCESS;
604        }
605    }
606
607    /**
608     * Adds a mapping between a string of text and a sound file. Using this, it
609     * is possible to add custom pronounciations for a string of text.
610     * After a call to this method, subsequent calls to {@link #speak(String, int, HashMap)}
611     * will play the specified sound resource if it is available, or synthesize the text it is
612     * missing.
613     *
614     * @param text
615     *            The string of text. Example: <code>"south_south_east"</code>
616     * @param filename
617     *            The full path to the sound file (for example:
618     *            "/sdcard/mysounds/hello.wav")
619     *
620     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
621     */
622    public int addSpeech(String text, String filename) {
623        synchronized (mStartLock) {
624            mUtterances.put(text, Uri.parse(filename));
625            return SUCCESS;
626        }
627    }
628
629
630    /**
631     * Adds a mapping between a string of text and a sound resource in a
632     * package. Use this to add custom earcons.
633     *
634     * @see #playEarcon(String, int, HashMap)
635     *
636     * @param earcon The name of the earcon.
637     *            Example: <code>"[tick]"</code><br/>
638     *
639     * @param packagename
640     *            the package name of the application that contains the
641     *            resource. This can for instance be the package name of your own application.
642     *            Example: <b>"com.google.marvin.compass"</b><br/>
643     *            The package name can be found in the AndroidManifest.xml of
644     *            the application containing the resource.
645     *            <p>
646     *            <code>&lt;manifest xmlns:android=&quot;...&quot;
647     *      package=&quot;<b>com.google.marvin.compass</b>&quot;&gt;</code>
648     *            </p>
649     *
650     * @param resourceId
651     *            Example: <code>R.raw.tick_snd</code>
652     *
653     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
654     */
655    public int addEarcon(String earcon, String packagename, int resourceId) {
656        synchronized(mStartLock) {
657            mEarcons.put(earcon, makeResourceUri(packagename, resourceId));
658            return SUCCESS;
659        }
660    }
661
662    /**
663     * Adds a mapping between a string of text and a sound file.
664     * Use this to add custom earcons.
665     *
666     * @see #playEarcon(String, int, HashMap)
667     *
668     * @param earcon
669     *            The name of the earcon.
670     *            Example: <code>"[tick]"</code>
671     * @param filename
672     *            The full path to the sound file (for example:
673     *            "/sdcard/mysounds/tick.wav")
674     *
675     * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}.
676     */
677    public int addEarcon(String earcon, String filename) {
678        synchronized(mStartLock) {
679            mEarcons.put(earcon, Uri.parse(filename));
680            return SUCCESS;
681        }
682    }
683
684    private Uri makeResourceUri(String packageName, int resourceId) {
685        return new Uri.Builder()
686                .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
687                .encodedAuthority(packageName)
688                .appendEncodedPath(String.valueOf(resourceId))
689                .build();
690    }
691
692    /**
693     * Speaks the string using the specified queuing strategy and speech
694     * parameters.
695     *
696     * @param text The string of text to be spoken.
697     * @param queueMode The queuing strategy to use, {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
698     * @param params Parameters for the request. Can be null.
699     *            Supported parameter names:
700     *            {@link Engine#KEY_PARAM_STREAM},
701     *            {@link Engine#KEY_PARAM_UTTERANCE_ID},
702     *            {@link Engine#KEY_PARAM_VOLUME},
703     *            {@link Engine#KEY_PARAM_PAN}.
704     *            Engine specific parameters may be passed in but the parameter keys
705     *            must be prefixed by the name of the engine they are intended for. For example
706     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
707     *            engine named "com.svox.pico" if it is being used.
708     *
709     * @return {@link #ERROR} or {@link #SUCCESS}.
710     */
711    public int speak(final String text, final int queueMode, final HashMap<String, String> params) {
712        return runAction(new Action<Integer>() {
713            @Override
714            public Integer run(ITextToSpeechService service) throws RemoteException {
715                Uri utteranceUri = mUtterances.get(text);
716                if (utteranceUri != null) {
717                    return service.playAudio(getPackageName(), utteranceUri, queueMode,
718                            getParams(params));
719                } else {
720                    return service.speak(getPackageName(), text, queueMode, getParams(params));
721                }
722            }
723        }, ERROR, "speak");
724    }
725
726    /**
727     * Plays the earcon using the specified queueing mode and parameters.
728     * The earcon must already have been added with {@link #addEarcon(String, String)} or
729     * {@link #addEarcon(String, String, int)}.
730     *
731     * @param earcon The earcon that should be played
732     * @param queueMode {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
733     * @param params Parameters for the request. Can be null.
734     *            Supported parameter names:
735     *            {@link Engine#KEY_PARAM_STREAM},
736     *            {@link Engine#KEY_PARAM_UTTERANCE_ID}.
737     *            Engine specific parameters may be passed in but the parameter keys
738     *            must be prefixed by the name of the engine they are intended for. For example
739     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
740     *            engine named "com.svox.pico" if it is being used.
741     *
742     * @return {@link #ERROR} or {@link #SUCCESS}.
743     */
744    public int playEarcon(final String earcon, final int queueMode,
745            final HashMap<String, String> params) {
746        return runAction(new Action<Integer>() {
747            @Override
748            public Integer run(ITextToSpeechService service) throws RemoteException {
749                Uri earconUri = mEarcons.get(earcon);
750                if (earconUri == null) {
751                    return ERROR;
752                }
753                return service.playAudio(getPackageName(), earconUri, queueMode,
754                        getParams(params));
755            }
756        }, ERROR, "playEarcon");
757    }
758
759    /**
760     * Plays silence for the specified amount of time using the specified
761     * queue mode.
762     *
763     * @param durationInMs The duration of the silence.
764     * @param queueMode {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
765     * @param params Parameters for the request. Can be null.
766     *            Supported parameter names:
767     *            {@link Engine#KEY_PARAM_UTTERANCE_ID}.
768     *            Engine specific parameters may be passed in but the parameter keys
769     *            must be prefixed by the name of the engine they are intended for. For example
770     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
771     *            engine named "com.svox.pico" if it is being used.
772     *
773     * @return {@link #ERROR} or {@link #SUCCESS}.
774     */
775    public int playSilence(final long durationInMs, final int queueMode,
776            final HashMap<String, String> params) {
777        return runAction(new Action<Integer>() {
778            @Override
779            public Integer run(ITextToSpeechService service) throws RemoteException {
780                return service.playSilence(getPackageName(), durationInMs, queueMode,
781                        getParams(params));
782            }
783        }, ERROR, "playSilence");
784    }
785
786    /**
787     * Checks whether the TTS engine is busy speaking.
788     *
789     * @return {@code true} if the TTS engine is speaking.
790     */
791    public boolean isSpeaking() {
792        return runAction(new Action<Boolean>() {
793            @Override
794            public Boolean run(ITextToSpeechService service) throws RemoteException {
795                return service.isSpeaking();
796            }
797        }, false, "isSpeaking");
798    }
799
800    /**
801     * Interrupts the current utterance (whether played or rendered to file) and discards other
802     * utterances in the queue.
803     *
804     * @return {@link #ERROR} or {@link #SUCCESS}.
805     */
806    public int stop() {
807        return runAction(new Action<Integer>() {
808            @Override
809            public Integer run(ITextToSpeechService service) throws RemoteException {
810                return service.stop(getPackageName());
811            }
812        }, ERROR, "stop");
813    }
814
815    /**
816     * Sets the speech rate.
817     *
818     * This has no effect on any pre-recorded speech.
819     *
820     * @param speechRate Speech rate. {@code 1.0} is the normal speech rate,
821     *            lower values slow down the speech ({@code 0.5} is half the normal speech rate),
822     *            greater values accelerate it ({@code 2.0} is twice the normal speech rate).
823     *
824     * @return {@link #ERROR} or {@link #SUCCESS}.
825     */
826    public int setSpeechRate(float speechRate) {
827        if (speechRate > 0.0f) {
828            int intRate = (int)(speechRate * 100);
829            if (intRate > 0) {
830                synchronized (mStartLock) {
831                    mParams.putInt(Engine.KEY_PARAM_RATE, intRate);
832                }
833                return SUCCESS;
834            }
835        }
836        return ERROR;
837    }
838
839    /**
840     * Sets the speech pitch for the TextToSpeech engine.
841     *
842     * This has no effect on any pre-recorded speech.
843     *
844     * @param pitch Speech pitch. {@code 1.0} is the normal pitch,
845     *            lower values lower the tone of the synthesized voice,
846     *            greater values increase it.
847     *
848     * @return {@link #ERROR} or {@link #SUCCESS}.
849     */
850    public int setPitch(float pitch) {
851        if (pitch > 0.0f) {
852            int intPitch = (int)(pitch * 100);
853            if (intPitch > 0) {
854                synchronized (mStartLock) {
855                    mParams.putInt(Engine.KEY_PARAM_PITCH, intPitch);
856                }
857                return SUCCESS;
858            }
859        }
860        return ERROR;
861    }
862
863    /**
864     * Sets the text-to-speech language.
865     * The TTS engine will try to use the closest match to the specified
866     * language as represented by the Locale, but there is no guarantee that the exact same Locale
867     * will be used. Use {@link #isLanguageAvailable(Locale)} to check the level of support
868     * before choosing the language to use for the next utterances.
869     *
870     * @param loc The locale describing the language to be used.
871     *
872     * @return Code indicating the support status for the locale. See {@link #LANG_AVAILABLE},
873     *         {@link #LANG_COUNTRY_AVAILABLE}, {@link #LANG_COUNTRY_VAR_AVAILABLE},
874     *         {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}.
875     */
876    public int setLanguage(final Locale loc) {
877        return runAction(new Action<Integer>() {
878            @Override
879            public Integer run(ITextToSpeechService service) throws RemoteException {
880                if (loc == null) {
881                    return LANG_NOT_SUPPORTED;
882                }
883                String language = loc.getISO3Language();
884                String country = loc.getISO3Country();
885                String variant = loc.getVariant();
886                // Check if the language, country, variant are available, and cache
887                // the available parts.
888                // Note that the language is not actually set here, instead it is cached so it
889                // will be associated with all upcoming utterances.
890                int result = service.loadLanguage(language, country, variant);
891                if (result >= LANG_AVAILABLE){
892                    if (result < LANG_COUNTRY_VAR_AVAILABLE) {
893                        variant = "";
894                        if (result < LANG_COUNTRY_AVAILABLE) {
895                            country = "";
896                        }
897                    }
898                    mParams.putString(Engine.KEY_PARAM_LANGUAGE, language);
899                    mParams.putString(Engine.KEY_PARAM_COUNTRY, country);
900                    mParams.putString(Engine.KEY_PARAM_VARIANT, variant);
901                }
902                return result;
903            }
904        }, LANG_NOT_SUPPORTED, "setLanguage");
905    }
906
907    /**
908     * Returns a Locale instance describing the language currently being used by the TextToSpeech
909     * engine.
910     *
911     * @return language, country (if any) and variant (if any) used by the engine stored in a Locale
912     *     instance, or {@code null} on error.
913     */
914    public Locale getLanguage() {
915        return runAction(new Action<Locale>() {
916            @Override
917            public Locale run(ITextToSpeechService service) throws RemoteException {
918                String[] locStrings = service.getLanguage();
919                if (locStrings != null && locStrings.length == 3) {
920                    return new Locale(locStrings[0], locStrings[1], locStrings[2]);
921                }
922                return null;
923            }
924        }, null, "getLanguage");
925    }
926
927    /**
928     * Checks if the specified language as represented by the Locale is available and supported.
929     *
930     * @param loc The Locale describing the language to be used.
931     *
932     * @return Code indicating the support status for the locale. See {@link #LANG_AVAILABLE},
933     *         {@link #LANG_COUNTRY_AVAILABLE}, {@link #LANG_COUNTRY_VAR_AVAILABLE},
934     *         {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}.
935     */
936    public int isLanguageAvailable(final Locale loc) {
937        return runAction(new Action<Integer>() {
938            @Override
939            public Integer run(ITextToSpeechService service) throws RemoteException {
940                return service.isLanguageAvailable(loc.getISO3Language(),
941                        loc.getISO3Country(), loc.getVariant());
942            }
943        }, LANG_NOT_SUPPORTED, "isLanguageAvailable");
944    }
945
946    /**
947     * Synthesizes the given text to a file using the specified parameters.
948     *
949     * @param text Thetext that should be synthesized
950     * @param params Parameters for the request. Can be null.
951     *            Supported parameter names:
952     *            {@link Engine#KEY_PARAM_UTTERANCE_ID}.
953     *            Engine specific parameters may be passed in but the parameter keys
954     *            must be prefixed by the name of the engine they are intended for. For example
955     *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
956     *            engine named "com.svox.pico" if it is being used.
957     * @param filename Absolute file filename to write the generated audio data to.It should be
958     *            something like "/sdcard/myappsounds/mysound.wav".
959     *
960     * @return {@link #ERROR} or {@link #SUCCESS}.
961     */
962    public int synthesizeToFile(final String text, final HashMap<String, String> params,
963            final String filename) {
964        return runAction(new Action<Integer>() {
965            @Override
966            public Integer run(ITextToSpeechService service) throws RemoteException {
967                return service.synthesizeToFile(getPackageName(), text, filename,
968                        getParams(params));
969            }
970        }, ERROR, "synthesizeToFile");
971    }
972
973    private Bundle getParams(HashMap<String, String> params) {
974        if (params != null && !params.isEmpty()) {
975            Bundle bundle = new Bundle(mParams);
976            copyIntParam(bundle, params, Engine.KEY_PARAM_STREAM);
977            copyStringParam(bundle, params, Engine.KEY_PARAM_UTTERANCE_ID);
978            copyFloatParam(bundle, params, Engine.KEY_PARAM_VOLUME);
979            copyFloatParam(bundle, params, Engine.KEY_PARAM_PAN);
980
981            // Copy over all parameters that start with the name of the
982            // engine that we are currently connected to. The engine is
983            // free to interpret them as it chooses.
984            if (!TextUtils.isEmpty(mCurrentEngine)) {
985                for (Map.Entry<String, String> entry : params.entrySet()) {
986                    final String key = entry.getKey();
987                    if (key != null && key.startsWith(mCurrentEngine)) {
988                        bundle.putString(key, entry.getValue());
989                    }
990                }
991            }
992
993            return bundle;
994        } else {
995            return mParams;
996        }
997    }
998
999    private void copyStringParam(Bundle bundle, HashMap<String, String> params, String key) {
1000        String value = params.get(key);
1001        if (value != null) {
1002            bundle.putString(key, value);
1003        }
1004    }
1005
1006    private void copyIntParam(Bundle bundle, HashMap<String, String> params, String key) {
1007        String valueString = params.get(key);
1008        if (!TextUtils.isEmpty(valueString)) {
1009            try {
1010                int value = Integer.parseInt(valueString);
1011                bundle.putInt(key, value);
1012            } catch (NumberFormatException ex) {
1013                // don't set the value in the bundle
1014            }
1015        }
1016    }
1017
1018    private void copyFloatParam(Bundle bundle, HashMap<String, String> params, String key) {
1019        String valueString = params.get(key);
1020        if (!TextUtils.isEmpty(valueString)) {
1021            try {
1022                float value = Float.parseFloat(valueString);
1023                bundle.putFloat(key, value);
1024            } catch (NumberFormatException ex) {
1025                // don't set the value in the bundle
1026            }
1027        }
1028    }
1029
1030    /**
1031     * Sets the listener that will be notified when synthesis of an utterance completes.
1032     *
1033     * @param listener The listener to use.
1034     *
1035     * @return {@link #ERROR} or {@link #SUCCESS}.
1036     */
1037    public int setOnUtteranceCompletedListener(final OnUtteranceCompletedListener listener) {
1038        return runAction(new Action<Integer>() {
1039            @Override
1040            public Integer run(ITextToSpeechService service) throws RemoteException {
1041                ITextToSpeechCallback.Stub callback = new ITextToSpeechCallback.Stub() {
1042                    public void utteranceCompleted(String utteranceId) {
1043                        if (listener != null) {
1044                            listener.onUtteranceCompleted(utteranceId);
1045                        }
1046                    }
1047                };
1048                service.setCallback(getPackageName(), callback);
1049                return SUCCESS;
1050            }
1051        }, ERROR, "setOnUtteranceCompletedListener");
1052    }
1053
1054    /**
1055     * Sets the TTS engine to use.
1056     *
1057     * @deprecated This doesn't inform callers when the TTS engine has been
1058     *        initialized. {@link #TextToSpeech(Context, OnInitListener, String)}
1059     *        can be used with the appropriate engine name.
1060     *
1061     * @param enginePackageName The package name for the synthesis engine (e.g. "com.svox.pico")
1062     *
1063     * @return {@link #ERROR} or {@link #SUCCESS}.
1064     */
1065    @Deprecated
1066    public int setEngineByPackageName(String enginePackageName) {
1067        mRequestedEngine = enginePackageName;
1068        return initTts();
1069    }
1070
1071    /**
1072     * Gets the package name of the default speech synthesis engine.
1073     *
1074     * @return Package name of the TTS engine that the user has chosen
1075     *        as their default.
1076     */
1077    public String getDefaultEngine() {
1078        return mEnginesHelper.getDefaultEngine();
1079    }
1080
1081    /**
1082     * Checks whether the user's settings should override settings requested
1083     * by the calling application. As of the Ice cream sandwich release,
1084     * user settings never forcibly override the app's settings.
1085     */
1086    public boolean areDefaultsEnforced() {
1087        return false;
1088    }
1089
1090    /**
1091     * Gets a list of all installed TTS engines.
1092     *
1093     * @return A list of engine info objects. The list can be empty, but never {@code null}.
1094     */
1095    public List<EngineInfo> getEngines() {
1096        return mEnginesHelper.getEngines();
1097    }
1098
1099
1100    private class Connection implements ServiceConnection {
1101        private ITextToSpeechService mService;
1102
1103        public void onServiceConnected(ComponentName name, IBinder service) {
1104            Log.i(TAG, "Connected to " + name);
1105            synchronized(mStartLock) {
1106                if (mServiceConnection != null) {
1107                    // Disconnect any previous service connection
1108                    mServiceConnection.disconnect();
1109                }
1110                mServiceConnection = this;
1111                mService = ITextToSpeechService.Stub.asInterface(service);
1112                dispatchOnInit(SUCCESS);
1113            }
1114        }
1115
1116        public void onServiceDisconnected(ComponentName name) {
1117            synchronized(mStartLock) {
1118                mService = null;
1119                // If this is the active connection, clear it
1120                if (mServiceConnection == this) {
1121                    mServiceConnection = null;
1122                }
1123            }
1124        }
1125
1126        public void disconnect() {
1127            mContext.unbindService(this);
1128        }
1129
1130        public <R> R runAction(Action<R> action, R errorResult, String method, boolean reconnect) {
1131            try {
1132                synchronized (mStartLock) {
1133                    if (mService == null) {
1134                        Log.w(TAG, method + " failed: not connected to TTS engine");
1135                        return errorResult;
1136                    }
1137                    return action.run(mService);
1138                }
1139            } catch (RemoteException ex) {
1140                Log.e(TAG, method + " failed", ex);
1141                if (reconnect) {
1142                    disconnect();
1143                    initTts();
1144                }
1145                return errorResult;
1146            }
1147        }
1148    }
1149
1150    private interface Action<R> {
1151        R run(ITextToSpeechService service) throws RemoteException;
1152    }
1153
1154    /**
1155     * Information about an installed text-to-speech engine.
1156     *
1157     * @see TextToSpeech#getEngines
1158     */
1159    public static class EngineInfo {
1160        /**
1161         * Engine package name..
1162         */
1163        public String name;
1164        /**
1165         * Localized label for the engine.
1166         */
1167        public String label;
1168        /**
1169         * Icon for the engine.
1170         */
1171        public int icon;
1172        /**
1173         * Whether this engine is a part of the system
1174         * image.
1175         *
1176         * @hide
1177         */
1178        public boolean system;
1179        /**
1180         * The priority the engine declares for the the intent filter
1181         * {@code android.intent.action.TTS_SERVICE}
1182         *
1183         * @hide
1184         */
1185        public int priority;
1186
1187        @Override
1188        public String toString() {
1189            return "EngineInfo{name=" + name + "}";
1190        }
1191
1192    }
1193
1194}
1195