mirror of
https://github.com/M66B/FairEmail.git
synced 2025-02-24 15:11:03 +00:00
TTS refactoring
This commit is contained in:
parent
ca27f41bf8
commit
cd4b187bd8
2 changed files with 62 additions and 20 deletions
|
@ -621,6 +621,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
||||||
|
|
||||||
liveAccountNetworkState.postDestroy();
|
liveAccountNetworkState.postDestroy();
|
||||||
|
|
||||||
|
TTSHelper.shutdown();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
stopForeground(true);
|
stopForeground(true);
|
||||||
} catch (Throwable ex) {
|
} catch (Throwable ex) {
|
||||||
|
|
|
@ -5,42 +5,82 @@ import android.speech.tts.TextToSpeech;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
public class TTSHelper {
|
public class TTSHelper {
|
||||||
private static boolean initialized;
|
private static Integer status = null;
|
||||||
private static TextToSpeech instance;
|
private static TextToSpeech instance = null;
|
||||||
|
private static List<Runnable> queue = new ArrayList<>();
|
||||||
|
private static final Object lock = new Object();
|
||||||
|
|
||||||
|
// https://developer.android.com/reference/android/speech/tts/TextToSpeech
|
||||||
|
// https://android-developers.googleblog.com/2009/09/introduction-to-text-to-speech-in.html
|
||||||
|
|
||||||
static void speak(
|
static void speak(
|
||||||
@NonNull final Context context,
|
@NonNull final Context context,
|
||||||
@NonNull final String utteranceId,
|
@NonNull final String utteranceId,
|
||||||
@NonNull final String text,
|
@NonNull final String text,
|
||||||
@NonNull final Locale locale) {
|
@NonNull final Locale locale) {
|
||||||
// https://developer.android.com/reference/android/speech/tts/TextToSpeech
|
|
||||||
// https://android-developers.googleblog.com/2009/09/introduction-to-text-to-speech-in.html
|
|
||||||
|
|
||||||
final Runnable speak = new Runnable() {
|
Runnable speak = new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
boolean available = (instance.setLanguage(locale) >= 0);
|
try {
|
||||||
EntityLog.log(context, "TTS queued language=" + locale + " available=" + available + " text=" + text);
|
boolean available = (instance.setLanguage(locale) >= 0);
|
||||||
instance.speak(text, TextToSpeech.QUEUE_ADD, null, utteranceId);
|
EntityLog.log(context, "TTS queued" +
|
||||||
|
" language=" + locale +
|
||||||
|
" available=" + available +
|
||||||
|
" utterance=" + utteranceId +
|
||||||
|
" text=" + text);
|
||||||
|
instance.speak(text, TextToSpeech.QUEUE_ADD, null, utteranceId);
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
Log.e(ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (initialized) {
|
synchronized (lock) {
|
||||||
speak.run();
|
if (status == null) {
|
||||||
return;
|
queue.add(speak);
|
||||||
|
if (instance == null)
|
||||||
|
try {
|
||||||
|
Log.i("TTS init");
|
||||||
|
instance = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
|
||||||
|
@Override
|
||||||
|
public void onInit(int initStatus) {
|
||||||
|
synchronized (lock) {
|
||||||
|
status = initStatus;
|
||||||
|
boolean ok = (status == TextToSpeech.SUCCESS);
|
||||||
|
Log.i("TTS status=" + status + " ok=" + ok + " queued=" + queue.size());
|
||||||
|
if (ok)
|
||||||
|
for (Runnable speak : queue)
|
||||||
|
speak.run();
|
||||||
|
queue.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
Log.e(ex);
|
||||||
|
status = TextToSpeech.ERROR;
|
||||||
|
}
|
||||||
|
} else if (status == TextToSpeech.SUCCESS)
|
||||||
|
speak.run();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
instance = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
|
static void shutdown() {
|
||||||
@Override
|
synchronized (lock) {
|
||||||
public void onInit(int status) {
|
if (instance != null)
|
||||||
initialized = (status == TextToSpeech.SUCCESS);
|
try {
|
||||||
Log.i("TTS status=" + status + " ok=" + initialized);
|
Log.i("TTS shutdown");
|
||||||
if (initialized)
|
instance.shutdown();
|
||||||
speak.run();
|
instance = null;
|
||||||
}
|
} catch (Throwable ex) {
|
||||||
});
|
Log.e(ex);
|
||||||
|
}
|
||||||
|
status = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue