donut snapshot
This commit is contained in:
@@ -27,37 +27,37 @@ import android.content.Intent;
|
||||
* {@hide}
|
||||
*/
|
||||
interface ITts {
|
||||
int setSpeechRate(in int speechRate);
|
||||
int setSpeechRate(in String callingApp, in int speechRate);
|
||||
|
||||
int setPitch(in int pitch);
|
||||
int setPitch(in String callingApp, in int pitch);
|
||||
|
||||
int speak(in String text, in int queueMode, in String[] params);
|
||||
int speak(in String callingApp, in String text, in int queueMode, in String[] params);
|
||||
|
||||
boolean isSpeaking();
|
||||
|
||||
int stop();
|
||||
int stop(in String callingApp);
|
||||
|
||||
void addSpeech(in String text, in String packageName, in int resId);
|
||||
void addSpeech(in String callingApp, in String text, in String packageName, in int resId);
|
||||
|
||||
void addSpeechFile(in String text, in String filename);
|
||||
void addSpeechFile(in String callingApp, in String text, in String filename);
|
||||
|
||||
String[] getLanguage();
|
||||
|
||||
int isLanguageAvailable(in String language, in String country, in String variant);
|
||||
|
||||
int setLanguage(in String language, in String country, in String variant);
|
||||
int setLanguage(in String callingApp, in String language, in String country, in String variant);
|
||||
|
||||
boolean synthesizeToFile(in String text, in String[] params, in String outputDirectory);
|
||||
boolean synthesizeToFile(in String callingApp, in String text, in String[] params, in String outputDirectory);
|
||||
|
||||
int playEarcon(in String earcon, in int queueMode, in String[] params);
|
||||
int playEarcon(in String callingApp, in String earcon, in int queueMode, in String[] params);
|
||||
|
||||
void addEarcon(in String earcon, in String packageName, in int resId);
|
||||
void addEarcon(in String callingApp, in String earcon, in String packageName, in int resId);
|
||||
|
||||
void addEarconFile(in String earcon, in String filename);
|
||||
void addEarconFile(in String callingApp, in String earcon, in String filename);
|
||||
|
||||
void registerCallback(ITtsCallback cb);
|
||||
int registerCallback(in String callingApp, ITtsCallback cb);
|
||||
|
||||
void unregisterCallback(ITtsCallback cb);
|
||||
int unregisterCallback(in String callingApp, ITtsCallback cb);
|
||||
|
||||
int playSilence(in long duration, in int queueMode, in String[] params);
|
||||
int playSilence(in String callingApp, in long duration, in int queueMode, in String[] params);
|
||||
}
|
||||
|
||||
@@ -23,5 +23,5 @@ package android.speech.tts;
|
||||
* {@hide}
|
||||
*/
|
||||
oneway interface ITtsCallback {
|
||||
void markReached(String mark);
|
||||
void utteranceCompleted(String utteranceId);
|
||||
}
|
||||
|
||||
500
core/java/android/speech/tts/TextToSpeech.java
Normal file → Executable file
500
core/java/android/speech/tts/TextToSpeech.java
Normal file → Executable file
@@ -22,6 +22,7 @@ import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.ServiceConnection;
|
||||
import android.media.AudioManager;
|
||||
import android.os.IBinder;
|
||||
import android.os.RemoteException;
|
||||
import android.util.Log;
|
||||
@@ -97,41 +98,147 @@ public class TextToSpeech {
|
||||
public void onInit(int status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the TTS has completed saying something that has an utterance ID set.
|
||||
*
|
||||
* The OnUtteranceCompletedListener must implement the onUtteranceCompleted function.
|
||||
* onUtteranceCompleted is passed a String that is the utteranceId given in
|
||||
* the original speak call.
|
||||
*/
|
||||
public interface OnUtteranceCompletedListener {
|
||||
public void onUtteranceCompleted(String utteranceId);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Internal constants for the TTS functionality
|
||||
*
|
||||
* {@hide}
|
||||
*/
|
||||
public class Engine {
|
||||
// default values for a TTS engine when settings are not found in the provider
|
||||
/**
|
||||
* {@hide}
|
||||
*/
|
||||
public static final int FALLBACK_TTS_DEFAULT_RATE = 100; // 1x
|
||||
/**
|
||||
* {@hide}
|
||||
*/
|
||||
public static final int FALLBACK_TTS_DEFAULT_PITCH = 100;// 1x
|
||||
/**
|
||||
* {@hide}
|
||||
*/
|
||||
public static final int FALLBACK_TTS_USE_DEFAULTS = 0; // false
|
||||
public static final String FALLBACK_TTS_DEFAULT_LANG = "eng";
|
||||
public static final String FALLBACK_TTS_DEFAULT_COUNTRY = "";
|
||||
public static final String FALLBACK_TTS_DEFAULT_VARIANT = "";
|
||||
/**
|
||||
* {@hide}
|
||||
*/
|
||||
public static final String FALLBACK_TTS_DEFAULT_SYNTH = "com.svox.pico";
|
||||
|
||||
// default values for rendering
|
||||
public static final int TTS_DEFAULT_STREAM = AudioManager.STREAM_MUSIC;
|
||||
|
||||
// return codes for a TTS engine's check data activity
|
||||
/**
|
||||
* Indicates success when checking the installation status of the resources used by the
|
||||
* text-to-speech engine with the android.intent.action.CHECK_TTS_DATA intent.
|
||||
*/
|
||||
public static final int CHECK_VOICE_DATA_PASS = 1;
|
||||
/**
|
||||
* Indicates failure when checking the installation status of the resources used by the
|
||||
* text-to-speech engine with the android.intent.action.CHECK_TTS_DATA intent.
|
||||
*/
|
||||
public static final int CHECK_VOICE_DATA_FAIL = 0;
|
||||
/**
|
||||
* Indicates erroneous data when checking the installation status of the resources used by
|
||||
* the text-to-speech engine with the android.intent.action.CHECK_TTS_DATA intent.
|
||||
*/
|
||||
public static final int CHECK_VOICE_DATA_BAD_DATA = -1;
|
||||
/**
|
||||
* Indicates missing resources when checking the installation status of the resources used
|
||||
* by the text-to-speech engine with the android.intent.action.CHECK_TTS_DATA intent.
|
||||
*/
|
||||
public static final int CHECK_VOICE_DATA_MISSING_DATA = -2;
|
||||
public static final int CHECK_VOICE_DATA_MISSING_DATA_NO_SDCARD = -3;
|
||||
/**
|
||||
* Indicates missing storage volume when checking the installation status of the resources
|
||||
* used by the text-to-speech engine with the android.intent.action.CHECK_TTS_DATA intent.
|
||||
*/
|
||||
public static final int CHECK_VOICE_DATA_MISSING_VOLUME = -3;
|
||||
|
||||
// return codes for a TTS engine's check data activity
|
||||
/**
|
||||
* Extra information received with the android.intent.action.CHECK_TTS_DATA intent where
|
||||
* the text-to-speech engine specifies the path to its resources.
|
||||
*/
|
||||
public static final String VOICE_DATA_ROOT_DIRECTORY = "dataRoot";
|
||||
/**
|
||||
* Extra information received with the android.intent.action.CHECK_TTS_DATA intent where
|
||||
* the text-to-speech engine specifies the file names of its resources under the
|
||||
* resource path.
|
||||
*/
|
||||
public static final String VOICE_DATA_FILES = "dataFiles";
|
||||
/**
|
||||
* Extra information received with the android.intent.action.CHECK_TTS_DATA intent where
|
||||
* the text-to-speech engine specifies the locale associated with each resource file.
|
||||
*/
|
||||
public static final String VOICE_DATA_FILES_INFO = "dataFilesInfo";
|
||||
|
||||
// keys for the parameters passed with speak commands
|
||||
// keys for the parameters passed with speak commands. Hidden keys are used internally
|
||||
// to maintain engine state for each TextToSpeech instance.
|
||||
/**
|
||||
* {@hide}
|
||||
*/
|
||||
public static final String TTS_KEY_PARAM_RATE = "rate";
|
||||
/**
|
||||
* {@hide}
|
||||
*/
|
||||
public static final String TTS_KEY_PARAM_LANGUAGE = "language";
|
||||
/**
|
||||
* {@hide}
|
||||
*/
|
||||
public static final String TTS_KEY_PARAM_COUNTRY = "country";
|
||||
/**
|
||||
* {@hide}
|
||||
*/
|
||||
public static final String TTS_KEY_PARAM_VARIANT = "variant";
|
||||
public static final int TTS_PARAM_POSITION_RATE = 0;
|
||||
public static final int TTS_PARAM_POSITION_LANGUAGE = 2;
|
||||
public static final int TTS_PARAM_POSITION_COUNTRY = 4;
|
||||
public static final int TTS_PARAM_POSITION_VARIANT = 6;
|
||||
/**
|
||||
* Parameter key to specify the audio stream type to be used when speaking text
|
||||
* or playing back a file.
|
||||
*/
|
||||
public static final String TTS_KEY_PARAM_STREAM = "streamType";
|
||||
/**
|
||||
* Parameter key to identify an utterance in the completion listener after text has been
|
||||
* spoken, a file has been played back or a silence duration has elapsed.
|
||||
*/
|
||||
public static final String TTS_KEY_PARAM_UTTERANCE_ID = "utteranceId";
|
||||
|
||||
// key positions in the array of cached parameters
|
||||
/**
|
||||
* {@hide}
|
||||
*/
|
||||
protected static final int TTS_PARAM_POSITION_RATE = 0;
|
||||
/**
|
||||
* {@hide}
|
||||
*/
|
||||
protected static final int TTS_PARAM_POSITION_LANGUAGE = 2;
|
||||
/**
|
||||
* {@hide}
|
||||
*/
|
||||
protected static final int TTS_PARAM_POSITION_COUNTRY = 4;
|
||||
/**
|
||||
* {@hide}
|
||||
*/
|
||||
protected static final int TTS_PARAM_POSITION_VARIANT = 6;
|
||||
/**
|
||||
* {@hide}
|
||||
*/
|
||||
protected static final int TTS_PARAM_POSITION_STREAM = 8;
|
||||
/**
|
||||
* {@hide}
|
||||
*/
|
||||
protected static final int TTS_PARAM_POSITION_UTTERANCE_ID = 10;
|
||||
/**
|
||||
* {@hide}
|
||||
*/
|
||||
protected static final int TTS_NB_CACHED_PARAMS = 6;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,7 +247,9 @@ public class TextToSpeech {
|
||||
private ServiceConnection mServiceConnection;
|
||||
|
||||
private ITts mITts = null;
|
||||
private ITtsCallback mITtscallback = null;
|
||||
private Context mContext = null;
|
||||
private String mPackageName = "";
|
||||
private OnInitListener mInitListener = null;
|
||||
private boolean mStarted = false;
|
||||
private final Object mStartLock = new Object();
|
||||
@@ -161,13 +270,16 @@ public class TextToSpeech {
|
||||
*/
|
||||
public TextToSpeech(Context context, OnInitListener listener) {
|
||||
mContext = context;
|
||||
mPackageName = mContext.getPackageName();
|
||||
mInitListener = listener;
|
||||
|
||||
mCachedParams = new String[2*4]; // 4 parameters, store key and value
|
||||
mCachedParams = new String[2*Engine.TTS_NB_CACHED_PARAMS]; // store key and value
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_RATE] = Engine.TTS_KEY_PARAM_RATE;
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_LANGUAGE] = Engine.TTS_KEY_PARAM_LANGUAGE;
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_COUNTRY] = Engine.TTS_KEY_PARAM_COUNTRY;
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_VARIANT] = Engine.TTS_KEY_PARAM_VARIANT;
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_STREAM] = Engine.TTS_KEY_PARAM_STREAM;
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_UTTERANCE_ID] = Engine.TTS_KEY_PARAM_UTTERANCE_ID;
|
||||
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_RATE + 1] =
|
||||
String.valueOf(Engine.FALLBACK_TTS_DEFAULT_RATE);
|
||||
@@ -177,6 +289,10 @@ public class TextToSpeech {
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_COUNTRY + 1] = defaultLoc.getISO3Country();
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_VARIANT + 1] = defaultLoc.getVariant();
|
||||
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_STREAM + 1] =
|
||||
String.valueOf(Engine.TTS_DEFAULT_STREAM);
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_UTTERANCE_ID + 1] = "";
|
||||
|
||||
initTts();
|
||||
}
|
||||
|
||||
@@ -263,18 +379,24 @@ public class TextToSpeech {
|
||||
return TTS_ERROR;
|
||||
}
|
||||
try {
|
||||
mITts.addSpeech(text, packagename, resourceId);
|
||||
mITts.addSpeech(mPackageName, text, packagename, resourceId);
|
||||
return TTS_SUCCESS;
|
||||
} catch (RemoteException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - addSpeech", "RemoteException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (NullPointerException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - addSpeech", "NullPointerException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (IllegalStateException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - addSpeech", "IllegalStateException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
}
|
||||
@@ -301,18 +423,127 @@ public class TextToSpeech {
|
||||
return TTS_ERROR;
|
||||
}
|
||||
try {
|
||||
mITts.addSpeechFile(text, filename);
|
||||
mITts.addSpeechFile(mPackageName, text, filename);
|
||||
return TTS_SUCCESS;
|
||||
} catch (RemoteException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - addSpeech", "RemoteException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (NullPointerException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - addSpeech", "NullPointerException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (IllegalStateException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - addSpeech", "IllegalStateException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
}
|
||||
return TTS_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds a mapping between a string of text and a sound resource in a
|
||||
* package.
|
||||
*
|
||||
* @see #TTS.playEarcon(String earcon, int queueMode, String[] params)
|
||||
*
|
||||
* @param earcon The name of the earcon
|
||||
* Example: <b><code>"[tick]"</code></b><br/>
|
||||
*
|
||||
* @param packagename
|
||||
* Pass the packagename of the application that contains the
|
||||
* resource. If the resource is in your own application (this is
|
||||
* the most common case), then put the packagename of your
|
||||
* application here.<br/>
|
||||
* Example: <b>"com.google.marvin.compass"</b><br/>
|
||||
* The packagename can be found in the AndroidManifest.xml of
|
||||
* your application.
|
||||
* <p>
|
||||
* <code><manifest xmlns:android="..."
|
||||
* package="<b>com.google.marvin.compass</b>"></code>
|
||||
* </p>
|
||||
*
|
||||
* @param resourceId
|
||||
* Example: <b><code>R.raw.tick_snd</code></b>
|
||||
*
|
||||
* @return Code indicating success or failure. See TTS_ERROR and TTS_SUCCESS.
|
||||
*/
|
||||
public int addEarcon(String earcon, String packagename, int resourceId) {
|
||||
synchronized(mStartLock) {
|
||||
if (!mStarted) {
|
||||
return TTS_ERROR;
|
||||
}
|
||||
try {
|
||||
mITts.addEarcon(mPackageName, earcon, packagename, resourceId);
|
||||
return TTS_SUCCESS;
|
||||
} catch (RemoteException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - addEarcon", "RemoteException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (NullPointerException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - addEarcon", "NullPointerException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (IllegalStateException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - addEarcon", "IllegalStateException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
}
|
||||
return TTS_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds a mapping between a string of text and a sound file. Using this, it
|
||||
* is possible to add custom earcons.
|
||||
*
|
||||
* @param earcon
|
||||
* The name of the earcon
|
||||
* @param filename
|
||||
* The full path to the sound file (for example:
|
||||
* "/sdcard/mysounds/tick.wav")
|
||||
*
|
||||
* @return Code indicating success or failure. See TTS_ERROR and TTS_SUCCESS.
|
||||
*/
|
||||
public int addEarcon(String earcon, String filename) {
|
||||
synchronized (mStartLock) {
|
||||
if (!mStarted) {
|
||||
return TTS_ERROR;
|
||||
}
|
||||
try {
|
||||
mITts.addEarconFile(mPackageName, earcon, filename);
|
||||
return TTS_SUCCESS;
|
||||
} catch (RemoteException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - addEarcon", "RemoteException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (NullPointerException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - addEarcon", "NullPointerException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (IllegalStateException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - addEarcon", "IllegalStateException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
}
|
||||
@@ -347,22 +578,38 @@ public class TextToSpeech {
|
||||
return result;
|
||||
}
|
||||
try {
|
||||
// TODO support extra parameters, passing cache of current parameters for the moment
|
||||
result = mITts.speak(text, queueMode, mCachedParams);
|
||||
if ((params != null) && (!params.isEmpty())) {
|
||||
String extra = params.get(Engine.TTS_KEY_PARAM_STREAM);
|
||||
if (extra != null) {
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_STREAM + 1] = extra;
|
||||
}
|
||||
extra = params.get(Engine.TTS_KEY_PARAM_UTTERANCE_ID);
|
||||
if (extra != null) {
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_UTTERANCE_ID + 1] = extra;
|
||||
}
|
||||
}
|
||||
result = mITts.speak(mPackageName, text, queueMode, mCachedParams);
|
||||
} catch (RemoteException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - speak", "RemoteException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (NullPointerException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - speak", "NullPointerException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (IllegalStateException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - speak", "IllegalStateException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} finally {
|
||||
return result;
|
||||
resetCachedParams();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -388,22 +635,38 @@ public class TextToSpeech {
|
||||
return result;
|
||||
}
|
||||
try {
|
||||
// TODO support extra parameters, passing null for the moment
|
||||
result = mITts.playEarcon(earcon, queueMode, null);
|
||||
if ((params != null) && (!params.isEmpty())) {
|
||||
String extra = params.get(Engine.TTS_KEY_PARAM_STREAM);
|
||||
if (extra != null) {
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_STREAM + 1] = extra;
|
||||
}
|
||||
extra = params.get(Engine.TTS_KEY_PARAM_UTTERANCE_ID);
|
||||
if (extra != null) {
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_UTTERANCE_ID + 1] = extra;
|
||||
}
|
||||
}
|
||||
result = mITts.playEarcon(mPackageName, earcon, queueMode, null);
|
||||
} catch (RemoteException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - playEarcon", "RemoteException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (NullPointerException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - playEarcon", "NullPointerException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (IllegalStateException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - playEarcon", "IllegalStateException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} finally {
|
||||
return result;
|
||||
resetCachedParams();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -419,25 +682,36 @@ public class TextToSpeech {
|
||||
*
|
||||
* @return Code indicating success or failure. See TTS_ERROR and TTS_SUCCESS.
|
||||
*/
|
||||
public int playSilence(long durationInMs, int queueMode) {
|
||||
public int playSilence(long durationInMs, int queueMode, HashMap<String,String> params) {
|
||||
synchronized (mStartLock) {
|
||||
int result = TTS_ERROR;
|
||||
if (!mStarted) {
|
||||
return result;
|
||||
}
|
||||
try {
|
||||
// TODO support extra parameters, passing cache of current parameters for the moment
|
||||
result = mITts.playSilence(durationInMs, queueMode, mCachedParams);
|
||||
if ((params != null) && (!params.isEmpty())) {
|
||||
String extra = params.get(Engine.TTS_KEY_PARAM_UTTERANCE_ID);
|
||||
if (extra != null) {
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_UTTERANCE_ID + 1] = extra;
|
||||
}
|
||||
}
|
||||
result = mITts.playSilence(mPackageName, durationInMs, queueMode, mCachedParams);
|
||||
} catch (RemoteException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - playSilence", "RemoteException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (NullPointerException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - playSilence", "NullPointerException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (IllegalStateException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - playSilence", "IllegalStateException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} finally {
|
||||
@@ -461,14 +735,20 @@ public class TextToSpeech {
|
||||
return mITts.isSpeaking();
|
||||
} catch (RemoteException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - isSpeaking", "RemoteException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (NullPointerException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - isSpeaking", "NullPointerException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (IllegalStateException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - isSpeaking", "IllegalStateException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
}
|
||||
@@ -489,17 +769,23 @@ public class TextToSpeech {
|
||||
return result;
|
||||
}
|
||||
try {
|
||||
result = mITts.stop();
|
||||
result = mITts.stop(mPackageName);
|
||||
} catch (RemoteException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - stop", "RemoteException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (NullPointerException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - stop", "NullPointerException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (IllegalStateException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - stop", "IllegalStateException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} finally {
|
||||
@@ -534,10 +820,24 @@ public class TextToSpeech {
|
||||
if (speechRate > 0) {
|
||||
int rate = (int)(speechRate*100);
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_RATE + 1] = String.valueOf(rate);
|
||||
result = mITts.setSpeechRate(rate);
|
||||
// the rate is not set here, instead it is cached so it will be associated
|
||||
// with all upcoming utterances.
|
||||
if (speechRate > 0.0f) {
|
||||
result = TTS_SUCCESS;
|
||||
} else {
|
||||
result = TTS_ERROR;
|
||||
}
|
||||
}
|
||||
} catch (RemoteException e) {
|
||||
} catch (NullPointerException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - setSpeechRate", "NullPointerException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (IllegalStateException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - setSpeechRate", "IllegalStateException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} finally {
|
||||
@@ -570,10 +870,24 @@ public class TextToSpeech {
|
||||
}
|
||||
try {
|
||||
if (pitch > 0) {
|
||||
result = mITts.setPitch((int)(pitch*100));
|
||||
result = mITts.setPitch(mPackageName, (int)(pitch*100));
|
||||
}
|
||||
} catch (RemoteException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - setPitch", "RemoteException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (NullPointerException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - setPitch", "NullPointerException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (IllegalStateException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - setPitch", "IllegalStateException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} finally {
|
||||
@@ -593,7 +907,9 @@ public class TextToSpeech {
|
||||
* @param loc
|
||||
* The locale describing the language to be used.
|
||||
*
|
||||
* @return Code indicating the support status for the locale. See the TTS_LANG_ codes.
|
||||
* @return code indicating the support status for the locale. See {@link #TTS_LANG_AVAILABLE},
|
||||
* {@link #TTS_LANG_COUNTRY_AVAILABLE}, {@link #TTS_LANG_COUNTRY_VAR_AVAILABLE},
|
||||
* {@link #TTS_LANG_MISSING_DATA} and {@link #TTS_LANG_NOT_SUPPORTED}.
|
||||
*/
|
||||
public int setLanguage(Locale loc) {
|
||||
synchronized (mStartLock) {
|
||||
@@ -605,10 +921,27 @@ public class TextToSpeech {
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_LANGUAGE + 1] = loc.getISO3Language();
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_COUNTRY + 1] = loc.getISO3Country();
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_VARIANT + 1] = loc.getVariant();
|
||||
result = mITts.setLanguage(mCachedParams[Engine.TTS_PARAM_POSITION_LANGUAGE + 1],
|
||||
|
||||
result = mITts.setLanguage(mPackageName,
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_LANGUAGE + 1],
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_COUNTRY + 1],
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_VARIANT + 1] );
|
||||
} catch (RemoteException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - setLanguage", "RemoteException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (NullPointerException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - setLanguage", "NullPointerException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (IllegalStateException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - setLanguage", "IllegalStateException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} finally {
|
||||
@@ -637,6 +970,20 @@ public class TextToSpeech {
|
||||
}
|
||||
} catch (RemoteException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - getLanguage", "RemoteException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (NullPointerException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - getLanguage", "NullPointerException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (IllegalStateException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - getLanguage", "IllegalStateException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
}
|
||||
@@ -650,8 +997,9 @@ public class TextToSpeech {
|
||||
* @param loc
|
||||
* The Locale describing the language to be used.
|
||||
*
|
||||
* @return one of TTS_LANG_NOT_SUPPORTED, TTS_LANG_MISSING_DATA, TTS_LANG_AVAILABLE,
|
||||
* TTS_LANG_COUNTRY_AVAILABLE, TTS_LANG_COUNTRY_VAR_AVAILABLE.
|
||||
* @return code indicating the support status for the locale. See {@link #TTS_LANG_AVAILABLE},
|
||||
* {@link #TTS_LANG_COUNTRY_AVAILABLE}, {@link #TTS_LANG_COUNTRY_VAR_AVAILABLE},
|
||||
* {@link #TTS_LANG_MISSING_DATA} and {@link #TTS_LANG_NOT_SUPPORTED}.
|
||||
*/
|
||||
public int isLanguageAvailable(Locale loc) {
|
||||
synchronized (mStartLock) {
|
||||
@@ -664,6 +1012,20 @@ public class TextToSpeech {
|
||||
loc.getISO3Country(), loc.getVariant());
|
||||
} catch (RemoteException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - isLanguageAvailable", "RemoteException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (NullPointerException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - isLanguageAvailable", "NullPointerException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (IllegalStateException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - isLanguageAvailable", "IllegalStateException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} finally {
|
||||
@@ -694,26 +1056,98 @@ public class TextToSpeech {
|
||||
return result;
|
||||
}
|
||||
try {
|
||||
// TODO support extra parameters, passing null for the moment
|
||||
if (mITts.synthesizeToFile(text, null, filename)){
|
||||
if ((params != null) && (!params.isEmpty())) {
|
||||
// no need to read the stream type here
|
||||
String extra = params.get(Engine.TTS_KEY_PARAM_UTTERANCE_ID);
|
||||
if (extra != null) {
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_UTTERANCE_ID + 1] = extra;
|
||||
}
|
||||
}
|
||||
if (mITts.synthesizeToFile(mPackageName, text, mCachedParams, filename)){
|
||||
result = TTS_SUCCESS;
|
||||
}
|
||||
} catch (RemoteException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - synthesizeToFile", "RemoteException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (NullPointerException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - synthesizeToFile", "NullPointerException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (IllegalStateException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - synthesizeToFile", "IllegalStateException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} finally {
|
||||
return result;
|
||||
resetCachedParams();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convenience method to reset the cached parameters to the current default values
|
||||
* if they are not persistent between calls to the service.
|
||||
*/
|
||||
private void resetCachedParams() {
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_STREAM + 1] =
|
||||
String.valueOf(Engine.TTS_DEFAULT_STREAM);
|
||||
mCachedParams[Engine.TTS_PARAM_POSITION_UTTERANCE_ID+ 1] = "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the OnUtteranceCompletedListener that will fire when an utterance completes.
|
||||
*
|
||||
* @param listener
|
||||
* The OnUtteranceCompletedListener
|
||||
*
|
||||
* @return Code indicating success or failure. See TTS_ERROR and TTS_SUCCESS.
|
||||
*/
|
||||
public int setOnUtteranceCompletedListener(
|
||||
final OnUtteranceCompletedListener listener) {
|
||||
synchronized (mStartLock) {
|
||||
int result = TTS_ERROR;
|
||||
if (!mStarted) {
|
||||
return result;
|
||||
}
|
||||
mITtscallback = new ITtsCallback.Stub() {
|
||||
public void utteranceCompleted(String utteranceId) throws RemoteException {
|
||||
if (listener != null) {
|
||||
listener.onUtteranceCompleted(utteranceId);
|
||||
}
|
||||
}
|
||||
};
|
||||
try {
|
||||
result = mITts.registerCallback(mPackageName, mITtscallback);
|
||||
} catch (RemoteException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - registerCallback", "RemoteException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (NullPointerException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - registerCallback", "NullPointerException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} catch (IllegalStateException e) {
|
||||
// TTS died; restart it.
|
||||
Log.e("TextToSpeech.java - registerCallback", "IllegalStateException");
|
||||
e.printStackTrace();
|
||||
mStarted = false;
|
||||
initTts();
|
||||
} finally {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user