Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1 | /* |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 2 | * Copyright (C) 2009 The Android Open Source Project |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
| 15 | */ |
Jean-Michel Trivi | 21a6a6d | 2009-06-08 15:21:46 -0700 | [diff] [blame] | 16 | package android.speech.tts; |
| 17 | |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 18 | import android.annotation.SdkConstant; |
| 19 | import android.annotation.SdkConstant.SdkConstantType; |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 20 | import android.content.ComponentName; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 21 | import android.content.ContentResolver; |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.ServiceConnection; |
Jean-Michel Trivi | a981013 | 2009-07-10 12:08:59 -0700 | [diff] [blame] | 25 | import android.media.AudioManager; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 26 | import android.net.Uri; |
Przemyslaw Szczepaniak | 8f3957c | 2012-11-07 14:03:53 +0000 | [diff] [blame] | 27 | import android.os.AsyncTask; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 28 | import android.os.Bundle; |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 29 | import android.os.IBinder; |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 30 | import android.os.ParcelFileDescriptor; |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 31 | import android.os.RemoteException; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 32 | import android.provider.Settings; |
| 33 | import android.text.TextUtils; |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 34 | import android.util.Log; |
| 35 | |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 36 | import java.io.File; |
| 37 | import java.io.FileNotFoundException; |
| 38 | import java.io.IOException; |
Narayan Kamath | 748af66 | 2011-10-31 14:20:01 +0000 | [diff] [blame] | 39 | import java.util.Collections; |
Jean-Michel Trivi | a8518c1 | 2009-06-10 17:33:34 -0700 | [diff] [blame] | 40 | import java.util.HashMap; |
Narayan Kamath | 748af66 | 2011-10-31 14:20:01 +0000 | [diff] [blame] | 41 | import java.util.HashSet; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 42 | import java.util.List; |
Jean-Michel Trivi | 679d728 | 2009-06-16 15:36:28 -0700 | [diff] [blame] | 43 | import java.util.Locale; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 44 | import java.util.Map; |
Przemyslaw Szczepaniak | d0b9279 | 2013-05-08 15:26:15 +0100 | [diff] [blame] | 45 | import java.util.MissingResourceException; |
Narayan Kamath | 748af66 | 2011-10-31 14:20:01 +0000 | [diff] [blame] | 46 | import java.util.Set; |
Jean-Michel Trivi | a8518c1 | 2009-06-10 17:33:34 -0700 | [diff] [blame] | 47 | |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 48 | /** |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 49 | * |
Jean-Michel Trivi | 62788e9 | 2009-07-02 16:29:30 -0700 | [diff] [blame] | 50 | * Synthesizes speech from text for immediate playback or to create a sound file. |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 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. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 56 | * |
Nick Kralevich | eb33705 | 2013-10-24 15:56:04 -0700 | [diff] [blame] | 57 | * @deprecated Use {@link TextToSpeechClient} instead |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 58 | */ |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 59 | @Deprecated |
Jean-Michel Trivi | a8518c1 | 2009-06-10 17:33:34 -0700 | [diff] [blame] | 60 | public class TextToSpeech { |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 61 | |
Bjorn Bringert | 2cad2cc | 2011-03-08 11:53:12 +0000 | [diff] [blame] | 62 | private static final String TAG = "TextToSpeech"; |
| 63 | |
Jean-Michel Trivi | 91bf30a | 2009-06-11 14:35:48 -0700 | [diff] [blame] | 64 | /** |
| 65 | * Denotes a successful operation. |
| 66 | */ |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 67 | public static final int SUCCESS = 0; |
Jean-Michel Trivi | 91bf30a | 2009-06-11 14:35:48 -0700 | [diff] [blame] | 68 | /** |
| 69 | * Denotes a generic operation failure. |
| 70 | */ |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 71 | public static final int ERROR = -1; |
Jean-Michel Trivi | 91bf30a | 2009-06-11 14:35:48 -0700 | [diff] [blame] | 72 | |
Jean-Michel Trivi | 679d728 | 2009-06-16 15:36:28 -0700 | [diff] [blame] | 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. |
Narayan Kamath | abc63fb | 2011-06-10 11:36:57 +0100 | [diff] [blame] | 76 | * Queues are flushed with respect to a given calling app. Entries in the queue |
| 77 | * from other callees are not discarded. |
Jean-Michel Trivi | 679d728 | 2009-06-16 15:36:28 -0700 | [diff] [blame] | 78 | */ |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 79 | public static final int QUEUE_FLUSH = 0; |
Jean-Michel Trivi | 679d728 | 2009-06-16 15:36:28 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Queue mode where the new entry is added at the end of the playback queue. |
| 82 | */ |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 83 | public static final int QUEUE_ADD = 1; |
Narayan Kamath | abc63fb | 2011-06-10 11:36:57 +0100 | [diff] [blame] | 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; |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 92 | |
Charles Chen | aaf842e | 2009-06-25 11:59:29 -0700 | [diff] [blame] | 93 | /** |
Jean-Michel Trivi | 9f5eadd | 2009-08-14 15:44:31 -0700 | [diff] [blame] | 94 | * Denotes the language is available exactly as specified by the locale. |
Charles Chen | aaf842e | 2009-06-25 11:59:29 -0700 | [diff] [blame] | 95 | */ |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 96 | public static final int LANG_COUNTRY_VAR_AVAILABLE = 2; |
Charles Chen | aaf842e | 2009-06-25 11:59:29 -0700 | [diff] [blame] | 97 | |
Charles Chen | aaf842e | 2009-06-25 11:59:29 -0700 | [diff] [blame] | 98 | /** |
Narayan Kamath | b956f37 | 2011-05-16 16:51:44 +0100 | [diff] [blame] | 99 | * Denotes the language is available for the language and country specified |
Charles Chen | aaf842e | 2009-06-25 11:59:29 -0700 | [diff] [blame] | 100 | * by the locale, but not the variant. |
| 101 | */ |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 102 | public static final int LANG_COUNTRY_AVAILABLE = 1; |
Charles Chen | aaf842e | 2009-06-25 11:59:29 -0700 | [diff] [blame] | 103 | |
Charles Chen | aaf842e | 2009-06-25 11:59:29 -0700 | [diff] [blame] | 104 | /** |
Narayan Kamath | b956f37 | 2011-05-16 16:51:44 +0100 | [diff] [blame] | 105 | * Denotes the language is available for the language by the locale, |
Charles Chen | aaf842e | 2009-06-25 11:59:29 -0700 | [diff] [blame] | 106 | * but not the country and variant. |
| 107 | */ |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 108 | public static final int LANG_AVAILABLE = 0; |
Charles Chen | aaf842e | 2009-06-25 11:59:29 -0700 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * Denotes the language data is missing. |
| 112 | */ |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 113 | public static final int LANG_MISSING_DATA = -1; |
Charles Chen | aaf842e | 2009-06-25 11:59:29 -0700 | [diff] [blame] | 114 | |
| 115 | /** |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 116 | * Denotes the language is not supported. |
Charles Chen | aaf842e | 2009-06-25 11:59:29 -0700 | [diff] [blame] | 117 | */ |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 118 | public static final int LANG_NOT_SUPPORTED = -2; |
| 119 | |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 120 | /** |
| 121 | * Broadcast Action: The TextToSpeech synthesizer has completed processing |
| 122 | * of all the text in the speech queue. |
Narayan Kamath | c34f76f | 2011-07-15 11:13:10 +0100 | [diff] [blame] | 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}. |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 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"; |
Charles Chen | aaf842e | 2009-06-25 11:59:29 -0700 | [diff] [blame] | 132 | |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 133 | /** |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 134 | * Interface definition of a callback to be invoked indicating the completion of the |
| 135 | * TextToSpeech engine initialization. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 136 | */ |
| 137 | public interface OnInitListener { |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 138 | /** |
| 139 | * Called to signal the completion of the TextToSpeech engine initialization. |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 140 | * |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 141 | * @param status {@link TextToSpeech#SUCCESS} or {@link TextToSpeech#ERROR}. |
| 142 | */ |
Jean-Michel Trivi | 91bf30a | 2009-06-11 14:35:48 -0700 | [diff] [blame] | 143 | public void onInit(int status); |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 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}). |
Przemyslaw Szczepaniak | 647abce | 2013-01-09 11:52:03 +0000 | [diff] [blame] | 150 | * |
| 151 | * @deprecated Use {@link UtteranceProgressListener} instead. |
Charles Chen | 78c9d0d | 2009-07-13 16:22:41 -0700 | [diff] [blame] | 152 | */ |
Przemyslaw Szczepaniak | 647abce | 2013-01-09 11:52:03 +0000 | [diff] [blame] | 153 | @Deprecated |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 154 | public interface OnUtteranceCompletedListener { |
| 155 | /** |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 156 | * Called when an utterance has been synthesized. |
| 157 | * |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 158 | * @param utteranceId the identifier of the utterance. |
Charles Chen | 60dd360 | 2010-01-07 18:56:24 -0800 | [diff] [blame] | 159 | */ |
| 160 | public void onUtteranceCompleted(String utteranceId); |
Charles Chen | 78c9d0d | 2009-07-13 16:22:41 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Charles Chen | 78c9d0d | 2009-07-13 16:22:41 -0700 | [diff] [blame] | 163 | /** |
Narayan Kamath | 748af66 | 2011-10-31 14:20:01 +0000 | [diff] [blame] | 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> |
Jean-Michel Trivi | d146874 | 2009-06-18 14:41:41 -0700 | [diff] [blame] | 183 | */ |
| 184 | public class Engine { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 185 | |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 186 | /** |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 187 | * Default speech rate. |
| 188 | * @hide |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 189 | */ |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 190 | public static final int DEFAULT_RATE = 100; |
| 191 | |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 192 | /** |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 193 | * Default pitch. |
| 194 | * @hide |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 195 | */ |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 196 | public static final int DEFAULT_PITCH = 100; |
| 197 | |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 198 | /** |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 199 | * Default volume. |
| 200 | * @hide |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 201 | */ |
Jean-Michel Trivi | 9d2d26a | 2011-01-05 16:08:21 -0800 | [diff] [blame] | 202 | public static final float DEFAULT_VOLUME = 1.0f; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 203 | |
Jean-Michel Trivi | 9d2d26a | 2011-01-05 16:08:21 -0800 | [diff] [blame] | 204 | /** |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 205 | * Default pan (centered). |
| 206 | * @hide |
Jean-Michel Trivi | 9d2d26a | 2011-01-05 16:08:21 -0800 | [diff] [blame] | 207 | */ |
| 208 | public static final float DEFAULT_PAN = 0.0f; |
Jean-Michel Trivi | 9d2d26a | 2011-01-05 16:08:21 -0800 | [diff] [blame] | 209 | |
| 210 | /** |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 211 | * Default value for {@link Settings.Secure#TTS_USE_DEFAULTS}. |
| 212 | * @hide |
Jean-Michel Trivi | 9d2d26a | 2011-01-05 16:08:21 -0800 | [diff] [blame] | 213 | */ |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 214 | public static final int USE_DEFAULTS = 0; // false |
Jean-Michel Trivi | d146874 | 2009-06-18 14:41:41 -0700 | [diff] [blame] | 215 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 216 | /** |
| 217 | * Package name of the default TTS engine. |
| 218 | * |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 219 | * @hide |
Narayan Kamath | 22302fb | 2011-06-10 14:22:07 +0100 | [diff] [blame] | 220 | * @deprecated No longer in use, the default engine is determined by |
Narayan Kamath | b9db1fb | 2011-07-14 15:30:36 +0100 | [diff] [blame] | 221 | * the sort order defined in {@link TtsEngines}. Note that |
Narayan Kamath | 22302fb | 2011-06-10 14:22:07 +0100 | [diff] [blame] | 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. |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 225 | */ |
Narayan Kamath | 22302fb | 2011-06-10 14:22:07 +0100 | [diff] [blame] | 226 | @Deprecated |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 227 | public static final String DEFAULT_ENGINE = "com.svox.pico"; |
| 228 | |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 229 | /** |
| 230 | * Default audio stream used when playing synthesized speech. |
| 231 | */ |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 232 | public static final int DEFAULT_STREAM = AudioManager.STREAM_MUSIC; |
Jean-Michel Trivi | a981013 | 2009-07-10 12:08:59 -0700 | [diff] [blame] | 233 | |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 234 | /** |
| 235 | * Indicates success when checking the installation status of the resources used by the |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 236 | * TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent. |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 237 | */ |
Jean-Michel Trivi | d146874 | 2009-06-18 14:41:41 -0700 | [diff] [blame] | 238 | public static final int CHECK_VOICE_DATA_PASS = 1; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 239 | |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 240 | /** |
| 241 | * Indicates failure when checking the installation status of the resources used by the |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 242 | * TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent. |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 243 | */ |
Jean-Michel Trivi | d146874 | 2009-06-18 14:41:41 -0700 | [diff] [blame] | 244 | public static final int CHECK_VOICE_DATA_FAIL = 0; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 245 | |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 246 | /** |
| 247 | * Indicates erroneous data when checking the installation status of the resources used by |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 248 | * the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent. |
Przemyslaw Szczepaniak | 647abce | 2013-01-09 11:52:03 +0000 | [diff] [blame] | 249 | * |
| 250 | * @deprecated Use CHECK_VOICE_DATA_FAIL instead. |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 251 | */ |
Przemyslaw Szczepaniak | 647abce | 2013-01-09 11:52:03 +0000 | [diff] [blame] | 252 | @Deprecated |
Jean-Michel Trivi | d146874 | 2009-06-18 14:41:41 -0700 | [diff] [blame] | 253 | public static final int CHECK_VOICE_DATA_BAD_DATA = -1; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 254 | |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 255 | /** |
| 256 | * Indicates missing resources when checking the installation status of the resources used |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 257 | * by the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent. |
Przemyslaw Szczepaniak | 647abce | 2013-01-09 11:52:03 +0000 | [diff] [blame] | 258 | * |
| 259 | * @deprecated Use CHECK_VOICE_DATA_FAIL instead. |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 260 | */ |
Przemyslaw Szczepaniak | 647abce | 2013-01-09 11:52:03 +0000 | [diff] [blame] | 261 | @Deprecated |
Jean-Michel Trivi | d146874 | 2009-06-18 14:41:41 -0700 | [diff] [blame] | 262 | public static final int CHECK_VOICE_DATA_MISSING_DATA = -2; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 263 | |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 264 | /** |
| 265 | * Indicates missing storage volume when checking the installation status of the resources |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 266 | * used by the TextToSpeech engine with the {@link #ACTION_CHECK_TTS_DATA} intent. |
Przemyslaw Szczepaniak | 647abce | 2013-01-09 11:52:03 +0000 | [diff] [blame] | 267 | * |
| 268 | * @deprecated Use CHECK_VOICE_DATA_FAIL instead. |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 269 | */ |
Przemyslaw Szczepaniak | 647abce | 2013-01-09 11:52:03 +0000 | [diff] [blame] | 270 | @Deprecated |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 271 | public static final int CHECK_VOICE_DATA_MISSING_VOLUME = -3; |
Charles Chen | 99a0fee | 2009-07-02 10:41:51 -0700 | [diff] [blame] | 272 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 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. |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 278 | */ |
| 279 | @SdkConstant(SdkConstantType.SERVICE_ACTION) |
| 280 | public static final String INTENT_ACTION_TTS_SERVICE = |
| 281 | "android.intent.action.TTS_SERVICE"; |
| 282 | |
Narayan Kamath | 4d03462 | 2011-06-16 12:43:46 +0100 | [diff] [blame] | 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 | |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 291 | // intents to ask engine to install data or check its data |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 292 | /** |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 293 | * Activity Action: Triggers the platform TextToSpeech engine to |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 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 | */ |
Jean-Michel Trivi | 9f5eadd | 2009-08-14 15:44:31 -0700 | [diff] [blame] | 301 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 302 | public static final String ACTION_INSTALL_TTS_DATA = |
| 303 | "android.speech.tts.engine.INSTALL_TTS_DATA"; |
| 304 | |
| 305 | /** |
Przemyslaw Szczepaniak | 647abce | 2013-01-09 11:52:03 +0000 | [diff] [blame] | 306 | * Broadcast Action: broadcast to signal the change in the list of available |
| 307 | * languages or/and their features. |
Jean-Michel Trivi | 77a5d39 | 2009-08-07 17:26:36 -0700 | [diff] [blame] | 308 | */ |
| 309 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 310 | public static final String ACTION_TTS_DATA_INSTALLED = |
| 311 | "android.speech.tts.engine.TTS_DATA_INSTALLED"; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 312 | |
Jean-Michel Trivi | 77a5d39 | 2009-08-07 17:26:36 -0700 | [diff] [blame] | 313 | /** |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 314 | * Activity Action: Starts the activity from the platform TextToSpeech |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 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}, |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 320 | * <p> Moreover, the data received in the activity result will contain the following |
| 321 | * fields: |
| 322 | * <ul> |
Przemyslaw Szczepaniak | 647abce | 2013-01-09 11:52:03 +0000 | [diff] [blame] | 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> |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 330 | * </ul> |
| 331 | */ |
Jean-Michel Trivi | 9f5eadd | 2009-08-14 15:44:31 -0700 | [diff] [blame] | 332 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 333 | public static final String ACTION_CHECK_TTS_DATA = |
| 334 | "android.speech.tts.engine.CHECK_TTS_DATA"; |
| 335 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 336 | /** |
Przemyslaw Szczepaniak | 647abce | 2013-01-09 11:52:03 +0000 | [diff] [blame] | 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> |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 344 | * |
Przemyslaw Szczepaniak | 647abce | 2013-01-09 11:52:03 +0000 | [diff] [blame] | 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> |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 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 | |
Przemyslaw Szczepaniak | 647abce | 2013-01-09 11:52:03 +0000 | [diff] [blame] | 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 | |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 361 | // extras for a TTS engine's check data activity |
| 362 | /** |
Przemyslaw Szczepaniak | 647abce | 2013-01-09 11:52:03 +0000 | [diff] [blame] | 363 | * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent result where |
Charles Chen | 76d9c3c | 2010-02-11 16:44:45 -0800 | [diff] [blame] | 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"; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 369 | |
Charles Chen | 76d9c3c | 2010-02-11 16:44:45 -0800 | [diff] [blame] | 370 | /** |
Przemyslaw Szczepaniak | 647abce | 2013-01-09 11:52:03 +0000 | [diff] [blame] | 371 | * Extra information received with the {@link #ACTION_CHECK_TTS_DATA} intent result where |
Charles Chen | 76d9c3c | 2010-02-11 16:44:45 -0800 | [diff] [blame] | 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"; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 377 | |
Charles Chen | 76d9c3c | 2010-02-11 16:44:45 -0800 | [diff] [blame] | 378 | /** |
Przemyslaw Szczepaniak | 647abce | 2013-01-09 11:52:03 +0000 | [diff] [blame] | 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 | /** |
Charles Chen | 76d9c3c | 2010-02-11 16:44:45 -0800 | [diff] [blame] | 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"). |
Przemyslaw Szczepaniak | 647abce | 2013-01-09 11:52:03 +0000 | [diff] [blame] | 417 | * |
| 418 | * @deprecated Redundant functionality, checking for existence of specific sets of voice |
| 419 | * data can be done on client side. |
Charles Chen | 76d9c3c | 2010-02-11 16:44:45 -0800 | [diff] [blame] | 420 | */ |
Przemyslaw Szczepaniak | 647abce | 2013-01-09 11:52:03 +0000 | [diff] [blame] | 421 | @Deprecated |
Charles Chen | 76d9c3c | 2010-02-11 16:44:45 -0800 | [diff] [blame] | 422 | public static final String EXTRA_CHECK_VOICE_DATA_FOR = "checkVoiceDataFor"; |
Charles Chen | 99a0fee | 2009-07-02 10:41:51 -0700 | [diff] [blame] | 423 | |
Jean-Michel Trivi | 77a5d39 | 2009-08-07 17:26:36 -0700 | [diff] [blame] | 424 | // extras for a TTS engine's data installation |
| 425 | /** |
Przemyslaw Szczepaniak | 647abce | 2013-01-09 11:52:03 +0000 | [diff] [blame] | 426 | * Extra information received with the {@link #ACTION_TTS_DATA_INSTALLED} intent result. |
Jean-Michel Trivi | 9f5eadd | 2009-08-14 15:44:31 -0700 | [diff] [blame] | 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}. |
Przemyslaw Szczepaniak | 647abce | 2013-01-09 11:52:03 +0000 | [diff] [blame] | 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. |
Jean-Michel Trivi | 77a5d39 | 2009-08-07 17:26:36 -0700 | [diff] [blame] | 434 | */ |
Przemyslaw Szczepaniak | 647abce | 2013-01-09 11:52:03 +0000 | [diff] [blame] | 435 | @Deprecated |
Jean-Michel Trivi | 77a5d39 | 2009-08-07 17:26:36 -0700 | [diff] [blame] | 436 | public static final String EXTRA_TTS_DATA_INSTALLED = "dataInstalled"; |
| 437 | |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 438 | // keys for the parameters passed with speak commands. Hidden keys are used internally |
| 439 | // to maintain engine state for each TextToSpeech instance. |
| 440 | /** |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 441 | * @hide |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 442 | */ |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 443 | public static final String KEY_PARAM_RATE = "rate"; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 444 | |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 445 | /** |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 446 | * @hide |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 447 | */ |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 448 | public static final String KEY_PARAM_LANGUAGE = "language"; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 449 | |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 450 | /** |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 451 | * @hide |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 452 | */ |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 453 | public static final String KEY_PARAM_COUNTRY = "country"; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 454 | |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 455 | /** |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 456 | * @hide |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 457 | */ |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 458 | public static final String KEY_PARAM_VARIANT = "variant"; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 459 | |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 460 | /** |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 461 | * @hide |
Charles Chen | 60dd360 | 2010-01-07 18:56:24 -0800 | [diff] [blame] | 462 | */ |
| 463 | public static final String KEY_PARAM_ENGINE = "engine"; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 464 | |
Charles Chen | 60dd360 | 2010-01-07 18:56:24 -0800 | [diff] [blame] | 465 | /** |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 466 | * @hide |
Charles Chen | 1a2712c | 2010-04-01 17:16:28 -0700 | [diff] [blame] | 467 | */ |
| 468 | public static final String KEY_PARAM_PITCH = "pitch"; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 469 | |
Charles Chen | 1a2712c | 2010-04-01 17:16:28 -0700 | [diff] [blame] | 470 | /** |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 471 | * Parameter key to specify the audio stream type to be used when speaking text |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 472 | * or playing back a file. The value should be one of the STREAM_ constants |
| 473 | * defined in {@link AudioManager}. |
| 474 | * |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 475 | * @see TextToSpeech#speak(String, int, HashMap) |
| 476 | * @see TextToSpeech#playEarcon(String, int, HashMap) |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 477 | */ |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 478 | public static final String KEY_PARAM_STREAM = "streamType"; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 479 | |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 480 | /** |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 481 | * Parameter key to identify an utterance in the |
| 482 | * {@link TextToSpeech.OnUtteranceCompletedListener} after text has been |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 483 | * spoken, a file has been played back or a silence duration has elapsed. |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 484 | * |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 485 | * @see TextToSpeech#speak(String, int, HashMap) |
| 486 | * @see TextToSpeech#playEarcon(String, int, HashMap) |
| 487 | * @see TextToSpeech#synthesizeToFile(String, HashMap, String) |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 488 | */ |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 489 | public static final String KEY_PARAM_UTTERANCE_ID = "utteranceId"; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 490 | |
Jean-Michel Trivi | 9d2d26a | 2011-01-05 16:08:21 -0800 | [diff] [blame] | 491 | /** |
Jean-Michel Trivi | 9d2d26a | 2011-01-05 16:08:21 -0800 | [diff] [blame] | 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 |
Jean-Michel Trivi | 9011ec8 | 2011-01-11 11:55:00 -0800 | [diff] [blame] | 494 | * where 0 is silence, and 1 is the maximum volume (the default behavior). |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 495 | * |
Jean-Michel Trivi | 9d2d26a | 2011-01-05 16:08:21 -0800 | [diff] [blame] | 496 | * @see TextToSpeech#speak(String, int, HashMap) |
| 497 | * @see TextToSpeech#playEarcon(String, int, HashMap) |
| 498 | */ |
| 499 | public static final String KEY_PARAM_VOLUME = "volume"; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 500 | |
Jean-Michel Trivi | 9d2d26a | 2011-01-05 16:08:21 -0800 | [diff] [blame] | 501 | /** |
Jean-Michel Trivi | 9d2d26a | 2011-01-05 16:08:21 -0800 | [diff] [blame] | 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, |
Jean-Michel Trivi | 9011ec8 | 2011-01-11 11:55:00 -0800 | [diff] [blame] | 504 | * 0 to center (the default behavior), and +1 to hard-right. |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 505 | * |
Jean-Michel Trivi | 9d2d26a | 2011-01-05 16:08:21 -0800 | [diff] [blame] | 506 | * @see TextToSpeech#speak(String, int, HashMap) |
| 507 | * @see TextToSpeech#playEarcon(String, int, HashMap) |
| 508 | */ |
| 509 | public static final String KEY_PARAM_PAN = "pan"; |
Jean-Michel Trivi | 62253a3 | 2009-07-14 13:55:19 -0700 | [diff] [blame] | 510 | |
Narayan Kamath | 748af66 | 2011-10-31 14:20:01 +0000 | [diff] [blame] | 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). |
Przemyslaw Szczepaniak | 647abce | 2013-01-09 11:52:03 +0000 | [diff] [blame] | 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) |
Narayan Kamath | 748af66 | 2011-10-31 14:20:01 +0000 | [diff] [blame] | 532 | */ |
| 533 | public static final String KEY_FEATURE_EMBEDDED_SYNTHESIS = "embeddedTts"; |
Jean-Michel Trivi | d146874 | 2009-06-18 14:41:41 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 536 | private final Context mContext; |
Przemyslaw Szczepaniak | 9c4012b | 2013-01-16 17:24:44 +0000 | [diff] [blame] | 537 | private Connection mConnectingServiceConnection; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 538 | private Connection mServiceConnection; |
| 539 | private OnInitListener mInitListener; |
Narayan Kamath | a57f238 | 2011-10-04 17:20:09 +0100 | [diff] [blame] | 540 | // Written from an unspecified application thread, read from |
| 541 | // a binder thread. |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 542 | private volatile UtteranceProgressListener mUtteranceProgressListener; |
Jean-Michel Trivi | 91bf30a | 2009-06-11 14:35:48 -0700 | [diff] [blame] | 543 | private final Object mStartLock = new Object(); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 544 | |
| 545 | private String mRequestedEngine; |
Narayan Kamath | c60aad2 | 2011-12-06 16:40:03 +0000 | [diff] [blame] | 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; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 551 | private final Map<String, Uri> mEarcons; |
| 552 | private final Map<String, Uri> mUtterances; |
| 553 | private final Bundle mParams = new Bundle(); |
Narayan Kamath | d3ee2fa | 2011-06-10 16:19:52 +0100 | [diff] [blame] | 554 | private final TtsEngines mEnginesHelper; |
Narayan Kamath | 68e2af5 | 2011-11-28 17:10:04 +0000 | [diff] [blame] | 555 | private final String mPackageName; |
Narayan Kamath | bd2492e | 2011-06-27 18:35:47 +0100 | [diff] [blame] | 556 | private volatile String mCurrentEngine = null; |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 557 | |
| 558 | /** |
Bjorn Bringert | 4bbca88 | 2011-04-19 18:45:25 +0100 | [diff] [blame] | 559 | * The constructor for the TextToSpeech class, using the default TTS engine. |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 560 | * This will also initialize the associated TextToSpeech engine if it isn't already running. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 561 | * |
| 562 | * @param context |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 563 | * The context this instance is running in. |
Jean-Michel Trivi | 91bf30a | 2009-06-11 14:35:48 -0700 | [diff] [blame] | 564 | * @param listener |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 565 | * The {@link TextToSpeech.OnInitListener} that will be called when the |
Przemyslaw Szczepaniak | e86a936a | 2013-04-30 16:41:14 +0100 | [diff] [blame] | 566 | * TextToSpeech engine has initialized. In a case of a failure the listener |
| 567 | * may be called immediately, before TextToSpeech instance is fully constructed. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 568 | */ |
Jean-Michel Trivi | 91bf30a | 2009-06-11 14:35:48 -0700 | [diff] [blame] | 569 | public TextToSpeech(Context context, OnInitListener listener) { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 570 | this(context, listener, null); |
| 571 | } |
| 572 | |
| 573 | /** |
Bjorn Bringert | 4bbca88 | 2011-04-19 18:45:25 +0100 | [diff] [blame] | 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 |
Przemyslaw Szczepaniak | e86a936a | 2013-04-30 16:41:14 +0100 | [diff] [blame] | 581 | * TextToSpeech engine has initialized. In a case of a failure the listener |
| 582 | * may be called immediately, before TextToSpeech instance is fully constructed. |
Bjorn Bringert | 4bbca88 | 2011-04-19 18:45:25 +0100 | [diff] [blame] | 583 | * @param engine Package name of the TTS engine to use. |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 584 | */ |
| 585 | public TextToSpeech(Context context, OnInitListener listener, String engine) { |
Narayan Kamath | c60aad2 | 2011-12-06 16:40:03 +0000 | [diff] [blame] | 586 | this(context, listener, engine, null, true); |
Narayan Kamath | 68e2af5 | 2011-11-28 17:10:04 +0000 | [diff] [blame] | 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, |
Narayan Kamath | c60aad2 | 2011-12-06 16:40:03 +0000 | [diff] [blame] | 596 | String packageName, boolean useFallback) { |
Jean-Michel Trivi | 91bf30a | 2009-06-11 14:35:48 -0700 | [diff] [blame] | 597 | mContext = context; |
| 598 | mInitListener = listener; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 599 | mRequestedEngine = engine; |
Narayan Kamath | c60aad2 | 2011-12-06 16:40:03 +0000 | [diff] [blame] | 600 | mUseFallback = useFallback; |
Jean-Michel Trivi | 87c9684 | 2009-06-25 14:29:15 -0700 | [diff] [blame] | 601 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 602 | mEarcons = new HashMap<String, Uri>(); |
| 603 | mUtterances = new HashMap<String, Uri>(); |
Narayan Kamath | 68e2af5 | 2011-11-28 17:10:04 +0000 | [diff] [blame] | 604 | mUtteranceProgressListener = null; |
Jean-Michel Trivi | a981013 | 2009-07-10 12:08:59 -0700 | [diff] [blame] | 605 | |
Narayan Kamath | d3ee2fa | 2011-06-10 16:19:52 +0100 | [diff] [blame] | 606 | mEnginesHelper = new TtsEngines(mContext); |
Narayan Kamath | 68e2af5 | 2011-11-28 17:10:04 +0000 | [diff] [blame] | 607 | if (packageName != null) { |
| 608 | mPackageName = packageName; |
| 609 | } else { |
| 610 | mPackageName = mContext.getPackageName(); |
| 611 | } |
Jean-Michel Trivi | a8518c1 | 2009-06-10 17:33:34 -0700 | [diff] [blame] | 612 | initTts(); |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Przemyslaw Szczepaniak | 9c4012b | 2013-01-16 17:24:44 +0000 | [diff] [blame] | 615 | private <R> R runActionNoReconnect(Action<R> action, R errorResult, String method, |
| 616 | boolean onlyEstablishedConnection) { |
| 617 | return runAction(action, errorResult, method, false, onlyEstablishedConnection); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | private <R> R runAction(Action<R> action, R errorResult, String method) { |
Przemyslaw Szczepaniak | 9c4012b | 2013-01-16 17:24:44 +0000 | [diff] [blame] | 621 | return runAction(action, errorResult, method, true, true); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Przemyslaw Szczepaniak | 9c4012b | 2013-01-16 17:24:44 +0000 | [diff] [blame] | 624 | private <R> R runAction(Action<R> action, R errorResult, String method, |
| 625 | boolean reconnect, boolean onlyEstablishedConnection) { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 626 | synchronized (mStartLock) { |
| 627 | if (mServiceConnection == null) { |
| 628 | Log.w(TAG, method + " failed: not bound to TTS engine"); |
| 629 | return errorResult; |
| 630 | } |
Przemyslaw Szczepaniak | 9c4012b | 2013-01-16 17:24:44 +0000 | [diff] [blame] | 631 | return mServiceConnection.runAction(action, errorResult, method, reconnect, |
| 632 | onlyEstablishedConnection); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
| 636 | private int initTts() { |
Narayan Kamath | b9db1fb | 2011-07-14 15:30:36 +0100 | [diff] [blame] | 637 | // Step 1: Try connecting to the engine that was requested. |
Narayan Kamath | c60aad2 | 2011-12-06 16:40:03 +0000 | [diff] [blame] | 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; |
Narayan Kamath | b9db1fb | 2011-07-14 15:30:36 +0100 | [diff] [blame] | 653 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Narayan Kamath | b9db1fb | 2011-07-14 15:30:36 +0100 | [diff] [blame] | 656 | // Step 2: Try connecting to the user's default engine. |
| 657 | final String defaultEngine = getDefaultEngine(); |
| 658 | if (defaultEngine != null && !defaultEngine.equals(mRequestedEngine)) { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 659 | if (connectToEngine(defaultEngine)) { |
Narayan Kamath | b9db1fb | 2011-07-14 15:30:36 +0100 | [diff] [blame] | 660 | mCurrentEngine = defaultEngine; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 661 | return SUCCESS; |
| 662 | } |
| 663 | } |
| 664 | |
Narayan Kamath | b9db1fb | 2011-07-14 15:30:36 +0100 | [diff] [blame] | 665 | // Step 3: Try connecting to the highest ranked engine in the |
| 666 | // system. |
Narayan Kamath | d3ee2fa | 2011-06-10 16:19:52 +0100 | [diff] [blame] | 667 | final String highestRanked = mEnginesHelper.getHighestRankedEngineName(); |
Narayan Kamath | b9db1fb | 2011-07-14 15:30:36 +0100 | [diff] [blame] | 668 | if (highestRanked != null && !highestRanked.equals(mRequestedEngine) && |
| 669 | !highestRanked.equals(defaultEngine)) { |
Narayan Kamath | 22302fb | 2011-06-10 14:22:07 +0100 | [diff] [blame] | 670 | if (connectToEngine(highestRanked)) { |
Narayan Kamath | b9db1fb | 2011-07-14 15:30:36 +0100 | [diff] [blame] | 671 | mCurrentEngine = highestRanked; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 672 | return SUCCESS; |
| 673 | } |
| 674 | } |
| 675 | |
Narayan Kamath | b9db1fb | 2011-07-14 15:30:36 +0100 | [diff] [blame] | 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; |
Narayan Kamath | 0e20fe5 | 2011-06-14 12:39:55 +0100 | [diff] [blame] | 681 | dispatchOnInit(ERROR); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 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); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 692 | return false; |
| 693 | } else { |
Narayan Kamath | 0e20fe5 | 2011-06-14 12:39:55 +0100 | [diff] [blame] | 694 | Log.i(TAG, "Sucessfully bound to " + engine); |
Przemyslaw Szczepaniak | 9c4012b | 2013-01-16 17:24:44 +0000 | [diff] [blame] | 695 | mConnectingServiceConnection = connection; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 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 | } |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 708 | |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 709 | private IBinder getCallerIdentity() { |
| 710 | return mServiceConnection.getCallerIdentity(); |
| 711 | } |
| 712 | |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 713 | /** |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 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. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 717 | */ |
| 718 | public void shutdown() { |
Przemyslaw Szczepaniak | 9c4012b | 2013-01-16 17:24:44 +0000 | [diff] [blame] | 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 |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 729 | runActionNoReconnect(new Action<Void>() { |
| 730 | @Override |
| 731 | public Void run(ITextToSpeechService service) throws RemoteException { |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 732 | service.setCallback(getCallerIdentity(), null); |
| 733 | service.stop(getCallerIdentity()); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 734 | mServiceConnection.disconnect(); |
Narayan Kamath | 90e5650 | 2011-07-18 14:43:07 +0100 | [diff] [blame] | 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; |
Narayan Kamath | bd2492e | 2011-06-27 18:35:47 +0100 | [diff] [blame] | 743 | mCurrentEngine = null; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 744 | return null; |
| 745 | } |
Przemyslaw Szczepaniak | 9c4012b | 2013-01-16 17:24:44 +0000 | [diff] [blame] | 746 | }, null, "shutdown", false); |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 749 | /** |
| 750 | * Adds a mapping between a string of text and a sound resource in a |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 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. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 754 | * |
| 755 | * @param text |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 756 | * The string of text. Example: <code>"south_south_east"</code> |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 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 |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 772 | * Example: <code>R.raw.south_south_east</code> |
Charles Chen | 5c22f51 | 2009-06-29 15:52:47 -0700 | [diff] [blame] | 773 | * |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 774 | * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 775 | */ |
Charles Chen | 5c22f51 | 2009-06-29 15:52:47 -0700 | [diff] [blame] | 776 | public int addSpeech(String text, String packagename, int resourceId) { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 777 | synchronized (mStartLock) { |
| 778 | mUtterances.put(text, makeResourceUri(packagename, resourceId)); |
| 779 | return SUCCESS; |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 783 | /** |
| 784 | * Adds a mapping between a string of text and a sound file. Using this, it |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 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. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 789 | * |
| 790 | * @param text |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 791 | * The string of text. Example: <code>"south_south_east"</code> |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 792 | * @param filename |
| 793 | * The full path to the sound file (for example: |
| 794 | * "/sdcard/mysounds/hello.wav") |
Charles Chen | 5c22f51 | 2009-06-29 15:52:47 -0700 | [diff] [blame] | 795 | * |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 796 | * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 797 | */ |
Charles Chen | 5c22f51 | 2009-06-29 15:52:47 -0700 | [diff] [blame] | 798 | public int addSpeech(String text, String filename) { |
Jean-Michel Trivi | 91bf30a | 2009-06-11 14:35:48 -0700 | [diff] [blame] | 799 | synchronized (mStartLock) { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 800 | mUtterances.put(text, Uri.parse(filename)); |
| 801 | return SUCCESS; |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 802 | } |
| 803 | } |
| 804 | |
| 805 | |
| 806 | /** |
Charles Chen | 904dfa5 | 2009-07-15 10:44:41 -0700 | [diff] [blame] | 807 | * Adds a mapping between a string of text and a sound resource in a |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 808 | * package. Use this to add custom earcons. |
Charles Chen | 904dfa5 | 2009-07-15 10:44:41 -0700 | [diff] [blame] | 809 | * |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 810 | * @see #playEarcon(String, int, HashMap) |
Charles Chen | 904dfa5 | 2009-07-15 10:44:41 -0700 | [diff] [blame] | 811 | * |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 812 | * @param earcon The name of the earcon. |
| 813 | * Example: <code>"[tick]"</code><br/> |
Charles Chen | 904dfa5 | 2009-07-15 10:44:41 -0700 | [diff] [blame] | 814 | * |
| 815 | * @param packagename |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 816 | * the package name of the application that contains the |
| 817 | * resource. This can for instance be the package name of your own application. |
Charles Chen | 904dfa5 | 2009-07-15 10:44:41 -0700 | [diff] [blame] | 818 | * Example: <b>"com.google.marvin.compass"</b><br/> |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 819 | * The package name can be found in the AndroidManifest.xml of |
| 820 | * the application containing the resource. |
Charles Chen | 904dfa5 | 2009-07-15 10:44:41 -0700 | [diff] [blame] | 821 | * <p> |
| 822 | * <code><manifest xmlns:android="..." |
| 823 | * package="<b>com.google.marvin.compass</b>"></code> |
| 824 | * </p> |
| 825 | * |
| 826 | * @param resourceId |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 827 | * Example: <code>R.raw.tick_snd</code> |
Charles Chen | 904dfa5 | 2009-07-15 10:44:41 -0700 | [diff] [blame] | 828 | * |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 829 | * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. |
Charles Chen | 904dfa5 | 2009-07-15 10:44:41 -0700 | [diff] [blame] | 830 | */ |
| 831 | public int addEarcon(String earcon, String packagename, int resourceId) { |
| 832 | synchronized(mStartLock) { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 833 | mEarcons.put(earcon, makeResourceUri(packagename, resourceId)); |
| 834 | return SUCCESS; |
Charles Chen | 904dfa5 | 2009-07-15 10:44:41 -0700 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | |
Charles Chen | 904dfa5 | 2009-07-15 10:44:41 -0700 | [diff] [blame] | 838 | /** |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 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) |
Charles Chen | 904dfa5 | 2009-07-15 10:44:41 -0700 | [diff] [blame] | 843 | * |
| 844 | * @param earcon |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 845 | * The name of the earcon. |
| 846 | * Example: <code>"[tick]"</code> |
Charles Chen | 904dfa5 | 2009-07-15 10:44:41 -0700 | [diff] [blame] | 847 | * @param filename |
| 848 | * The full path to the sound file (for example: |
| 849 | * "/sdcard/mysounds/tick.wav") |
| 850 | * |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 851 | * @return Code indicating success or failure. See {@link #ERROR} and {@link #SUCCESS}. |
Charles Chen | 904dfa5 | 2009-07-15 10:44:41 -0700 | [diff] [blame] | 852 | */ |
| 853 | public int addEarcon(String earcon, String filename) { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 854 | synchronized(mStartLock) { |
| 855 | mEarcons.put(earcon, Uri.parse(filename)); |
| 856 | return SUCCESS; |
Charles Chen | 904dfa5 | 2009-07-15 10:44:41 -0700 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 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 | } |
Charles Chen | 904dfa5 | 2009-07-15 10:44:41 -0700 | [diff] [blame] | 867 | |
| 868 | /** |
Przemyslaw Szczepaniak | 442f178 | 2012-09-26 14:21:26 +0100 | [diff] [blame] | 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. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 876 | * |
Przemyslaw Szczepaniak | 2d940bc | 2012-11-19 12:22:59 +0000 | [diff] [blame] | 877 | * @param text The string of text to be spoken. No longer than |
| 878 | * {@link #getMaxSpeechInputLength()} characters. |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 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}. |
Narayan Kamath | b956f37 | 2011-05-16 16:51:44 +0100 | [diff] [blame] | 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. |
Charles Chen | 5c22f51 | 2009-06-29 15:52:47 -0700 | [diff] [blame] | 890 | * |
Przemyslaw Szczepaniak | 442f178 | 2012-09-26 14:21:26 +0100 | [diff] [blame] | 891 | * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the speak operation. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 892 | */ |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 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) { |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 899 | return service.playAudio(getCallerIdentity(), utteranceUri, queueMode, |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 900 | getParams(params)); |
| 901 | } else { |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 902 | return service.speak(getCallerIdentity(), text, queueMode, getParams(params)); |
Jean-Michel Trivi | a981013 | 2009-07-10 12:08:59 -0700 | [diff] [blame] | 903 | } |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 904 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 905 | }, ERROR, "speak"); |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 908 | /** |
| 909 | * Plays the earcon using the specified queueing mode and parameters. |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 910 | * The earcon must already have been added with {@link #addEarcon(String, String)} or |
| 911 | * {@link #addEarcon(String, String, int)}. |
Przemyslaw Szczepaniak | 442f178 | 2012-09-26 14:21:26 +0100 | [diff] [blame] | 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. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 918 | * |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 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}, |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 924 | * {@link Engine#KEY_PARAM_UTTERANCE_ID}. |
Narayan Kamath | b956f37 | 2011-05-16 16:51:44 +0100 | [diff] [blame] | 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. |
Charles Chen | 5c22f51 | 2009-06-29 15:52:47 -0700 | [diff] [blame] | 929 | * |
Przemyslaw Szczepaniak | 442f178 | 2012-09-26 14:21:26 +0100 | [diff] [blame] | 930 | * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the playEarcon operation. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 931 | */ |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 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; |
Jean-Michel Trivi | a981013 | 2009-07-10 12:08:59 -0700 | [diff] [blame] | 940 | } |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 941 | return service.playAudio(getCallerIdentity(), earconUri, queueMode, |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 942 | getParams(params)); |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 943 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 944 | }, ERROR, "playEarcon"); |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 945 | } |
Jean-Michel Trivi | 679d728 | 2009-06-16 15:36:28 -0700 | [diff] [blame] | 946 | |
Charles Chen | 5c22f51 | 2009-06-29 15:52:47 -0700 | [diff] [blame] | 947 | /** |
| 948 | * Plays silence for the specified amount of time using the specified |
| 949 | * queue mode. |
Przemyslaw Szczepaniak | 442f178 | 2012-09-26 14:21:26 +0100 | [diff] [blame] | 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. |
Charles Chen | 5c22f51 | 2009-06-29 15:52:47 -0700 | [diff] [blame] | 956 | * |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 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: |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 961 | * {@link Engine#KEY_PARAM_UTTERANCE_ID}. |
Narayan Kamath | b956f37 | 2011-05-16 16:51:44 +0100 | [diff] [blame] | 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. |
Charles Chen | 5c22f51 | 2009-06-29 15:52:47 -0700 | [diff] [blame] | 966 | * |
Przemyslaw Szczepaniak | 442f178 | 2012-09-26 14:21:26 +0100 | [diff] [blame] | 967 | * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the playSilence operation. |
Charles Chen | 5c22f51 | 2009-06-29 15:52:47 -0700 | [diff] [blame] | 968 | */ |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 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 { |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 974 | return service.playSilence(getCallerIdentity(), durationInMs, queueMode, |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 975 | params == null ? null : params.get(Engine.KEY_PARAM_UTTERANCE_ID)); |
Charles Chen | f032bc7 | 2009-06-26 14:41:55 -0700 | [diff] [blame] | 976 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 977 | }, ERROR, "playSilence"); |
Jean-Michel Trivi | a8518c1 | 2009-06-10 17:33:34 -0700 | [diff] [blame] | 978 | } |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 979 | |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 980 | /** |
Narayan Kamath | 748af66 | 2011-10-31 14:20:01 +0000 | [diff] [blame] | 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 | */ |
| 994 | public Set<String> getFeatures(final Locale locale) { |
| 995 | return runAction(new Action<Set<String>>() { |
| 996 | @Override |
| 997 | public Set<String> run(ITextToSpeechService service) throws RemoteException { |
Przemyslaw Szczepaniak | 58f1665 | 2013-09-20 15:29:25 +0100 | [diff] [blame] | 998 | String[] features = null; |
| 999 | try { |
| 1000 | features = service.getFeaturesForLanguage( |
Narayan Kamath | 748af66 | 2011-10-31 14:20:01 +0000 | [diff] [blame] | 1001 | locale.getISO3Language(), locale.getISO3Country(), locale.getVariant()); |
Przemyslaw Szczepaniak | 58f1665 | 2013-09-20 15:29:25 +0100 | [diff] [blame] | 1002 | } catch(MissingResourceException e) { |
| 1003 | Log.w(TAG, "Couldn't retrieve 3 letter ISO 639-2/T language and/or ISO 3166 " + |
| 1004 | "country code for locale: " + locale, e); |
| 1005 | return null; |
| 1006 | } |
| 1007 | |
Narayan Kamath | 748af66 | 2011-10-31 14:20:01 +0000 | [diff] [blame] | 1008 | if (features != null) { |
| 1009 | final Set<String> featureSet = new HashSet<String>(); |
| 1010 | Collections.addAll(featureSet, features); |
| 1011 | return featureSet; |
| 1012 | } |
| 1013 | return null; |
| 1014 | } |
| 1015 | }, null, "getFeatures"); |
| 1016 | } |
| 1017 | |
| 1018 | /** |
Narayan Kamath | c34f76f | 2011-07-15 11:13:10 +0100 | [diff] [blame] | 1019 | * Checks whether the TTS engine is busy speaking. Note that a speech item is |
| 1020 | * considered complete once it's audio data has been sent to the audio mixer, or |
| 1021 | * written to a file. There might be a finite lag between this point, and when |
| 1022 | * the audio hardware completes playback. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1023 | * |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1024 | * @return {@code true} if the TTS engine is speaking. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1025 | */ |
| 1026 | public boolean isSpeaking() { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1027 | return runAction(new Action<Boolean>() { |
| 1028 | @Override |
| 1029 | public Boolean run(ITextToSpeechService service) throws RemoteException { |
| 1030 | return service.isSpeaking(); |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1031 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1032 | }, false, "isSpeaking"); |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1035 | /** |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 1036 | * Interrupts the current utterance (whether played or rendered to file) and discards other |
| 1037 | * utterances in the queue. |
Charles Chen | 5c22f51 | 2009-06-29 15:52:47 -0700 | [diff] [blame] | 1038 | * |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1039 | * @return {@link #ERROR} or {@link #SUCCESS}. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1040 | */ |
Charles Chen | 5c22f51 | 2009-06-29 15:52:47 -0700 | [diff] [blame] | 1041 | public int stop() { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1042 | return runAction(new Action<Integer>() { |
| 1043 | @Override |
| 1044 | public Integer run(ITextToSpeechService service) throws RemoteException { |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 1045 | return service.stop(getCallerIdentity()); |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1046 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1047 | }, ERROR, "stop"); |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1050 | /** |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1051 | * Sets the speech rate. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1052 | * |
Jean-Michel Trivi | 679d728 | 2009-06-16 15:36:28 -0700 | [diff] [blame] | 1053 | * This has no effect on any pre-recorded speech. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1054 | * |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1055 | * @param speechRate Speech rate. {@code 1.0} is the normal speech rate, |
| 1056 | * lower values slow down the speech ({@code 0.5} is half the normal speech rate), |
| 1057 | * greater values accelerate it ({@code 2.0} is twice the normal speech rate). |
Charles Chen | 5c22f51 | 2009-06-29 15:52:47 -0700 | [diff] [blame] | 1058 | * |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1059 | * @return {@link #ERROR} or {@link #SUCCESS}. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1060 | */ |
Charles Chen | 5c22f51 | 2009-06-29 15:52:47 -0700 | [diff] [blame] | 1061 | public int setSpeechRate(float speechRate) { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1062 | if (speechRate > 0.0f) { |
| 1063 | int intRate = (int)(speechRate * 100); |
| 1064 | if (intRate > 0) { |
| 1065 | synchronized (mStartLock) { |
| 1066 | mParams.putInt(Engine.KEY_PARAM_RATE, intRate); |
Jean-Michel Trivi | 679d728 | 2009-06-16 15:36:28 -0700 | [diff] [blame] | 1067 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1068 | return SUCCESS; |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1069 | } |
| 1070 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1071 | return ERROR; |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1074 | /** |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 1075 | * Sets the speech pitch for the TextToSpeech engine. |
Jean-Michel Trivi | 2ea5349 | 2009-06-23 13:44:40 -0700 | [diff] [blame] | 1076 | * |
Jean-Michel Trivi | 2ea5349 | 2009-06-23 13:44:40 -0700 | [diff] [blame] | 1077 | * This has no effect on any pre-recorded speech. |
| 1078 | * |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1079 | * @param pitch Speech pitch. {@code 1.0} is the normal pitch, |
Jean-Michel Trivi | 2ea5349 | 2009-06-23 13:44:40 -0700 | [diff] [blame] | 1080 | * lower values lower the tone of the synthesized voice, |
| 1081 | * greater values increase it. |
Charles Chen | 5c22f51 | 2009-06-29 15:52:47 -0700 | [diff] [blame] | 1082 | * |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1083 | * @return {@link #ERROR} or {@link #SUCCESS}. |
Jean-Michel Trivi | 2ea5349 | 2009-06-23 13:44:40 -0700 | [diff] [blame] | 1084 | */ |
Charles Chen | 5c22f51 | 2009-06-29 15:52:47 -0700 | [diff] [blame] | 1085 | public int setPitch(float pitch) { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1086 | if (pitch > 0.0f) { |
| 1087 | int intPitch = (int)(pitch * 100); |
| 1088 | if (intPitch > 0) { |
| 1089 | synchronized (mStartLock) { |
| 1090 | mParams.putInt(Engine.KEY_PARAM_PITCH, intPitch); |
Jean-Michel Trivi | 2ea5349 | 2009-06-23 13:44:40 -0700 | [diff] [blame] | 1091 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1092 | return SUCCESS; |
Jean-Michel Trivi | 2ea5349 | 2009-06-23 13:44:40 -0700 | [diff] [blame] | 1093 | } |
| 1094 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1095 | return ERROR; |
Jean-Michel Trivi | 2ea5349 | 2009-06-23 13:44:40 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
Jean-Michel Trivi | 2ea5349 | 2009-06-23 13:44:40 -0700 | [diff] [blame] | 1098 | /** |
Narayan Kamath | bd2492e | 2011-06-27 18:35:47 +0100 | [diff] [blame] | 1099 | * @return the engine currently in use by this TextToSpeech instance. |
| 1100 | * @hide |
| 1101 | */ |
| 1102 | public String getCurrentEngine() { |
| 1103 | return mCurrentEngine; |
| 1104 | } |
| 1105 | |
| 1106 | /** |
Przemyslaw Szczepaniak | b465337 | 2012-12-04 14:57:58 +0000 | [diff] [blame] | 1107 | * Returns a Locale instance describing the language currently being used as the default |
| 1108 | * Text-to-speech language. |
| 1109 | * |
| 1110 | * @return language, country (if any) and variant (if any) used by the client stored in a |
| 1111 | * Locale instance, or {@code null} on error. |
| 1112 | */ |
| 1113 | public Locale getDefaultLanguage() { |
| 1114 | return runAction(new Action<Locale>() { |
| 1115 | @Override |
| 1116 | public Locale run(ITextToSpeechService service) throws RemoteException { |
| 1117 | String[] defaultLanguage = service.getClientDefaultLanguage(); |
| 1118 | |
| 1119 | return new Locale(defaultLanguage[0], defaultLanguage[1], defaultLanguage[2]); |
| 1120 | } |
| 1121 | }, null, "getDefaultLanguage"); |
| 1122 | } |
| 1123 | |
| 1124 | /** |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1125 | * Sets the text-to-speech language. |
| 1126 | * The TTS engine will try to use the closest match to the specified |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 1127 | * language as represented by the Locale, but there is no guarantee that the exact same Locale |
| 1128 | * will be used. Use {@link #isLanguageAvailable(Locale)} to check the level of support |
| 1129 | * before choosing the language to use for the next utterances. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1130 | * |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1131 | * @param loc The locale describing the language to be used. |
Charles Chen | 5c22f51 | 2009-06-29 15:52:47 -0700 | [diff] [blame] | 1132 | * |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1133 | * @return Code indicating the support status for the locale. See {@link #LANG_AVAILABLE}, |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 1134 | * {@link #LANG_COUNTRY_AVAILABLE}, {@link #LANG_COUNTRY_VAR_AVAILABLE}, |
| 1135 | * {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1136 | */ |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1137 | public int setLanguage(final Locale loc) { |
| 1138 | return runAction(new Action<Integer>() { |
| 1139 | @Override |
| 1140 | public Integer run(ITextToSpeechService service) throws RemoteException { |
| 1141 | if (loc == null) { |
| 1142 | return LANG_NOT_SUPPORTED; |
| 1143 | } |
Przemyslaw Szczepaniak | d0b9279 | 2013-05-08 15:26:15 +0100 | [diff] [blame] | 1144 | String language = null, country = null; |
| 1145 | try { |
| 1146 | language = loc.getISO3Language(); |
| 1147 | } catch (MissingResourceException e) { |
| 1148 | Log.w(TAG, "Couldn't retrieve ISO 639-2/T language code for locale: " + loc, e); |
| 1149 | return LANG_NOT_SUPPORTED; |
| 1150 | } |
| 1151 | |
| 1152 | try { |
| 1153 | country = loc.getISO3Country(); |
| 1154 | } catch (MissingResourceException e) { |
| 1155 | Log.w(TAG, "Couldn't retrieve ISO 3166 country code for locale: " + loc, e); |
| 1156 | return LANG_NOT_SUPPORTED; |
| 1157 | } |
| 1158 | |
Charles Chen | 1a2712c | 2010-04-01 17:16:28 -0700 | [diff] [blame] | 1159 | String variant = loc.getVariant(); |
Przemyslaw Szczepaniak | d0b9279 | 2013-05-08 15:26:15 +0100 | [diff] [blame] | 1160 | |
Charles Chen | 1a2712c | 2010-04-01 17:16:28 -0700 | [diff] [blame] | 1161 | // Check if the language, country, variant are available, and cache |
| 1162 | // the available parts. |
| 1163 | // Note that the language is not actually set here, instead it is cached so it |
| 1164 | // will be associated with all upcoming utterances. |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 1165 | |
| 1166 | int result = service.loadLanguage(getCallerIdentity(), language, country, variant); |
Charles Chen | 1a2712c | 2010-04-01 17:16:28 -0700 | [diff] [blame] | 1167 | if (result >= LANG_AVAILABLE){ |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1168 | if (result < LANG_COUNTRY_VAR_AVAILABLE) { |
| 1169 | variant = ""; |
| 1170 | if (result < LANG_COUNTRY_AVAILABLE) { |
| 1171 | country = ""; |
| 1172 | } |
Charles Chen | 1a2712c | 2010-04-01 17:16:28 -0700 | [diff] [blame] | 1173 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1174 | mParams.putString(Engine.KEY_PARAM_LANGUAGE, language); |
| 1175 | mParams.putString(Engine.KEY_PARAM_COUNTRY, country); |
| 1176 | mParams.putString(Engine.KEY_PARAM_VARIANT, variant); |
Charles Chen | 1a2712c | 2010-04-01 17:16:28 -0700 | [diff] [blame] | 1177 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1178 | return result; |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1179 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1180 | }, LANG_NOT_SUPPORTED, "setLanguage"); |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1181 | } |
| 1182 | |
Charles Chen | aaf842e | 2009-06-25 11:59:29 -0700 | [diff] [blame] | 1183 | /** |
Przemyslaw Szczepaniak | 228323e | 2012-11-12 11:04:56 +0000 | [diff] [blame] | 1184 | * Returns a Locale instance describing the language currently being used for synthesis |
| 1185 | * requests sent to the TextToSpeech engine. |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1186 | * |
Przemyslaw Szczepaniak | 228323e | 2012-11-12 11:04:56 +0000 | [diff] [blame] | 1187 | * In Android 4.2 and before (API <= 17) this function returns the language that is currently |
| 1188 | * being used by the TTS engine. That is the last language set by this or any other |
| 1189 | * client by a {@link TextToSpeech#setLanguage} call to the same engine. |
| 1190 | * |
| 1191 | * In Android versions after 4.2 this function returns the language that is currently being |
| 1192 | * used for the synthesis requests sent from this client. That is the last language set |
| 1193 | * by a {@link TextToSpeech#setLanguage} call on this instance. |
| 1194 | * |
| 1195 | * @return language, country (if any) and variant (if any) used by the client stored in a |
| 1196 | * Locale instance, or {@code null} on error. |
Jean-Michel Trivi | ddb0a80 | 2009-06-29 16:38:47 -0700 | [diff] [blame] | 1197 | */ |
| 1198 | public Locale getLanguage() { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1199 | return runAction(new Action<Locale>() { |
| 1200 | @Override |
Przemyslaw Szczepaniak | 228323e | 2012-11-12 11:04:56 +0000 | [diff] [blame] | 1201 | public Locale run(ITextToSpeechService service) { |
| 1202 | /* No service call, but we're accessing mParams, hence need for |
| 1203 | wrapping it as an Action instance */ |
| 1204 | String lang = mParams.getString(Engine.KEY_PARAM_LANGUAGE, ""); |
| 1205 | String country = mParams.getString(Engine.KEY_PARAM_COUNTRY, ""); |
| 1206 | String variant = mParams.getString(Engine.KEY_PARAM_VARIANT, ""); |
| 1207 | return new Locale(lang, country, variant); |
Jean-Michel Trivi | ddb0a80 | 2009-06-29 16:38:47 -0700 | [diff] [blame] | 1208 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1209 | }, null, "getLanguage"); |
Jean-Michel Trivi | ddb0a80 | 2009-06-29 16:38:47 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | /** |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 1213 | * Checks if the specified language as represented by the Locale is available and supported. |
Charles Chen | aaf842e | 2009-06-25 11:59:29 -0700 | [diff] [blame] | 1214 | * |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1215 | * @param loc The Locale describing the language to be used. |
Charles Chen | 5c22f51 | 2009-06-29 15:52:47 -0700 | [diff] [blame] | 1216 | * |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1217 | * @return Code indicating the support status for the locale. See {@link #LANG_AVAILABLE}, |
Jean-Michel Trivi | ed06578 | 2009-07-28 14:31:48 -0700 | [diff] [blame] | 1218 | * {@link #LANG_COUNTRY_AVAILABLE}, {@link #LANG_COUNTRY_VAR_AVAILABLE}, |
| 1219 | * {@link #LANG_MISSING_DATA} and {@link #LANG_NOT_SUPPORTED}. |
Charles Chen | aaf842e | 2009-06-25 11:59:29 -0700 | [diff] [blame] | 1220 | */ |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1221 | public int isLanguageAvailable(final Locale loc) { |
| 1222 | return runAction(new Action<Integer>() { |
| 1223 | @Override |
| 1224 | public Integer run(ITextToSpeechService service) throws RemoteException { |
Przemyslaw Szczepaniak | d0b9279 | 2013-05-08 15:26:15 +0100 | [diff] [blame] | 1225 | String language = null, country = null; |
| 1226 | |
| 1227 | try { |
| 1228 | language = loc.getISO3Language(); |
| 1229 | } catch (MissingResourceException e) { |
| 1230 | Log.w(TAG, "Couldn't retrieve ISO 639-2/T language code for locale: " + loc, e); |
| 1231 | return LANG_NOT_SUPPORTED; |
| 1232 | } |
| 1233 | |
| 1234 | try { |
| 1235 | country = loc.getISO3Country(); |
| 1236 | } catch (MissingResourceException e) { |
| 1237 | Log.w(TAG, "Couldn't retrieve ISO 3166 country code for locale: " + loc, e); |
| 1238 | return LANG_NOT_SUPPORTED; |
| 1239 | } |
| 1240 | |
| 1241 | return service.isLanguageAvailable(language, country, loc.getVariant()); |
Jean-Michel Trivi | ddb0a80 | 2009-06-29 16:38:47 -0700 | [diff] [blame] | 1242 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1243 | }, LANG_NOT_SUPPORTED, "isLanguageAvailable"); |
Charles Chen | aaf842e | 2009-06-25 11:59:29 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1246 | /** |
Charles Chen | d498909 | 2009-06-26 15:32:46 -0700 | [diff] [blame] | 1247 | * Synthesizes the given text to a file using the specified parameters. |
Przemyslaw Szczepaniak | 442f178 | 2012-09-26 14:21:26 +0100 | [diff] [blame] | 1248 | * This method is asynchronous, i.e. the method just adds the request to the queue of TTS |
| 1249 | * requests and then returns. The synthesis might not have finished (or even started!) at the |
| 1250 | * time when this method returns. In order to reliably detect errors during synthesis, |
| 1251 | * we recommend setting an utterance progress listener (see |
| 1252 | * {@link #setOnUtteranceProgressListener}) and using the |
| 1253 | * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1254 | * |
Przemyslaw Szczepaniak | 2d940bc | 2012-11-19 12:22:59 +0000 | [diff] [blame] | 1255 | * @param text The text that should be synthesized. No longer than |
| 1256 | * {@link #getMaxSpeechInputLength()} characters. |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1257 | * @param params Parameters for the request. Can be null. |
| 1258 | * Supported parameter names: |
Jean-Michel Trivi | a9b417e | 2009-09-11 18:34:02 -0700 | [diff] [blame] | 1259 | * {@link Engine#KEY_PARAM_UTTERANCE_ID}. |
Narayan Kamath | b956f37 | 2011-05-16 16:51:44 +0100 | [diff] [blame] | 1260 | * Engine specific parameters may be passed in but the parameter keys |
| 1261 | * must be prefixed by the name of the engine they are intended for. For example |
| 1262 | * the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the |
| 1263 | * engine named "com.svox.pico" if it is being used. |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1264 | * @param filename Absolute file filename to write the generated audio data to.It should be |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1265 | * something like "/sdcard/myappsounds/mysound.wav". |
Charles Chen | 5c22f51 | 2009-06-29 15:52:47 -0700 | [diff] [blame] | 1266 | * |
Przemyslaw Szczepaniak | 442f178 | 2012-09-26 14:21:26 +0100 | [diff] [blame] | 1267 | * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the synthesizeToFile operation. |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1268 | */ |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1269 | public int synthesizeToFile(final String text, final HashMap<String, String> params, |
| 1270 | final String filename) { |
| 1271 | return runAction(new Action<Integer>() { |
| 1272 | @Override |
| 1273 | public Integer run(ITextToSpeechService service) throws RemoteException { |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 1274 | ParcelFileDescriptor fileDescriptor; |
| 1275 | int returnValue; |
| 1276 | try { |
| 1277 | File file = new File(filename); |
| 1278 | if(file.exists() && !file.canWrite()) { |
| 1279 | Log.e(TAG, "Can't write to " + filename); |
| 1280 | return ERROR; |
| 1281 | } |
| 1282 | fileDescriptor = ParcelFileDescriptor.open(file, |
| 1283 | ParcelFileDescriptor.MODE_WRITE_ONLY | |
| 1284 | ParcelFileDescriptor.MODE_CREATE | |
| 1285 | ParcelFileDescriptor.MODE_TRUNCATE); |
| 1286 | returnValue = service.synthesizeToFileDescriptor(getCallerIdentity(), text, |
| 1287 | fileDescriptor, getParams(params)); |
| 1288 | fileDescriptor.close(); |
| 1289 | return returnValue; |
| 1290 | } catch (FileNotFoundException e) { |
| 1291 | Log.e(TAG, "Opening file " + filename + " failed", e); |
| 1292 | return ERROR; |
| 1293 | } catch (IOException e) { |
| 1294 | Log.e(TAG, "Closing file " + filename + " failed", e); |
| 1295 | return ERROR; |
| 1296 | } |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1297 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1298 | }, ERROR, "synthesizeToFile"); |
| 1299 | } |
| 1300 | |
| 1301 | private Bundle getParams(HashMap<String, String> params) { |
| 1302 | if (params != null && !params.isEmpty()) { |
| 1303 | Bundle bundle = new Bundle(mParams); |
| 1304 | copyIntParam(bundle, params, Engine.KEY_PARAM_STREAM); |
| 1305 | copyStringParam(bundle, params, Engine.KEY_PARAM_UTTERANCE_ID); |
| 1306 | copyFloatParam(bundle, params, Engine.KEY_PARAM_VOLUME); |
| 1307 | copyFloatParam(bundle, params, Engine.KEY_PARAM_PAN); |
Narayan Kamath | b956f37 | 2011-05-16 16:51:44 +0100 | [diff] [blame] | 1308 | |
Narayan Kamath | 748af66 | 2011-10-31 14:20:01 +0000 | [diff] [blame] | 1309 | // Copy feature strings defined by the framework. |
| 1310 | copyStringParam(bundle, params, Engine.KEY_FEATURE_NETWORK_SYNTHESIS); |
Narayan Kamath | 6c07a20 | 2011-11-07 14:21:39 +0000 | [diff] [blame] | 1311 | copyStringParam(bundle, params, Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS); |
Narayan Kamath | 748af66 | 2011-10-31 14:20:01 +0000 | [diff] [blame] | 1312 | |
Narayan Kamath | b956f37 | 2011-05-16 16:51:44 +0100 | [diff] [blame] | 1313 | // Copy over all parameters that start with the name of the |
| 1314 | // engine that we are currently connected to. The engine is |
| 1315 | // free to interpret them as it chooses. |
| 1316 | if (!TextUtils.isEmpty(mCurrentEngine)) { |
| 1317 | for (Map.Entry<String, String> entry : params.entrySet()) { |
| 1318 | final String key = entry.getKey(); |
| 1319 | if (key != null && key.startsWith(mCurrentEngine)) { |
| 1320 | bundle.putString(key, entry.getValue()); |
| 1321 | } |
| 1322 | } |
| 1323 | } |
| 1324 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1325 | return bundle; |
| 1326 | } else { |
| 1327 | return mParams; |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | private void copyStringParam(Bundle bundle, HashMap<String, String> params, String key) { |
| 1332 | String value = params.get(key); |
| 1333 | if (value != null) { |
| 1334 | bundle.putString(key, value); |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | private void copyIntParam(Bundle bundle, HashMap<String, String> params, String key) { |
| 1339 | String valueString = params.get(key); |
| 1340 | if (!TextUtils.isEmpty(valueString)) { |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1341 | try { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1342 | int value = Integer.parseInt(valueString); |
| 1343 | bundle.putInt(key, value); |
| 1344 | } catch (NumberFormatException ex) { |
| 1345 | // don't set the value in the bundle |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1346 | } |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1347 | } |
| 1348 | } |
| 1349 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1350 | private void copyFloatParam(Bundle bundle, HashMap<String, String> params, String key) { |
| 1351 | String valueString = params.get(key); |
| 1352 | if (!TextUtils.isEmpty(valueString)) { |
| 1353 | try { |
| 1354 | float value = Float.parseFloat(valueString); |
| 1355 | bundle.putFloat(key, value); |
| 1356 | } catch (NumberFormatException ex) { |
| 1357 | // don't set the value in the bundle |
| 1358 | } |
Jean-Michel Trivi | 9d2d26a | 2011-01-05 16:08:21 -0800 | [diff] [blame] | 1359 | } |
Jean-Michel Trivi | a981013 | 2009-07-10 12:08:59 -0700 | [diff] [blame] | 1360 | } |
| 1361 | |
Charles Chen | 78c9d0d | 2009-07-13 16:22:41 -0700 | [diff] [blame] | 1362 | /** |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1363 | * Sets the listener that will be notified when synthesis of an utterance completes. |
Charles Chen | 78c9d0d | 2009-07-13 16:22:41 -0700 | [diff] [blame] | 1364 | * |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1365 | * @param listener The listener to use. |
Charles Chen | 78c9d0d | 2009-07-13 16:22:41 -0700 | [diff] [blame] | 1366 | * |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1367 | * @return {@link #ERROR} or {@link #SUCCESS}. |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 1368 | * |
| 1369 | * @deprecated Use {@link #setOnUtteranceProgressListener(UtteranceProgressListener)} |
| 1370 | * instead. |
Charles Chen | 78c9d0d | 2009-07-13 16:22:41 -0700 | [diff] [blame] | 1371 | */ |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 1372 | @Deprecated |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1373 | public int setOnUtteranceCompletedListener(final OnUtteranceCompletedListener listener) { |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 1374 | mUtteranceProgressListener = UtteranceProgressListener.from(listener); |
| 1375 | return TextToSpeech.SUCCESS; |
| 1376 | } |
| 1377 | |
| 1378 | /** |
| 1379 | * Sets the listener that will be notified of various events related to the |
| 1380 | * synthesis of a given utterance. |
| 1381 | * |
| 1382 | * See {@link UtteranceProgressListener} and |
| 1383 | * {@link TextToSpeech.Engine#KEY_PARAM_UTTERANCE_ID}. |
| 1384 | * |
| 1385 | * @param listener the listener to use. |
| 1386 | * @return {@link #ERROR} or {@link #SUCCESS} |
| 1387 | */ |
| 1388 | public int setOnUtteranceProgressListener(UtteranceProgressListener listener) { |
| 1389 | mUtteranceProgressListener = listener; |
Narayan Kamath | a57f238 | 2011-10-04 17:20:09 +0100 | [diff] [blame] | 1390 | return TextToSpeech.SUCCESS; |
Charles Chen | 78c9d0d | 2009-07-13 16:22:41 -0700 | [diff] [blame] | 1391 | } |
| 1392 | |
Charles Chen | b4fbe76 | 2009-11-18 16:34:32 -0800 | [diff] [blame] | 1393 | /** |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1394 | * Sets the TTS engine to use. |
Charles Chen | b4fbe76 | 2009-11-18 16:34:32 -0800 | [diff] [blame] | 1395 | * |
Narayan Kamath | 3f0363b | 2011-06-10 09:57:32 +0100 | [diff] [blame] | 1396 | * @deprecated This doesn't inform callers when the TTS engine has been |
| 1397 | * initialized. {@link #TextToSpeech(Context, OnInitListener, String)} |
Narayan Kamath | b9db1fb | 2011-07-14 15:30:36 +0100 | [diff] [blame] | 1398 | * can be used with the appropriate engine name. Also, there is no |
| 1399 | * guarantee that the engine specified will be loaded. If it isn't |
| 1400 | * installed or disabled, the user / system wide defaults will apply. |
Narayan Kamath | 3f0363b | 2011-06-10 09:57:32 +0100 | [diff] [blame] | 1401 | * |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1402 | * @param enginePackageName The package name for the synthesis engine (e.g. "com.svox.pico") |
Charles Chen | b4fbe76 | 2009-11-18 16:34:32 -0800 | [diff] [blame] | 1403 | * |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1404 | * @return {@link #ERROR} or {@link #SUCCESS}. |
Charles Chen | b4fbe76 | 2009-11-18 16:34:32 -0800 | [diff] [blame] | 1405 | */ |
Narayan Kamath | 3f0363b | 2011-06-10 09:57:32 +0100 | [diff] [blame] | 1406 | @Deprecated |
Charles Chen | 60dd360 | 2010-01-07 18:56:24 -0800 | [diff] [blame] | 1407 | public int setEngineByPackageName(String enginePackageName) { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1408 | mRequestedEngine = enginePackageName; |
| 1409 | return initTts(); |
Charles Chen | b4fbe76 | 2009-11-18 16:34:32 -0800 | [diff] [blame] | 1410 | } |
| 1411 | |
Charles Chen | def7185 | 2010-03-25 19:59:50 -0700 | [diff] [blame] | 1412 | /** |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1413 | * Gets the package name of the default speech synthesis engine. |
Charles Chen | def7185 | 2010-03-25 19:59:50 -0700 | [diff] [blame] | 1414 | * |
Narayan Kamath | 22302fb | 2011-06-10 14:22:07 +0100 | [diff] [blame] | 1415 | * @return Package name of the TTS engine that the user has chosen |
| 1416 | * as their default. |
Charles Chen | def7185 | 2010-03-25 19:59:50 -0700 | [diff] [blame] | 1417 | */ |
| 1418 | public String getDefaultEngine() { |
Narayan Kamath | d3ee2fa | 2011-06-10 16:19:52 +0100 | [diff] [blame] | 1419 | return mEnginesHelper.getDefaultEngine(); |
Charles Chen | def7185 | 2010-03-25 19:59:50 -0700 | [diff] [blame] | 1420 | } |
Charles Chen | 4222925 | 2010-03-29 18:30:30 -0700 | [diff] [blame] | 1421 | |
Charles Chen | 4222925 | 2010-03-29 18:30:30 -0700 | [diff] [blame] | 1422 | /** |
Narayan Kamath | c3edf2a | 2011-06-15 12:35:06 +0100 | [diff] [blame] | 1423 | * Checks whether the user's settings should override settings requested |
| 1424 | * by the calling application. As of the Ice cream sandwich release, |
| 1425 | * user settings never forcibly override the app's settings. |
Charles Chen | 4222925 | 2010-03-29 18:30:30 -0700 | [diff] [blame] | 1426 | */ |
| 1427 | public boolean areDefaultsEnforced() { |
Narayan Kamath | c3edf2a | 2011-06-15 12:35:06 +0100 | [diff] [blame] | 1428 | return false; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Bjorn Bringert | 2cad2cc | 2011-03-08 11:53:12 +0000 | [diff] [blame] | 1431 | /** |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1432 | * Gets a list of all installed TTS engines. |
| 1433 | * |
Narayan Kamath | 22302fb | 2011-06-10 14:22:07 +0100 | [diff] [blame] | 1434 | * @return A list of engine info objects. The list can be empty, but never {@code null}. |
Bjorn Bringert | 2cad2cc | 2011-03-08 11:53:12 +0000 | [diff] [blame] | 1435 | */ |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1436 | public List<EngineInfo> getEngines() { |
Narayan Kamath | d3ee2fa | 2011-06-10 16:19:52 +0100 | [diff] [blame] | 1437 | return mEnginesHelper.getEngines(); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | private class Connection implements ServiceConnection { |
| 1441 | private ITextToSpeechService mService; |
Przemyslaw Szczepaniak | 8f3957c | 2012-11-07 14:03:53 +0000 | [diff] [blame] | 1442 | |
Przemyslaw Szczepaniak | 9c4012b | 2013-01-16 17:24:44 +0000 | [diff] [blame] | 1443 | private SetupConnectionAsyncTask mOnSetupConnectionAsyncTask; |
| 1444 | |
| 1445 | private boolean mEstablished; |
Przemyslaw Szczepaniak | 8f3957c | 2012-11-07 14:03:53 +0000 | [diff] [blame] | 1446 | |
Narayan Kamath | a57f238 | 2011-10-04 17:20:09 +0100 | [diff] [blame] | 1447 | private final ITextToSpeechCallback.Stub mCallback = new ITextToSpeechCallback.Stub() { |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1448 | public void onStop(String utteranceId) throws RemoteException { |
| 1449 | // do nothing |
| 1450 | }; |
| 1451 | |
Narayan Kamath | a57f238 | 2011-10-04 17:20:09 +0100 | [diff] [blame] | 1452 | @Override |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1453 | public void onFallback(String utteranceId) throws RemoteException { |
| 1454 | // do nothing |
| 1455 | } |
| 1456 | |
| 1457 | @Override |
| 1458 | public void onSuccess(String utteranceId) { |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 1459 | UtteranceProgressListener listener = mUtteranceProgressListener; |
Narayan Kamath | a57f238 | 2011-10-04 17:20:09 +0100 | [diff] [blame] | 1460 | if (listener != null) { |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 1461 | listener.onDone(utteranceId); |
| 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | @Override |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1466 | public void onError(String utteranceId, int errorCode) { |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 1467 | UtteranceProgressListener listener = mUtteranceProgressListener; |
| 1468 | if (listener != null) { |
| 1469 | listener.onError(utteranceId); |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | @Override |
| 1474 | public void onStart(String utteranceId) { |
| 1475 | UtteranceProgressListener listener = mUtteranceProgressListener; |
| 1476 | if (listener != null) { |
| 1477 | listener.onStart(utteranceId); |
Narayan Kamath | a57f238 | 2011-10-04 17:20:09 +0100 | [diff] [blame] | 1478 | } |
| 1479 | } |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1480 | |
| 1481 | @Override |
| 1482 | public void onVoicesInfoChange(List<VoiceInfo> voicesInfo) throws RemoteException { |
| 1483 | // Ignore it |
| 1484 | } |
Narayan Kamath | a57f238 | 2011-10-04 17:20:09 +0100 | [diff] [blame] | 1485 | }; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1486 | |
Przemyslaw Szczepaniak | 9c4012b | 2013-01-16 17:24:44 +0000 | [diff] [blame] | 1487 | private class SetupConnectionAsyncTask extends AsyncTask<Void, Void, Integer> { |
Przemyslaw Szczepaniak | 8f3957c | 2012-11-07 14:03:53 +0000 | [diff] [blame] | 1488 | private final ComponentName mName; |
Przemyslaw Szczepaniak | 8f3957c | 2012-11-07 14:03:53 +0000 | [diff] [blame] | 1489 | |
Przemyslaw Szczepaniak | 9c4012b | 2013-01-16 17:24:44 +0000 | [diff] [blame] | 1490 | public SetupConnectionAsyncTask(ComponentName name) { |
Przemyslaw Szczepaniak | 8f3957c | 2012-11-07 14:03:53 +0000 | [diff] [blame] | 1491 | mName = name; |
Przemyslaw Szczepaniak | 8f3957c | 2012-11-07 14:03:53 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | @Override |
| 1495 | protected Integer doInBackground(Void... params) { |
| 1496 | synchronized(mStartLock) { |
| 1497 | if (isCancelled()) { |
| 1498 | return null; |
| 1499 | } |
| 1500 | |
| 1501 | try { |
Przemyslaw Szczepaniak | 9c4012b | 2013-01-16 17:24:44 +0000 | [diff] [blame] | 1502 | mService.setCallback(getCallerIdentity(), mCallback); |
| 1503 | String[] defaultLanguage = mService.getClientDefaultLanguage(); |
Przemyslaw Szczepaniak | b465337 | 2012-12-04 14:57:58 +0000 | [diff] [blame] | 1504 | |
| 1505 | mParams.putString(Engine.KEY_PARAM_LANGUAGE, defaultLanguage[0]); |
| 1506 | mParams.putString(Engine.KEY_PARAM_COUNTRY, defaultLanguage[1]); |
| 1507 | mParams.putString(Engine.KEY_PARAM_VARIANT, defaultLanguage[2]); |
| 1508 | |
| 1509 | Log.i(TAG, "Set up connection to " + mName); |
Przemyslaw Szczepaniak | 8f3957c | 2012-11-07 14:03:53 +0000 | [diff] [blame] | 1510 | return SUCCESS; |
| 1511 | } catch (RemoteException re) { |
| 1512 | Log.e(TAG, "Error connecting to service, setCallback() failed"); |
| 1513 | return ERROR; |
| 1514 | } |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | @Override |
| 1519 | protected void onPostExecute(Integer result) { |
| 1520 | synchronized(mStartLock) { |
Przemyslaw Szczepaniak | 9c4012b | 2013-01-16 17:24:44 +0000 | [diff] [blame] | 1521 | if (mOnSetupConnectionAsyncTask == this) { |
| 1522 | mOnSetupConnectionAsyncTask = null; |
Przemyslaw Szczepaniak | 8f3957c | 2012-11-07 14:03:53 +0000 | [diff] [blame] | 1523 | } |
Przemyslaw Szczepaniak | 9c4012b | 2013-01-16 17:24:44 +0000 | [diff] [blame] | 1524 | mEstablished = true; |
Przemyslaw Szczepaniak | 8f3957c | 2012-11-07 14:03:53 +0000 | [diff] [blame] | 1525 | dispatchOnInit(result); |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | |
Przemyslaw Szczepaniak | 091d56c | 2012-08-16 16:34:54 +0100 | [diff] [blame] | 1530 | @Override |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1531 | public void onServiceConnected(ComponentName name, IBinder service) { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1532 | synchronized(mStartLock) { |
Przemyslaw Szczepaniak | 9c4012b | 2013-01-16 17:24:44 +0000 | [diff] [blame] | 1533 | mConnectingServiceConnection = null; |
| 1534 | |
Przemyslaw Szczepaniak | 8f3957c | 2012-11-07 14:03:53 +0000 | [diff] [blame] | 1535 | Log.i(TAG, "Connected to " + name); |
| 1536 | |
Przemyslaw Szczepaniak | 9c4012b | 2013-01-16 17:24:44 +0000 | [diff] [blame] | 1537 | if (mOnSetupConnectionAsyncTask != null) { |
| 1538 | mOnSetupConnectionAsyncTask.cancel(false); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1539 | } |
Przemyslaw Szczepaniak | 8f3957c | 2012-11-07 14:03:53 +0000 | [diff] [blame] | 1540 | |
Przemyslaw Szczepaniak | 9c4012b | 2013-01-16 17:24:44 +0000 | [diff] [blame] | 1541 | mService = ITextToSpeechService.Stub.asInterface(service); |
| 1542 | mServiceConnection = Connection.this; |
| 1543 | |
| 1544 | mEstablished = false; |
| 1545 | mOnSetupConnectionAsyncTask = new SetupConnectionAsyncTask(name); |
| 1546 | mOnSetupConnectionAsyncTask.execute(); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1547 | } |
| 1548 | } |
| 1549 | |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 1550 | public IBinder getCallerIdentity() { |
| 1551 | return mCallback; |
| 1552 | } |
| 1553 | |
Przemyslaw Szczepaniak | 8f3957c | 2012-11-07 14:03:53 +0000 | [diff] [blame] | 1554 | /** |
| 1555 | * Clear connection related fields and cancel mOnServiceConnectedAsyncTask if set. |
| 1556 | * |
Przemyslaw Szczepaniak | 9c4012b | 2013-01-16 17:24:44 +0000 | [diff] [blame] | 1557 | * @return true if we cancel mOnSetupConnectionAsyncTask in progress. |
Przemyslaw Szczepaniak | 8f3957c | 2012-11-07 14:03:53 +0000 | [diff] [blame] | 1558 | */ |
| 1559 | private boolean clearServiceConnection() { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1560 | synchronized(mStartLock) { |
Przemyslaw Szczepaniak | 8f3957c | 2012-11-07 14:03:53 +0000 | [diff] [blame] | 1561 | boolean result = false; |
Przemyslaw Szczepaniak | 9c4012b | 2013-01-16 17:24:44 +0000 | [diff] [blame] | 1562 | if (mOnSetupConnectionAsyncTask != null) { |
| 1563 | result = mOnSetupConnectionAsyncTask.cancel(false); |
| 1564 | mOnSetupConnectionAsyncTask = null; |
Przemyslaw Szczepaniak | 8f3957c | 2012-11-07 14:03:53 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1567 | mService = null; |
| 1568 | // If this is the active connection, clear it |
| 1569 | if (mServiceConnection == this) { |
| 1570 | mServiceConnection = null; |
| 1571 | } |
Przemyslaw Szczepaniak | 8f3957c | 2012-11-07 14:03:53 +0000 | [diff] [blame] | 1572 | return result; |
| 1573 | } |
| 1574 | } |
| 1575 | |
| 1576 | @Override |
| 1577 | public void onServiceDisconnected(ComponentName name) { |
| 1578 | Log.i(TAG, "Asked to disconnect from " + name); |
| 1579 | if (clearServiceConnection()) { |
| 1580 | /* We need to protect against a rare case where engine |
| 1581 | * dies just after successful connection - and we process onServiceDisconnected |
| 1582 | * before OnServiceConnectedAsyncTask.onPostExecute. onServiceDisconnected cancels |
| 1583 | * OnServiceConnectedAsyncTask.onPostExecute and we don't call dispatchOnInit |
| 1584 | * with ERROR as argument. |
| 1585 | */ |
| 1586 | dispatchOnInit(ERROR); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | public void disconnect() { |
| 1591 | mContext.unbindService(this); |
Przemyslaw Szczepaniak | 8f3957c | 2012-11-07 14:03:53 +0000 | [diff] [blame] | 1592 | clearServiceConnection(); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
Przemyslaw Szczepaniak | 9c4012b | 2013-01-16 17:24:44 +0000 | [diff] [blame] | 1595 | public boolean isEstablished() { |
| 1596 | return mService != null && mEstablished; |
| 1597 | } |
| 1598 | |
| 1599 | public <R> R runAction(Action<R> action, R errorResult, String method, |
| 1600 | boolean reconnect, boolean onlyEstablishedConnection) { |
Przemyslaw Szczepaniak | 091d56c | 2012-08-16 16:34:54 +0100 | [diff] [blame] | 1601 | synchronized (mStartLock) { |
| 1602 | try { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1603 | if (mService == null) { |
| 1604 | Log.w(TAG, method + " failed: not connected to TTS engine"); |
| 1605 | return errorResult; |
| 1606 | } |
Fergus Henderson | 1c2df38 | 2013-01-25 14:59:59 +0000 | [diff] [blame] | 1607 | if (onlyEstablishedConnection && !isEstablished()) { |
| 1608 | Log.w(TAG, method + " failed: TTS engine connection not fully set up"); |
Przemyslaw Szczepaniak | 9c4012b | 2013-01-16 17:24:44 +0000 | [diff] [blame] | 1609 | return errorResult; |
| 1610 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1611 | return action.run(mService); |
Przemyslaw Szczepaniak | 091d56c | 2012-08-16 16:34:54 +0100 | [diff] [blame] | 1612 | } catch (RemoteException ex) { |
| 1613 | Log.e(TAG, method + " failed", ex); |
| 1614 | if (reconnect) { |
| 1615 | disconnect(); |
| 1616 | initTts(); |
| 1617 | } |
| 1618 | return errorResult; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1619 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1620 | } |
| 1621 | } |
| 1622 | } |
| 1623 | |
| 1624 | private interface Action<R> { |
| 1625 | R run(ITextToSpeechService service) throws RemoteException; |
| 1626 | } |
| 1627 | |
| 1628 | /** |
| 1629 | * Information about an installed text-to-speech engine. |
| 1630 | * |
| 1631 | * @see TextToSpeech#getEngines |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1632 | */ |
| 1633 | public static class EngineInfo { |
| 1634 | /** |
| 1635 | * Engine package name.. |
| 1636 | */ |
| 1637 | public String name; |
| 1638 | /** |
| 1639 | * Localized label for the engine. |
| 1640 | */ |
| 1641 | public String label; |
| 1642 | /** |
| 1643 | * Icon for the engine. |
| 1644 | */ |
| 1645 | public int icon; |
Narayan Kamath | 22302fb | 2011-06-10 14:22:07 +0100 | [diff] [blame] | 1646 | /** |
| 1647 | * Whether this engine is a part of the system |
| 1648 | * image. |
Narayan Kamath | d3ee2fa | 2011-06-10 16:19:52 +0100 | [diff] [blame] | 1649 | * |
| 1650 | * @hide |
Narayan Kamath | 22302fb | 2011-06-10 14:22:07 +0100 | [diff] [blame] | 1651 | */ |
Narayan Kamath | d3ee2fa | 2011-06-10 16:19:52 +0100 | [diff] [blame] | 1652 | public boolean system; |
Narayan Kamath | 22302fb | 2011-06-10 14:22:07 +0100 | [diff] [blame] | 1653 | /** |
| 1654 | * The priority the engine declares for the the intent filter |
| 1655 | * {@code android.intent.action.TTS_SERVICE} |
Narayan Kamath | d3ee2fa | 2011-06-10 16:19:52 +0100 | [diff] [blame] | 1656 | * |
| 1657 | * @hide |
Narayan Kamath | 22302fb | 2011-06-10 14:22:07 +0100 | [diff] [blame] | 1658 | */ |
Narayan Kamath | d3ee2fa | 2011-06-10 16:19:52 +0100 | [diff] [blame] | 1659 | public int priority; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1660 | |
| 1661 | @Override |
| 1662 | public String toString() { |
| 1663 | return "EngineInfo{name=" + name + "}"; |
| 1664 | } |
| 1665 | |
Bjorn Bringert | 2cad2cc | 2011-03-08 11:53:12 +0000 | [diff] [blame] | 1666 | } |
Narayan Kamath | 22302fb | 2011-06-10 14:22:07 +0100 | [diff] [blame] | 1667 | |
Przemyslaw Szczepaniak | 2d940bc | 2012-11-19 12:22:59 +0000 | [diff] [blame] | 1668 | /** |
| 1669 | * Limit of length of input string passed to speak and synthesizeToFile. |
| 1670 | * |
| 1671 | * @see #speak |
| 1672 | * @see #synthesizeToFile |
| 1673 | */ |
| 1674 | public static int getMaxSpeechInputLength() { |
| 1675 | return 4000; |
| 1676 | } |
Jean-Michel Trivi | e74d507 | 2009-05-26 10:43:08 -0700 | [diff] [blame] | 1677 | } |