修改朗读定时计时方法,handler.removeCallbacks有时候会失效

pull/1275/head
gedoor 3 years ago
parent 4a67405303
commit 84fa663f7b
  1. 50
      app/src/main/java/io/legado/app/service/BaseReadAloudService.kt

@ -29,6 +29,10 @@ import io.legado.app.ui.book.read.page.entities.TextChapter
import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.postEvent
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import splitties.init.appCtx
abstract class BaseReadAloudService : BaseService(),
@ -56,7 +60,7 @@ abstract class BaseReadAloudService : BaseService(),
internal var readAloudNumber: Int = 0
internal var textChapter: TextChapter? = null
internal var pageIndex = 0
private val dsRunnable: Runnable = Runnable { doDs() }
private var dsJob: Job? = null
override fun onCreate() {
super.onCreate()
@ -69,7 +73,7 @@ abstract class BaseReadAloudService : BaseService(),
initBroadcastReceiver()
upNotification()
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING)
startDs()
doDs()
}
override fun onDestroy() {
@ -149,7 +153,7 @@ abstract class BaseReadAloudService : BaseService(),
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PAUSED)
postEvent(EventBus.ALOUD_STATE, Status.PAUSE)
ReadBook.uploadProgress()
startDs()
doDs()
}
@CallSuper
@ -184,7 +188,7 @@ abstract class BaseReadAloudService : BaseService(),
private fun setTimer(minute: Int) {
timeMinute = minute
startDs()
doDs()
}
private fun addTimer() {
@ -194,32 +198,34 @@ abstract class BaseReadAloudService : BaseService(),
timeMinute += 10
if (timeMinute > 180) timeMinute = 180
}
startDs()
}
private fun startDs() {
postEvent(EventBus.TTS_DS, timeMinute)
upNotification()
handler.removeCallbacks(dsRunnable)
handler.postDelayed(dsRunnable, 60000)
doDs()
}
/**
* 定时
*/
@Synchronized
private fun doDs() {
handler.removeCallbacks(dsRunnable)
if (!pause) {
if (timeMinute >= 0) {
timeMinute--
}
if (timeMinute == 0) {
ReadAloud.stop(this)
}
}
postEvent(EventBus.TTS_DS, timeMinute)
upNotification()
handler.postDelayed(dsRunnable, 60000)
dsJob?.cancel()
dsJob = launch {
while (isActive) {
delay(60000)
if (isActive) {
if (!pause) {
if (timeMinute >= 0) {
timeMinute--
}
if (timeMinute == 0) {
ReadAloud.stop(this@BaseReadAloudService)
}
}
postEvent(EventBus.TTS_DS, timeMinute)
upNotification()
}
}
}
}
/**

Loading…
Cancel
Save