From 4d625b8f7d4eb22209dba53cf19353d8aa7455ea Mon Sep 17 00:00:00 2001 From: fctom1215 <fctom1215@outlook.com> Date: 星期二, 10 三月 2020 17:47:13 +0800 Subject: [PATCH] 修改TTS播放。 --- lib/src/main/java/com/anyun/exam/lib/util/Speaker.java | 33 +++++++++++++++++++++++++++++++-- 1 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/src/main/java/com/anyun/exam/lib/util/Speaker.java b/lib/src/main/java/com/anyun/exam/lib/util/Speaker.java index 8a7c738..dfbea00 100644 --- a/lib/src/main/java/com/anyun/exam/lib/util/Speaker.java +++ b/lib/src/main/java/com/anyun/exam/lib/util/Speaker.java @@ -2,6 +2,7 @@ import android.content.Context; import android.speech.tts.TextToSpeech; +import android.speech.tts.UtteranceProgressListener; import android.util.Log; import com.anyun.exam.lib.RemoteService; @@ -11,13 +12,16 @@ public class Speaker { private Context context; private TextToSpeech tts; + private SpeakerCallback callback = null; final public String TAG = Speaker.class.getCanonicalName(); - public Speaker(final Context context) { + public Speaker(final Context context, final SpeakerCallback cb) { // TODO Auto-generated constructor stub Log.d(TAG, "Speaker Init..."); this.context = context; + this.callback = cb; + tts = new TextToSpeech(context, new TextToSpeech.OnInitListener() { @Override public void onInit(int status) { @@ -34,13 +38,38 @@ { Log.d(TAG, "TextToSpeech.LANG_NOT_SUPPORTED"); } + if (callback != null) { + callback.PlayInit(true); + } + } + } + }); + + tts.setOnUtteranceProgressListener(new UtteranceProgressListener() { + @Override + public void onStart(String s) { + if (callback != null) { + callback.PlayStart(); + } + } + + @Override + public void onDone(String s) { + if (callback != null) { + callback.PlayDone(); + } + } + + @Override + public void onError(String s) { + if (callback != null) { + callback.PlayError(); } } }); } public void speak(String text) { - Log.d(TAG, "SPEAK"); // tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); tts.speak(text, TextToSpeech.QUEUE_ADD, null, "speech"); } -- Gitblit v1.8.0