From 940e4c5a97ba0c6105cf32a8b1b3fe1bef0039e2 Mon Sep 17 00:00:00 2001 From: gedoor Date: Mon, 23 Nov 2020 21:55:23 +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 | 23 ++++----------- .../io/legado/app/service/help/AudioPlay.kt | 29 +++++++++++++++++++ .../legado/app/ui/audio/AudioPlayActivity.kt | 12 +------- .../legado/app/ui/audio/AudioPlayViewModel.kt | 17 +---------- 4 files changed, 37 insertions(+), 44 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 b83c10d7b..6d688cedf 100644 --- a/app/src/main/java/io/legado/app/service/AudioPlayService.kt +++ b/app/src/main/java/io/legado/app/service/AudioPlayService.kt @@ -161,8 +161,10 @@ class AudioPlayService : BaseService(), private fun resume() { pause = false - mediaPlayer.start() - mediaPlayer.seekTo(position) + if (!mediaPlayer.isPlaying) { + mediaPlayer.start() + mediaPlayer.seekTo(position) + } handler.removeCallbacks(mpRunnable) handler.postDelayed(mpRunnable, 1000) upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING) @@ -337,7 +339,7 @@ class AudioPlayService : BaseService(), AudioPlay.durChapterIndex-- AudioPlay.durPageIndex = 0 AudioPlay.book?.durChapterIndex = AudioPlay.durChapterIndex - saveRead() + AudioPlay.saveRead() position = 0 loadContent(AudioPlay.durChapterIndex) } @@ -349,7 +351,7 @@ class AudioPlayService : BaseService(), AudioPlay.durChapterIndex++ AudioPlay.durPageIndex = 0 AudioPlay.book?.durChapterIndex = AudioPlay.durChapterIndex - saveRead() + AudioPlay.saveRead() position = 0 loadContent(AudioPlay.durChapterIndex) } else { @@ -357,19 +359,6 @@ class AudioPlayService : BaseService(), } } - private fun saveRead() { - launch(IO) { - AudioPlay.book?.let { book -> - book.lastCheckCount = 0 - book.durChapterTime = System.currentTimeMillis() - book.durChapterIndex = AudioPlay.durChapterIndex - book.durChapterPos = AudioPlay.durPageIndex - book.durChapterTitle = subtitle - App.db.bookDao().update(book) - } - } - } - 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 06bb6b5a9..6cf2ee111 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 @@ -3,9 +3,11 @@ package io.legado.app.service.help import android.content.Context import android.content.Intent import androidx.lifecycle.MutableLiveData +import io.legado.app.App 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 @@ -73,6 +75,18 @@ 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 prev(context: Context) { if (AudioPlayService.isRun) { val intent = Intent(context, AudioPlayService::class.java) @@ -89,4 +103,19 @@ object AudioPlay { } } + fun saveRead() { + Coroutine.async { + book?.let { book -> + book.lastCheckCount = 0 + book.durChapterTime = System.currentTimeMillis() + book.durChapterIndex = AudioPlay.durChapterIndex + book.durChapterPos = AudioPlay.durPageIndex + App.db.bookChapterDao().getChapter(book.bookUrl, book.durChapterIndex)?.let { + book.durChapterTitle = it.title + } + App.db.bookDao().update(book) + } + } + } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt b/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt index 424387f10..69604fc6f 100644 --- a/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt +++ b/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt @@ -20,7 +20,6 @@ import io.legado.app.data.entities.Book import io.legado.app.help.BlurTransformation import io.legado.app.help.ImageLoader import io.legado.app.lib.dialogs.alert -import io.legado.app.service.AudioPlayService import io.legado.app.service.help.AudioPlay import io.legado.app.ui.book.changesource.ChangeSourceDialog import io.legado.app.ui.book.toc.ChapterListActivity @@ -175,16 +174,7 @@ class AudioPlayActivity : when (requestCode) { requestCodeChapter -> data?.getIntExtra("index", AudioPlay.durChapterIndex)?.let { if (it != AudioPlay.durChapterIndex) { - val isPlay = !AudioPlayService.pause - AudioPlay.pause(this) - AudioPlay.status = Status.STOP - AudioPlay.durChapterIndex = it - AudioPlay.durPageIndex = 0 - AudioPlay.book?.durChapterIndex = AudioPlay.durChapterIndex - viewModel.saveRead() - if (isPlay) { - AudioPlay.play(this) - } + AudioPlay.skipTo(this, it) } } } diff --git a/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt b/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt index 5225b21ff..5d0a1016b 100644 --- a/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt @@ -47,7 +47,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) AudioPlay.chapterSize = count } } - saveRead() + AudioPlay.saveRead() } } } @@ -128,21 +128,6 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) } } - fun saveRead() { - execute { - AudioPlay.book?.let { book -> - book.lastCheckCount = 0 - book.durChapterTime = System.currentTimeMillis() - book.durChapterIndex = AudioPlay.durChapterIndex - book.durChapterPos = AudioPlay.durPageIndex - App.db.bookChapterDao().getChapter(book.bookUrl, book.durChapterIndex)?.let { - book.durChapterTitle = it.title - } - App.db.bookDao().update(book) - } - } - } - fun removeFromBookshelf(success: (() -> Unit)?) { execute { AudioPlay.book?.let {