diff --git a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt index 620566feb..13f4d90c9 100644 --- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt @@ -2,6 +2,7 @@ package io.legado.app.service import android.app.PendingIntent import android.media.MediaPlayer +import io.legado.app.constant.AppLog import io.legado.app.constant.EventBus import io.legado.app.help.AppConfig import io.legado.app.help.coroutine.Coroutine @@ -73,6 +74,16 @@ class HttpReadAloudService : BaseReadAloudService(), player.stop() } + private fun playNext() { + readAloudNumber += contentList[nowSpeak].length + 1 + if (nowSpeak < contentList.lastIndex) { + nowSpeak++ + play() + } else { + nextChapter() + } + } + private fun downloadAudio() { task?.cancel() task = execute { @@ -234,32 +245,28 @@ class HttpReadAloudService : BaseReadAloudService(), postEvent(EventBus.TTS_PROGRESS, readAloudNumber + 1) } + private var errorNo = 0 + override fun onError(mp: MediaPlayer?, what: Int, extra: Int): Boolean { LogUtils.d("mp", "what:$what extra:$extra") if (what == -38 && extra == 0) { return true } + AppLog.addLog("朗读错误,($what, $extra)") + errorNo++ + if (errorNo >= 3) { + toastOnUi("朗读连续3次错误, 最后一次错误代码($what, $extra)") + } launch { - delay(100) - readAloudNumber += contentList[nowSpeak].length + 1 - if (nowSpeak < contentList.lastIndex) { - nowSpeak++ - play() - } else { - nextChapter() - } + delay(500) + playNext() } - return true + return false } override fun onCompletion(mp: MediaPlayer?) { - readAloudNumber += contentList[nowSpeak].length + 1 - if (nowSpeak < contentList.lastIndex) { - nowSpeak++ - play() - } else { - nextChapter() - } + errorNo = 0 + playNext() } override fun aloudServicePendingIntent(actionStr: String): PendingIntent? {