From 8da32275424b96f4ee0c3a65a2d7cad2763a3943 Mon Sep 17 00:00:00 2001 From: gedoor Date: Wed, 9 Dec 2020 19:16:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/ui/book/read/TextActionMenu.kt | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt b/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt index 29002b83f..5d0946903 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt @@ -8,6 +8,7 @@ import android.content.pm.ResolveInfo import android.net.Uri import android.os.Build import android.speech.tts.TextToSpeech +import android.speech.tts.UtteranceProgressListener import android.view.LayoutInflater import android.view.Menu import android.view.ViewGroup @@ -40,6 +41,9 @@ class TextActionMenu(private val context: Context, private val callBack: CallBac private val adapter = Adapter(context) private val menu = MenuBuilder(context) private val moreMenu = MenuBuilder(context) + private val ttsListener by lazy { + TTSUtteranceListener() + } init { @SuppressLint("InflateParams") @@ -159,7 +163,9 @@ class TextActionMenu(private val context: Context, private val callBack: CallBac private fun readAloud(text: String) { lastText = text if (textToSpeech == null) { - textToSpeech = TextToSpeech(context, this) + textToSpeech = TextToSpeech(context, this).apply { + setOnUtteranceProgressListener(ttsListener) + } return } if (!ttsInitFinish) return @@ -217,6 +223,22 @@ class TextActionMenu(private val context: Context, private val callBack: CallBac } } + private inner class TTSUtteranceListener : UtteranceProgressListener() { + + override fun onStart(utteranceId: String?) { + + } + + override fun onDone(utteranceId: String?) { + textToSpeech?.shutdown() + textToSpeech = null + } + + override fun onError(utteranceId: String?) { + + } + } + interface CallBack { val selectedText: String