diff --git a/app/src/main/java/io/legado/app/help/CrashHandler.kt b/app/src/main/java/io/legado/app/help/CrashHandler.kt index e842707d3..d54d09a57 100644 --- a/app/src/main/java/io/legado/app/help/CrashHandler.kt +++ b/app/src/main/java/io/legado/app/help/CrashHandler.kt @@ -8,7 +8,7 @@ import android.os.Handler import android.os.Looper import android.util.Log import android.widget.Toast -import io.legado.app.service.TTSReadAloudService +import io.legado.app.service.help.ReadAloud import io.legado.app.utils.FileUtils import java.io.PrintWriter import java.io.StringWriter @@ -49,7 +49,7 @@ class CrashHandler(val context: Context) : Thread.UncaughtExceptionHandler { * uncaughtException 回调函数 */ override fun uncaughtException(thread: Thread, ex: Throwable) { - TTSReadAloudService.clearTTS() + ReadAloud.stop(context) handleException(ex) mDefaultHandler?.uncaughtException(thread, ex) } diff --git a/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt b/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt index b1d59ceb6..d1341a4c3 100644 --- a/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt @@ -19,20 +19,8 @@ import java.util.* class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener { - companion object { - private var textToSpeech: TextToSpeech? = null - private var ttsInitFinish = false - - fun clearTTS() { - textToSpeech?.let { - it.stop() - it.shutdown() - } - textToSpeech = null - ttsInitFinish = false - } - } - + private var textToSpeech: TextToSpeech? = null + private var ttsInitFinish = false private val ttsUtteranceListener = TTSUtteranceListener() override fun onCreate() { @@ -47,14 +35,23 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener stopSelf() } + override fun onDestroy() { + super.onDestroy() + clearTTS() + } + + @Synchronized private fun initTts() { ttsInitFinish = false textToSpeech = TextToSpeech(this, this) } - override fun onDestroy() { - super.onDestroy() - clearTTS() + @Synchronized + fun clearTTS() { + textToSpeech?.stop() + textToSpeech?.shutdown() + textToSpeech = null + ttsInitFinish = false } override fun onInit(status: Int) {