pull/32/head
kunfei 5 years ago
parent 1e72cd46db
commit 759d602885
  1. 2
      app/src/main/java/io/legado/app/help/CrashHandler.kt
  2. 15
      app/src/main/java/io/legado/app/service/ReadAloudService.kt

@ -8,6 +8,7 @@ import android.os.Handler
import android.os.Looper import android.os.Looper
import android.util.Log import android.util.Log
import android.widget.Toast import android.widget.Toast
import io.legado.app.service.ReadAloudService
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.PrintWriter import java.io.PrintWriter
@ -53,6 +54,7 @@ class CrashHandler : Thread.UncaughtExceptionHandler {
* uncaughtException 回调函数 * uncaughtException 回调函数
*/ */
override fun uncaughtException(thread: Thread, ex: Throwable) { override fun uncaughtException(thread: Thread, ex: Throwable) {
ReadAloudService.clearTTS()
handleException(ex) handleException(ex)
mDefaultHandler?.uncaughtException(thread, ex) mDefaultHandler?.uncaughtException(thread, ex)
} }

@ -29,6 +29,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
companion object { companion object {
val tag: String = ReadAloudService::class.java.simpleName val tag: String = ReadAloudService::class.java.simpleName
var isRun = false var isRun = false
var textToSpeech: TextToSpeech? = null
fun play( fun play(
context: Context, context: Context,
@ -69,9 +70,14 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
context.startService(intent) context.startService(intent)
} }
} }
fun clearTTS() {
textToSpeech?.stop()
textToSpeech?.shutdown()
textToSpeech = null
}
} }
private var textToSpeech: TextToSpeech? = null
private var ttsIsSuccess: Boolean = false private var ttsIsSuccess: Boolean = false
private lateinit var audioManager: AudioManager private lateinit var audioManager: AudioManager
private lateinit var mFocusRequest: AudioFocusRequest private lateinit var mFocusRequest: AudioFocusRequest
@ -103,12 +109,11 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
} }
override fun onDestroy() { override fun onDestroy() {
textToSpeech?.stop() super.onDestroy()
textToSpeech?.shutdown() clearTTS()
isRun = false
unregisterReceiver(broadcastReceiver) unregisterReceiver(broadcastReceiver)
postEvent(Bus.ALOUD_STATE, Status.STOP) postEvent(Bus.ALOUD_STATE, Status.STOP)
super.onDestroy() isRun = false
} }
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

Loading…
Cancel
Save