This source file includes following definitions.
- create
- destroy
- isInitialized
- getVoiceCount
- getVoiceName
- getVoiceLanguage
- speak
- stop
- initialize
- nativeVoicesChanged
- nativeOnEndEvent
- nativeOnStartEvent
- nativeOnErrorEvent
package org.chromium.chrome.browser;
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import org.chromium.base.CalledByNative;
import org.chromium.base.ThreadUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
class TtsPlatformImpl {
private static class TtsVoice {
private TtsVoice(String name, String language) {
mName = name;
mLanguage = language;
}
private final String mName;
private final String mLanguage;
}
private long mNativeTtsPlatformImplAndroid;
private final TextToSpeech mTextToSpeech;
private boolean mInitialized;
private ArrayList<TtsVoice> mVoices;
private String mCurrentLanguage;
private TtsPlatformImpl(long nativeTtsPlatformImplAndroid, Context context) {
mInitialized = false;
mNativeTtsPlatformImplAndroid = nativeTtsPlatformImplAndroid;
mTextToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
initialize();
}
});
}
}
});
mTextToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onDone(final String utteranceId) {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mNativeTtsPlatformImplAndroid != 0) {
nativeOnEndEvent(mNativeTtsPlatformImplAndroid,
Integer.parseInt(utteranceId));
}
}
});
}
@Override
public void onError(final String utteranceId) {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mNativeTtsPlatformImplAndroid != 0) {
nativeOnErrorEvent(mNativeTtsPlatformImplAndroid,
Integer.parseInt(utteranceId));
}
}
});
}
@Override
public void onStart(final String utteranceId) {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mNativeTtsPlatformImplAndroid != 0) {
nativeOnStartEvent(mNativeTtsPlatformImplAndroid,
Integer.parseInt(utteranceId));
}
}
});
}
});
}
@CalledByNative
private static TtsPlatformImpl create(int nativeTtsPlatformImplAndroid,
Context context) {
return new TtsPlatformImpl(nativeTtsPlatformImplAndroid, context);
}
@CalledByNative
private void destroy() {
mNativeTtsPlatformImplAndroid = 0;
}
@CalledByNative
private boolean isInitialized() {
return mInitialized;
}
@CalledByNative
private int getVoiceCount() {
assert mInitialized == true;
return mVoices.size();
}
@CalledByNative
private String getVoiceName(int voiceIndex) {
assert mInitialized == true;
return mVoices.get(voiceIndex).mName;
}
@CalledByNative
private String getVoiceLanguage(int voiceIndex) {
assert mInitialized == true;
return mVoices.get(voiceIndex).mLanguage;
}
@CalledByNative
private boolean speak(int utteranceId, String text, String lang,
float rate, float pitch, float volume) {
assert mInitialized == true;
if (!lang.equals(mCurrentLanguage)) {
mTextToSpeech.setLanguage(new Locale(lang));
mCurrentLanguage = lang;
}
mTextToSpeech.setSpeechRate(rate);
mTextToSpeech.setPitch(pitch);
HashMap<String, String> params = new HashMap<String, String>();
if (volume != 1.0) {
params.put(TextToSpeech.Engine.KEY_PARAM_VOLUME, Double.toString(volume));
}
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, Integer.toString(utteranceId));
int result = mTextToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, params);
return (result == TextToSpeech.SUCCESS);
}
@CalledByNative
private void stop() {
assert mInitialized == true;
mTextToSpeech.stop();
}
private void initialize() {
assert mNativeTtsPlatformImplAndroid != 0;
String defaultEngineName = mTextToSpeech.getDefaultEngine();
String engineLabel = defaultEngineName;
for (TextToSpeech.EngineInfo info : mTextToSpeech.getEngines()) {
if (info.name.equals(defaultEngineName)) engineLabel = info.label;
}
Locale[] locales = Locale.getAvailableLocales();
mVoices = new ArrayList<TtsVoice>();
for (int i = 0; i < locales.length; ++i) {
if (!locales[i].getVariant().isEmpty()) continue;
if (mTextToSpeech.isLanguageAvailable(locales[i]) > 0) {
String name = locales[i].getDisplayLanguage();
if (!locales[i].getCountry().isEmpty()) {
name += " " + locales[i].getDisplayCountry();
}
TtsVoice voice = new TtsVoice(name, locales[i].toString());
mVoices.add(voice);
}
}
mInitialized = true;
nativeVoicesChanged(mNativeTtsPlatformImplAndroid);
}
private native void nativeVoicesChanged(long nativeTtsPlatformImplAndroid);
private native void nativeOnEndEvent(long nativeTtsPlatformImplAndroid, int utteranceId);
private native void nativeOnStartEvent(long nativeTtsPlatformImplAndroid, int utteranceId);
private native void nativeOnErrorEvent(long nativeTtsPlatformImplAndroid, int utteranceId);
}