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 +++++++++++++++- lib/src/main/cpp/driver_test.cpp | 8 ++-- lib/src/main/java/com/anyun/exam/lib/util/SpeakerCallback.java | 8 ++++ lib/src/main/cpp/master/comm_if.cpp | 2 lib/src/main/java/com/anyun/exam/lib/RemoteService.java | 27 ++++++++++++- 5 files changed, 69 insertions(+), 9 deletions(-) diff --git a/lib/src/main/cpp/driver_test.cpp b/lib/src/main/cpp/driver_test.cpp index bb73b52..94a5278 100644 --- a/lib/src/main/cpp/driver_test.cpp +++ b/lib/src/main/cpp/driver_test.cpp @@ -374,6 +374,8 @@ void SetDummyLightExam(int n, struct dummy_light_exam *cfg) { + DEBUG("鑾峰彇妯℃嫙璺�冪伅鍏夋祴璇曢」鐩�"); + if (TestStart) return; if (DummyLightContent != NULL) { @@ -392,18 +394,15 @@ void StartDriverExam(int start, int type) { bool err = false; + DEBUG("++++++++++++鑰冭瘯鍚姩 start %d type %d+++++++++++++", start, type); if (start == 0) { - DEBUG("-------------缁撴潫鑰冭瘯----------------"); - // CurrExamMapIndex = -1; // TestStart = false; // CommTestStart(false); // MA_SendExamStatus(0, 0); return; } - - DEBUG("+++++++++++++++寮�濮嬭�冭瘯++++++++++++++++++++"); if (MapNum == 0) { err = true; @@ -424,6 +423,7 @@ examFaultIndex = 0; TestStart = true; + TestType = type; CommTestStart(true); if (type == TEST_TYPE_ROAD_DUMMY_LIGHT) { diff --git a/lib/src/main/cpp/master/comm_if.cpp b/lib/src/main/cpp/master/comm_if.cpp index 5b64329..7ba83ed 100644 --- a/lib/src/main/cpp/master/comm_if.cpp +++ b/lib/src/main/cpp/master/comm_if.cpp @@ -791,7 +791,7 @@ const Value &s2 = (*itr)["tts"]; content[n].item = s1.GetInt(); - strcpy(content[n].tts, s1.GetString()); + strcpy(content[n].tts, s2.GetString()); n++; } } diff --git a/lib/src/main/java/com/anyun/exam/lib/RemoteService.java b/lib/src/main/java/com/anyun/exam/lib/RemoteService.java index bc33c8a..2712b49 100644 --- a/lib/src/main/java/com/anyun/exam/lib/RemoteService.java +++ b/lib/src/main/java/com/anyun/exam/lib/RemoteService.java @@ -13,6 +13,7 @@ import com.anyun.exam.lib.util.DESUtil; import com.anyun.exam.lib.util.NetUtils; import com.anyun.exam.lib.util.Speaker; +import com.anyun.exam.lib.util.SpeakerCallback; import androidx.annotation.Nullable; @@ -78,7 +79,7 @@ public void onCreate() { super.onCreate(); Log.i(TAG,"onCreate()"); - speaker = new Speaker(getApplicationContext()); + speaker = new Speaker(/*getApplicationContext()*/this, new TTSCallback()); new Thread(new StartNative()).start(); @@ -93,7 +94,6 @@ } catch (InterruptedException e) { } - ttsInitSucc = true; } } @@ -186,6 +186,29 @@ return h.toString(); } + class TTSCallback implements SpeakerCallback { + @Override + public void PlayInit(boolean ret) { + Log.d(TAG, "TTS寮曟搸鍒濆鍖栨垚鍔�"); + ttsInitSucc = ret; + } + + @Override + public void PlayStart() { + Log.d(TAG, "TTS寮曟搸鎾斁寮�濮�"); + } + + @Override + public void PlayDone() { + Log.d(TAG, "TTS寮曟搸鎾斁缁撴潫"); + } + + @Override + public void PlayError() { + Log.d(TAG, "TTS寮曟搸鎾斁鍑洪敊"); + } + } + // Used to load the 'native-lib' library on application startup. static { System.loadLibrary("native-lib"); 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"); } diff --git a/lib/src/main/java/com/anyun/exam/lib/util/SpeakerCallback.java b/lib/src/main/java/com/anyun/exam/lib/util/SpeakerCallback.java new file mode 100644 index 0000000..faaa68e --- /dev/null +++ b/lib/src/main/java/com/anyun/exam/lib/util/SpeakerCallback.java @@ -0,0 +1,8 @@ +package com.anyun.exam.lib.util; + +public interface SpeakerCallback { + void PlayInit(boolean ret); + void PlayStart(); + void PlayDone(); + void PlayError(); +} -- Gitblit v1.8.0