From 9f5ee794d5f79d8de0e5f4284f8fa1cf1bc94383 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 15 Aug 2021 19:12:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AA=92=E4=BD=93=E6=8C=89=E9=94=AE=E5=8F=8C?= =?UTF-8?q?=E5=87=BB=E4=B8=8A=E4=B8=80=E9=A6=96=E4=B8=8B=E4=B8=80=E9=A6=96?= =?UTF-8?q?=E4=BC=9A=E8=B7=B3=E5=88=B0=E4=B8=8A=E4=B8=80=E7=AB=A0=E4=B8=8B?= =?UTF-8?q?=E4=B8=80=E7=AB=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/receiver/MediaButtonReceiver.kt | 37 ++++++++++++++++++- .../app/service/BaseReadAloudService.kt | 24 +++++++++++- .../app/service/HttpReadAloudService.kt | 28 ++------------ .../legado/app/service/TTSReadAloudService.kt | 28 ++------------ 4 files changed, 65 insertions(+), 52 deletions(-) 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() - } - } - /** * 暂停朗读 */