From 8fc32eb9be11f9d7b2f186e3499eed51f9ba8a03 Mon Sep 17 00:00:00 2001 From: gedoor Date: Mon, 23 Nov 2020 22:30:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/service/AudioPlayService.kt | 32 +------- .../io/legado/app/service/help/AudioPlay.kt | 74 ++++++++++++++----- 2 files changed, 58 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/AudioPlayService.kt b/app/src/main/java/io/legado/app/service/AudioPlayService.kt index 6d688cedf..64104ed87 100644 --- a/app/src/main/java/io/legado/app/service/AudioPlayService.kt +++ b/app/src/main/java/io/legado/app/service/AudioPlayService.kt @@ -91,8 +91,8 @@ class AudioPlayService : BaseService(), } IntentAction.pause -> pause(true) IntentAction.resume -> resume() - IntentAction.prev -> moveToPrev() - IntentAction.next -> moveToNext() + IntentAction.prev -> AudioPlay.prev(this) + IntentAction.next -> AudioPlay.next(this) IntentAction.adjustSpeed -> upSpeed(intent.getFloatExtra("adjust", 1f)) IntentAction.addTimer -> addTimer() IntentAction.setTimer -> setTimer(intent.getIntExtra("minute", 0)) @@ -230,7 +230,7 @@ class AudioPlayService : BaseService(), */ override fun onCompletion(mp: MediaPlayer?) { handler.removeCallbacks(mpRunnable) - moveToNext() + AudioPlay.next(this) } private fun setTimer(minute: Int) { @@ -333,32 +333,6 @@ class AudioPlayService : BaseService(), } } - private fun moveToPrev() { - if (AudioPlay.durChapterIndex > 0) { - mediaPlayer.pause() - AudioPlay.durChapterIndex-- - AudioPlay.durPageIndex = 0 - AudioPlay.book?.durChapterIndex = AudioPlay.durChapterIndex - AudioPlay.saveRead() - position = 0 - loadContent(AudioPlay.durChapterIndex) - } - } - - private fun moveToNext() { - if (AudioPlay.durChapterIndex < AudioPlay.chapterSize - 1) { - mediaPlayer.pause() - AudioPlay.durChapterIndex++ - AudioPlay.durPageIndex = 0 - AudioPlay.book?.durChapterIndex = AudioPlay.durChapterIndex - AudioPlay.saveRead() - position = 0 - loadContent(AudioPlay.durChapterIndex) - } else { - stopSelf() - } - } - private fun saveProgress() { launch(IO) { AudioPlay.book?.let { diff --git a/app/src/main/java/io/legado/app/service/help/AudioPlay.kt b/app/src/main/java/io/legado/app/service/help/AudioPlay.kt index 6cf2ee111..ec120b491 100644 --- a/app/src/main/java/io/legado/app/service/help/AudioPlay.kt +++ b/app/src/main/java/io/legado/app/service/help/AudioPlay.kt @@ -4,12 +4,14 @@ import android.content.Context import android.content.Intent import androidx.lifecycle.MutableLiveData import io.legado.app.App +import io.legado.app.constant.EventBus import io.legado.app.constant.IntentAction import io.legado.app.constant.Status import io.legado.app.data.entities.Book import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.webBook.WebBook import io.legado.app.service.AudioPlayService +import io.legado.app.utils.postEvent object AudioPlay { var titleData = MutableLiveData() @@ -75,31 +77,65 @@ object AudioPlay { } } - fun skipTo(context: Context, chapterIndex: Int) { - val isPlay = !AudioPlayService.pause - pause(context) - status = Status.STOP - durChapterIndex = chapterIndex - durPageIndex = 0 - book?.durChapterIndex = durChapterIndex - if (isPlay) { - play(context) + fun skipTo(context: Context, index: Int) { + Coroutine.async { + book?.let { book -> + val isPlay = !AudioPlayService.pause + pause(context) + status = Status.STOP + durChapterIndex = index + durPageIndex = 0 + book.durChapterIndex = durChapterIndex + book.durChapterPos = 0 + saveRead() + App.db.bookChapterDao().getChapter(book.bookUrl, durChapterIndex)?.let { chapter -> + postEvent(EventBus.AUDIO_SUB_TITLE, chapter.title) + } + if (isPlay) { + play(context) + } + } } } fun prev(context: Context) { - if (AudioPlayService.isRun) { - val intent = Intent(context, AudioPlayService::class.java) - intent.action = IntentAction.prev - context.startService(intent) + Coroutine.async { + book?.let { book -> + if (book.durChapterIndex <= 0) { + return@let + } + pause(context) + durChapterIndex-- + durPageIndex = 0 + book.durChapterIndex = durChapterIndex + book.durChapterPos = 0 + saveRead() + App.db.bookChapterDao().getChapter(book.bookUrl, durChapterIndex)?.let { chapter -> + postEvent(EventBus.AUDIO_SUB_TITLE, chapter.title) + } + if (AudioPlayService.isRun) { + play(context) + } + } } } fun next(context: Context) { - if (AudioPlayService.isRun) { - val intent = Intent(context, AudioPlayService::class.java) - intent.action = IntentAction.next - context.startService(intent) + Coroutine.async { + book?.let { book -> + pause(context) + durChapterIndex++ + durPageIndex = 0 + book.durChapterIndex = durChapterIndex + book.durChapterPos = 0 + saveRead() + App.db.bookChapterDao().getChapter(book.bookUrl, durChapterIndex)?.let { chapter -> + postEvent(EventBus.AUDIO_SUB_TITLE, chapter.title) + } + if (AudioPlayService.isRun) { + play(context) + } + } } } @@ -108,8 +144,8 @@ object AudioPlay { book?.let { book -> book.lastCheckCount = 0 book.durChapterTime = System.currentTimeMillis() - book.durChapterIndex = AudioPlay.durChapterIndex - book.durChapterPos = AudioPlay.durPageIndex + book.durChapterIndex = durChapterIndex + book.durChapterPos = durPageIndex App.db.bookChapterDao().getChapter(book.bookUrl, book.durChapterIndex)?.let { book.durChapterTitle = it.title }