diff --git a/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt b/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt index 79d53779e..fd27171cc 100644 --- a/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt +++ b/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt @@ -3,6 +3,9 @@ package io.legado.app.receiver import android.content.BroadcastReceiver import android.content.Context import android.content.Intent +import android.os.Handler +import android.os.Looper +import android.os.Message import android.view.KeyEvent import io.legado.app.constant.EventBus import io.legado.app.data.appDb @@ -12,10 +15,12 @@ import io.legado.app.service.AudioPlayService import io.legado.app.service.BaseReadAloudService import io.legado.app.service.help.AudioPlay import io.legado.app.service.help.ReadAloud +import io.legado.app.service.help.ReadBook import io.legado.app.ui.book.audio.AudioPlayActivity import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.ui.main.MainActivity import io.legado.app.utils.postEvent +import splitties.init.appCtx /** @@ -32,6 +37,21 @@ class MediaButtonReceiver : BroadcastReceiver() { companion object { + val handler = object : Handler(Looper.getMainLooper()) { + + override fun handleMessage(msg: Message) { + when (msg.what) { + KeyEvent.KEYCODE_MEDIA_PREVIOUS -> { + ReadAloud.prevParagraph(appCtx) + } + KeyEvent.KEYCODE_MEDIA_NEXT -> { + ReadAloud.nextParagraph(appCtx) + } + } + } + + } + fun handleIntent(context: Context, intent: Intent): Boolean { val intentAction = intent.action if (Intent.ACTION_MEDIA_BUTTON == intentAction) { @@ -42,10 +62,23 @@ class MediaButtonReceiver : BroadcastReceiver() { if (action == KeyEvent.ACTION_DOWN) { when (keycode) { KeyEvent.KEYCODE_MEDIA_PREVIOUS -> { - ReadAloud.prevParagraph(context) + if (handler.hasMessages(KeyEvent.KEYCODE_MEDIA_PREVIOUS)) { + handler.removeMessages(KeyEvent.KEYCODE_MEDIA_PREVIOUS) + ReadBook.moveToPrevChapter(true) + } else { + handler.sendEmptyMessageDelayed( + KeyEvent.KEYCODE_MEDIA_PREVIOUS, + 500 + ) + } } KeyEvent.KEYCODE_MEDIA_NEXT -> { - ReadAloud.nextParagraph(context) + if (handler.hasMessages(KeyEvent.KEYCODE_MEDIA_NEXT)) { + handler.removeMessages(KeyEvent.KEYCODE_MEDIA_NEXT) + ReadBook.moveToNextChapter(true) + } else { + handler.sendEmptyMessageDelayed(KeyEvent.KEYCODE_MEDIA_NEXT, 500) + } } else -> readAloud(context) } diff --git a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt index 2e7e97227..1375c31d9 100644 --- a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt @@ -137,6 +137,8 @@ abstract class BaseReadAloudService : BaseService(), postEvent(EventBus.ALOUD_STATE, Status.PLAY) } + abstract fun playStop() + @CallSuper open fun pauseReadAloud(pause: Boolean) { BaseReadAloudService.pause = pause @@ -157,9 +159,27 @@ abstract class BaseReadAloudService : BaseService(), abstract fun upSpeechRate(reset: Boolean = false) - abstract fun prevP() + private fun prevP() { + if (nowSpeak > 0) { + playStop() + nowSpeak-- + readAloudNumber -= contentList[nowSpeak].length.minus(1) + play() + } else { + ReadBook.moveToPrevChapter(true) + } + } - abstract fun nextP() + private fun nextP() { + if (nowSpeak < contentList.size - 1) { + playStop() + readAloudNumber += contentList[nowSpeak].length.plus(1) + nowSpeak++ + play() + } else { + nextChapter() + } + } private fun setTimer(minute: Int) { timeMinute = minute 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 7d60c76d9..0c901b275 100644 --- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt @@ -68,6 +68,10 @@ class HttpReadAloudService : BaseReadAloudService(), } } + override fun playStop() { + player.stop() + } + private fun downloadAudio() { task?.cancel() task = execute { @@ -216,30 +220,6 @@ class HttpReadAloudService : BaseReadAloudService(), downloadAudio() } - /** - * 上一段 - */ - override fun prevP() { - if (nowSpeak > 0) { - player.stop() - nowSpeak-- - readAloudNumber -= contentList[nowSpeak].length.minus(1) - play() - } - } - - /** - * 下一段 - */ - override fun nextP() { - if (nowSpeak < contentList.size - 1) { - player.stop() - readAloudNumber += contentList[nowSpeak].length.plus(1) - nowSpeak++ - play() - } - } - override fun onPrepared(mp: MediaPlayer?) { super.play() if (pause) return 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 16ad9c52a..20f1a1573 100644 --- a/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt @@ -76,6 +76,10 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener } } + override fun playStop() { + textToSpeech?.stop() + } + /** * 更新朗读速度 */ @@ -90,30 +94,6 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener } } - /** - * 上一段 - */ - override fun prevP() { - if (nowSpeak > 0) { - textToSpeech?.stop() - nowSpeak-- - readAloudNumber -= contentList[nowSpeak].length.minus(1) - play() - } - } - - /** - * 下一段 - */ - override fun nextP() { - if (nowSpeak < contentList.size - 1) { - textToSpeech?.stop() - readAloudNumber += contentList[nowSpeak].length.plus(1) - nowSpeak++ - play() - } - } - /** * 暂停朗读 */