From 759d6028853bfa0e1074fdf292f7eb81c63ac26a Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 25 Aug 2019 19:52:24 +0800 Subject: [PATCH] up --- .../main/java/io/legado/app/help/CrashHandler.kt | 2 ++ .../io/legado/app/service/ReadAloudService.kt | 15 ++++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) 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 98e50fa21..35c29304c 100644 --- a/app/src/main/java/io/legado/app/help/CrashHandler.kt +++ b/app/src/main/java/io/legado/app/help/CrashHandler.kt @@ -8,6 +8,7 @@ import android.os.Handler import android.os.Looper import android.util.Log import android.widget.Toast +import io.legado.app.service.ReadAloudService import java.io.File import java.io.FileOutputStream import java.io.PrintWriter @@ -53,6 +54,7 @@ class CrashHandler : Thread.UncaughtExceptionHandler { * uncaughtException 回调函数 */ override fun uncaughtException(thread: Thread, ex: Throwable) { + ReadAloudService.clearTTS() handleException(ex) mDefaultHandler?.uncaughtException(thread, ex) } diff --git a/app/src/main/java/io/legado/app/service/ReadAloudService.kt b/app/src/main/java/io/legado/app/service/ReadAloudService.kt index cb300d6e3..574641b6a 100644 --- a/app/src/main/java/io/legado/app/service/ReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/ReadAloudService.kt @@ -29,6 +29,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage companion object { val tag: String = ReadAloudService::class.java.simpleName var isRun = false + var textToSpeech: TextToSpeech? = null fun play( context: Context, @@ -69,9 +70,14 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage context.startService(intent) } } + + fun clearTTS() { + textToSpeech?.stop() + textToSpeech?.shutdown() + textToSpeech = null + } } - private var textToSpeech: TextToSpeech? = null private var ttsIsSuccess: Boolean = false private lateinit var audioManager: AudioManager private lateinit var mFocusRequest: AudioFocusRequest @@ -103,12 +109,11 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage } override fun onDestroy() { - textToSpeech?.stop() - textToSpeech?.shutdown() - isRun = false + super.onDestroy() + clearTTS() unregisterReceiver(broadcastReceiver) postEvent(Bus.ALOUD_STATE, Status.STOP) - super.onDestroy() + isRun = false } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {