媒体按键双击上一首下一首会跳到上一章下一章

pull/1251/head
gedoor 3 years ago
parent 5f1050d3ec
commit 9f5ee794d5
  1. 37
      app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt
  2. 24
      app/src/main/java/io/legado/app/service/BaseReadAloudService.kt
  3. 28
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt
  4. 28
      app/src/main/java/io/legado/app/service/TTSReadAloudService.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)
}

@ -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

@ -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

@ -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()
}
}
/**
* 暂停朗读
*/

Loading…
Cancel
Save