TextToSpeechServicepublic abstract class TextToSpeechService extends android.app.Service Abstract base class for TTS engine implementations. The following methods
need to be implemented:
- {@link #onIsLanguageAvailable}
- {@link #onLoadLanguage}
- {@link #onGetLanguage}
- {@link #onSynthesizeText}
- {@link #onStop}
The first three deal primarily with language management, and are used to
query the engine for it's support for a given language and indicate to it
that requests in a given language are imminent.
{@link #onSynthesizeText} is central to the engine implementation. The
implementation should synthesize text as per the request parameters and
return synthesized data via the supplied callback. This class and its helpers
will then consume that data, which might mean queuing it for playback or writing
it to a file or similar. All calls to this method will be on a single thread,
which will be different from the main thread of the service. Synthesis must be
synchronous which means the engine must NOT hold on to the callback or call any
methods on it after the method returns.
{@link #onStop} tells the engine that it should stop
all ongoing synthesis, if any. Any pending data from the current synthesis
will be discarded.
{@link #onGetLanguage} is not required as of JELLYBEAN_MR2 (API 18) and later, it is only
called on earlier versions of Android.
API Level 20 adds support for Voice objects. Voices are an abstraction that allow the TTS
service to expose multiple backends for a single locale. Each one of them can have a different
features set. In order to fully take advantage of voices, an engine should implement
the following methods:
- {@link #onGetVoices()}
- {@link #onIsValidVoiceName(String)}
- {@link #onLoadVoice(String)}
- {@link #onGetDefaultVoiceNameFor(String, String, String)}
The first three methods are siblings of the {@link #onGetLanguage},
{@link #onIsLanguageAvailable} and {@link #onLoadLanguage} methods. The last one,
{@link #onGetDefaultVoiceNameFor(String, String, String)} is a link between locale and voice
based methods. Since API level 21 {@link TextToSpeech#setLanguage} is implemented by
calling {@link TextToSpeech#setVoice} with the voice returned by
{@link #onGetDefaultVoiceNameFor(String, String, String)}.
If the client uses a voice instead of a locale, {@link SynthesisRequest} will contain the
requested voice name.
The default implementations of Voice-related methods implement them using the
pre-existing locale-based implementation. |
Fields Summary |
---|
private static final boolean | DBG | private static final String | TAG | private static final String | SYNTH_THREAD_NAME | private SynthHandler | mSynthHandler | private AudioPlaybackHandler | mAudioPlaybackHandler | private TtsEngines | mEngineHelper | private CallbackMap | mCallbacks | private String | mPackageName | private final Object | mVoicesInfoLock | private final ITextToSpeechService.Stub | mBinderBinder returned from {@code #onBind(Intent)}. The methods in this class can be
called called from several different threads. |
Methods Summary |
---|
private int | getDefaultSpeechRate()
return getSecureSettingInt(Settings.Secure.TTS_DEFAULT_RATE, Engine.DEFAULT_RATE);
| private int | getExpectedLanguageAvailableStatus(java.util.Locale locale)
int expectedStatus = TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE;
if (locale.getVariant().isEmpty()) {
if (locale.getCountry().isEmpty()) {
expectedStatus = TextToSpeech.LANG_AVAILABLE;
} else {
expectedStatus = TextToSpeech.LANG_COUNTRY_AVAILABLE;
}
}
return expectedStatus;
| private int | getSecureSettingInt(java.lang.String name, int defaultValue)
return Settings.Secure.getInt(getContentResolver(), name, defaultValue);
| private java.lang.String[] | getSettingsLocale()
final Locale locale = mEngineHelper.getLocalePrefForEngine(mPackageName);
return TtsEngines.toOldLocaleStringFormat(locale);
| public android.os.IBinder | onBind(android.content.Intent intent)
if (TextToSpeech.Engine.INTENT_ACTION_TTS_SERVICE.equals(intent.getAction())) {
return mBinder;
}
return null;
| public void | onCreate()
if (DBG) Log.d(TAG, "onCreate()");
super.onCreate();
SynthThread synthThread = new SynthThread();
synthThread.start();
mSynthHandler = new SynthHandler(synthThread.getLooper());
mAudioPlaybackHandler = new AudioPlaybackHandler();
mAudioPlaybackHandler.start();
mEngineHelper = new TtsEngines(this);
mCallbacks = new CallbackMap();
mPackageName = getApplicationInfo().packageName;
String[] defaultLocale = getSettingsLocale();
// Load default language
onLoadLanguage(defaultLocale[0], defaultLocale[1], defaultLocale[2]);
| public void | onDestroy()
if (DBG) Log.d(TAG, "onDestroy()");
// Tell the synthesizer to stop
mSynthHandler.quit();
// Tell the audio playback thread to stop.
mAudioPlaybackHandler.quit();
// Unregister all callbacks.
mCallbacks.kill();
super.onDestroy();
| public java.lang.String | onGetDefaultVoiceNameFor(java.lang.String lang, java.lang.String country, java.lang.String variant)Return a name of the default voice for a given locale.
This method provides a mapping between locales and available voices. This method is
used in {@link TextToSpeech#setLanguage}, which calls this method and then calls
{@link TextToSpeech#setVoice} with the voice returned by this method.
Also, it's used by {@link TextToSpeech#getDefaultVoice()} to find a default voice for
the default locale.
int localeStatus = onIsLanguageAvailable(lang, country, variant);
Locale iso3Locale = null;
switch (localeStatus) {
case TextToSpeech.LANG_AVAILABLE:
iso3Locale = new Locale(lang);
break;
case TextToSpeech.LANG_COUNTRY_AVAILABLE:
iso3Locale = new Locale(lang, country);
break;
case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
iso3Locale = new Locale(lang, country, variant);
break;
default:
return null;
}
Locale properLocale = TtsEngines.normalizeTTSLocale(iso3Locale);
String voiceName = properLocale.toLanguageTag();
if (onIsValidVoiceName(voiceName) == TextToSpeech.SUCCESS) {
return voiceName;
} else {
return null;
}
| protected java.util.Set | onGetFeaturesForLanguage(java.lang.String lang, java.lang.String country, java.lang.String variant)Queries the service for a set of features supported for a given language.
Can be called on multiple threads.
return null;
| protected abstract java.lang.String[] | onGetLanguage()Returns the language, country and variant currently being used by the TTS engine.
This method will be called only on Android 4.2 and before (API <= 17). In later versions
this method is not called by the Android TTS framework.
Can be called on multiple threads.
| public java.util.List | onGetVoices()Queries the service for a set of supported voices.
Can be called on multiple threads.
The default implementation tries to enumerate all available locales, pass them to
{@link #onIsLanguageAvailable(String, String, String)} and create Voice instances (using
the locale's BCP-47 language tag as the voice name) for the ones that are supported.
Note, that this implementation is suitable only for engines that don't have multiple voices
for a single locale. Also, this implementation won't work with Locales not listed in the
set returned by the {@link Locale#getAvailableLocales()} method.
// Enumerate all locales and check if they are available
ArrayList<Voice> voices = new ArrayList<Voice>();
for (Locale locale : Locale.getAvailableLocales()) {
int expectedStatus = getExpectedLanguageAvailableStatus(locale);
try {
int localeStatus = onIsLanguageAvailable(locale.getISO3Language(),
locale.getISO3Country(), locale.getVariant());
if (localeStatus != expectedStatus) {
continue;
}
} catch (MissingResourceException e) {
// Ignore locale without iso 3 codes
continue;
}
Set<String> features = onGetFeaturesForLanguage(locale.getISO3Language(),
locale.getISO3Country(), locale.getVariant());
voices.add(new Voice(locale.toLanguageTag(), locale, Voice.QUALITY_NORMAL,
Voice.LATENCY_NORMAL, false, features));
}
return voices;
| protected abstract int | onIsLanguageAvailable(java.lang.String lang, java.lang.String country, java.lang.String variant)Checks whether the engine supports a given language.
Can be called on multiple threads.
Its return values HAVE to be consistent with onLoadLanguage.
| public int | onIsValidVoiceName(java.lang.String voiceName)Checks whether the engine supports a voice with a given name.
Can be called on multiple threads.
The default implementation treats the voice name as a language tag, creating a Locale from
the voice name, and passes it to {@link #onIsLanguageAvailable(String, String, String)}.
Locale locale = Locale.forLanguageTag(voiceName);
if (locale == null) {
return TextToSpeech.ERROR;
}
int expectedStatus = getExpectedLanguageAvailableStatus(locale);
try {
int localeStatus = onIsLanguageAvailable(locale.getISO3Language(),
locale.getISO3Country(), locale.getVariant());
if (localeStatus != expectedStatus) {
return TextToSpeech.ERROR;
}
return TextToSpeech.SUCCESS;
} catch (MissingResourceException e) {
return TextToSpeech.ERROR;
}
| protected abstract int | onLoadLanguage(java.lang.String lang, java.lang.String country, java.lang.String variant)Notifies the engine that it should load a speech synthesis language. There is no guarantee
that this method is always called before the language is used for synthesis. It is merely
a hint to the engine that it will probably get some synthesis requests for this language
at some point in the future.
Can be called on multiple threads.
In <= Android 4.2 (<= API 17) can be called on main and service binder threads.
In > Android 4.2 (> API 17) can be called on main and synthesis threads.
| public int | onLoadVoice(java.lang.String voiceName)Notifies the engine that it should load a speech synthesis voice. There is no guarantee
that this method is always called before the voice is used for synthesis. It is merely
a hint to the engine that it will probably get some synthesis requests for this voice
at some point in the future.
Will be called only on synthesis thread.
The default implementation creates a Locale from the voice name (by interpreting the name as
a BCP-47 tag for the locale), and passes it to
{@link #onLoadLanguage(String, String, String)}.
Locale locale = Locale.forLanguageTag(voiceName);
if (locale == null) {
return TextToSpeech.ERROR;
}
int expectedStatus = getExpectedLanguageAvailableStatus(locale);
try {
int localeStatus = onIsLanguageAvailable(locale.getISO3Language(),
locale.getISO3Country(), locale.getVariant());
if (localeStatus != expectedStatus) {
return TextToSpeech.ERROR;
}
onLoadLanguage(locale.getISO3Language(),
locale.getISO3Country(), locale.getVariant());
return TextToSpeech.SUCCESS;
} catch (MissingResourceException e) {
return TextToSpeech.ERROR;
}
| protected abstract void | onStop()Notifies the service that it should stop any in-progress speech synthesis.
This method can be called even if no speech synthesis is currently in progress.
Can be called on multiple threads, but not on the synthesis thread.
| protected abstract void | onSynthesizeText(SynthesisRequest request, SynthesisCallback callback)Tells the service to synthesize speech from the given text. This method
should block until the synthesis is finished. Used for requests from V1
clients ({@link android.speech.tts.TextToSpeech}). Called on the synthesis
thread.
|
|