зеркало из https://github.com/mozilla/gecko-dev.git
Backed out changeset b60e7b3407b3 (bug 1544584) for Android Checkstyle in /builds/worker/workspace/build/src/mobile/android/geckoview/src/main/java/org/mozilla/gecko/SpeechSynthesisService.java
This commit is contained in:
Родитель
69828aa2fe
Коммит
f8c68f3fd8
|
@ -20,20 +20,13 @@ import java.util.HashSet;
|
|||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class SpeechSynthesisService {
|
||||
private static final String LOGTAG = "GeckoSpeechSynthesis";
|
||||
// Object type is used to make it easier to remove android.speech dependencies using Proguard.
|
||||
private static Object sTTS;
|
||||
private static TextToSpeech sTTS;
|
||||
|
||||
@WrapForJNI(calledFrom = "gecko")
|
||||
public static void initSynth() {
|
||||
initSynthInternal();
|
||||
}
|
||||
|
||||
// Extra internal method to make it easier to remove android.speech dependencies using Proguard.
|
||||
private static void initSynthInternal() {
|
||||
if (sTTS != null) {
|
||||
return;
|
||||
}
|
||||
|
@ -54,20 +47,15 @@ public class SpeechSynthesisService {
|
|||
});
|
||||
}
|
||||
|
||||
private static TextToSpeech getTTS() {
|
||||
return (TextToSpeech) sTTS;
|
||||
}
|
||||
|
||||
private static void registerVoicesByLocale() {
|
||||
ThreadUtils.postToBackgroundThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
TextToSpeech tss = getTTS();
|
||||
Locale defaultLocale = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2
|
||||
? tss.getDefaultLanguage()
|
||||
: tss.getLanguage();
|
||||
? sTTS.getDefaultLanguage()
|
||||
: sTTS.getLanguage();
|
||||
for (Locale locale : getAvailableLanguages()) {
|
||||
final Set<String> features = tss.getFeatures(locale);
|
||||
final Set<String> features = sTTS.getFeatures(locale);
|
||||
boolean isLocal = features != null && features.contains(TextToSpeech.Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS);
|
||||
String localeStr = locale.toString();
|
||||
registerVoice("moz-tts:android:" + localeStr, locale.getDisplayName(), localeStr.replace("_", "-"), !isLocal, defaultLocale == locale);
|
||||
|
@ -81,11 +69,11 @@ public class SpeechSynthesisService {
|
|||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
// While this method was introduced in 21, it seems that it
|
||||
// has not been implemented in the speech service side until 23.
|
||||
return getTTS().getAvailableLanguages();
|
||||
return sTTS.getAvailableLanguages();
|
||||
}
|
||||
Set<Locale> locales = new HashSet<Locale>();
|
||||
for (Locale locale : Locale.getAvailableLocales()) {
|
||||
if (locale.getVariant().isEmpty() && getTTS().isLanguageAvailable(locale) > 0) {
|
||||
if (locale.getVariant().isEmpty() && sTTS.isLanguageAvailable(locale) > 0) {
|
||||
locales.add(locale);
|
||||
}
|
||||
}
|
||||
|
@ -102,29 +90,24 @@ public class SpeechSynthesisService {
|
|||
@WrapForJNI(calledFrom = "gecko")
|
||||
public static String speak(final String uri, final String text, final float rate,
|
||||
final float pitch, final float volume) {
|
||||
AtomicBoolean result = new AtomicBoolean(false);
|
||||
final String utteranceId = UUID.randomUUID().toString();
|
||||
speakInternal(uri, text, rate, pitch, volume, utteranceId, result);
|
||||
return result.get() ? utteranceId : null;
|
||||
}
|
||||
|
||||
// Extra internal method to make it easier to remove android.speech dependencies using Proguard.
|
||||
private static void speakInternal(final String uri, final String text, final float rate,
|
||||
final float pitch, final float volume, String utteranceId, AtomicBoolean result) {
|
||||
if (sTTS == null) {
|
||||
Log.w(LOGTAG, "TextToSpeech is not initialized");
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
HashMap<String, String> params = new HashMap<String, String>();
|
||||
final String utteranceId = UUID.randomUUID().toString();
|
||||
params.put(TextToSpeech.Engine.KEY_PARAM_VOLUME, Float.toString(volume));
|
||||
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceId);
|
||||
TextToSpeech tss = (TextToSpeech) sTTS;
|
||||
tss.setLanguage(new Locale(uri.substring("moz-tts:android:".length())));
|
||||
tss.setSpeechRate(rate);
|
||||
tss.setPitch(pitch);
|
||||
int speakRes = tss.speak(text, TextToSpeech.QUEUE_FLUSH, params);
|
||||
result.set(speakRes == TextToSpeech.SUCCESS);
|
||||
sTTS.setLanguage(new Locale(uri.substring("moz-tts:android:".length())));
|
||||
sTTS.setSpeechRate(rate);
|
||||
sTTS.setPitch(pitch);
|
||||
int result = sTTS.speak(text, TextToSpeech.QUEUE_FLUSH, params);
|
||||
if (result != TextToSpeech.SUCCESS) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return utteranceId;
|
||||
}
|
||||
|
||||
private static void setUtteranceListener() {
|
||||
|
@ -133,7 +116,7 @@ public class SpeechSynthesisService {
|
|||
return;
|
||||
}
|
||||
|
||||
getTTS().setOnUtteranceProgressListener(new UtteranceProgressListener() {
|
||||
sTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
|
||||
@Override
|
||||
public void onDone(final String utteranceId) {
|
||||
dispatchEnd(utteranceId);
|
||||
|
@ -180,17 +163,12 @@ public class SpeechSynthesisService {
|
|||
|
||||
@WrapForJNI(calledFrom = "gecko")
|
||||
public static void stop() {
|
||||
stopInternal();
|
||||
}
|
||||
|
||||
// Extra internal method to make it easier to remove android.speech dependencies using Proguard.
|
||||
private static void stopInternal() {
|
||||
if (sTTS == null) {
|
||||
Log.w(LOGTAG, "TextToSpeech is not initialized");
|
||||
return;
|
||||
}
|
||||
|
||||
getTTS().stop();
|
||||
sTTS.stop();
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
|
||||
// Android M has onStop method. If Android L or above, dispatch
|
||||
// event
|
||||
|
|
Загрузка…
Ссылка в новой задаче