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