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 9ae6df41a..a407381ab 100644 --- a/app/src/main/java/io/legado/app/service/AudioPlayService.kt +++ b/app/src/main/java/io/legado/app/service/AudioPlayService.kt @@ -14,7 +14,6 @@ import android.support.v4.media.session.MediaSessionCompat import android.support.v4.media.session.PlaybackStateCompat import androidx.core.app.NotificationCompat import androidx.media.AudioFocusRequestCompat -import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.PlaybackException import com.google.android.exoplayer2.Player import com.google.android.exoplayer2.SimpleExoPlayer @@ -25,6 +24,7 @@ import io.legado.app.constant.EventBus import io.legado.app.constant.IntentAction import io.legado.app.constant.Status import io.legado.app.data.appDb +import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.help.ExoPlayerHelper import io.legado.app.help.IntentHelp @@ -54,7 +54,7 @@ class AudioPlayService : BaseService(), private lateinit var audioManager: AudioManager private lateinit var mFocusRequest: AudioFocusRequestCompat - private lateinit var exoPlayer: ExoPlayer + private lateinit var exoPlayer: SimpleExoPlayer private var title: String = "" private var subtitle: String = "" private var mediaSessionCompat: MediaSessionCompat? = null @@ -135,6 +135,7 @@ class AudioPlayService : BaseService(), exoPlayer.setMediaSource(mediaSource) exoPlayer.playWhenReady = true exoPlayer.prepare() + exoPlayer.seekTo(position.toLong()) }.onFailure { it.printStackTrace() toastOnUi("$url ${it.localizedMessage}") @@ -163,7 +164,6 @@ class AudioPlayService : BaseService(), pause = false if (!exoPlayer.isPlaying) { exoPlayer.play() - exoPlayer.seekTo(position.toLong()) } upPlayProgress() upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING) @@ -177,11 +177,7 @@ class AudioPlayService : BaseService(), } private fun adjustProgress(position: Int) { - if (exoPlayer.isPlaying) { - exoPlayer.seekTo(position.toLong()) - } else { - this.position = position - } + exoPlayer.seekTo(position.toLong()) } private fun upSpeed(adjust: Float) { @@ -205,8 +201,13 @@ class AudioPlayService : BaseService(), } Player.STATE_READY -> { // 准备好 - AudioPlay.status = Status.PLAY - postEvent(EventBus.AUDIO_STATE, Status.PLAY) + if (exoPlayer.playWhenReady) { + AudioPlay.status = Status.PLAY + postEvent(EventBus.AUDIO_STATE, Status.PLAY) + } else { + AudioPlay.status = Status.PAUSE + postEvent(EventBus.AUDIO_STATE, Status.PAUSE) + } postEvent(EventBus.AUDIO_SIZE, exoPlayer.duration) upPlayProgress() AudioPlay.saveDurChapter(exoPlayer.duration) @@ -277,8 +278,11 @@ class AudioPlayService : BaseService(), upPlayProgressJob?.cancel() upPlayProgressJob = launch { while (isActive) { - saveProgress() - postEvent(EventBus.AUDIO_PROGRESS, exoPlayer.currentPosition) + AudioPlay.book?.let { + it.durChapterPos = exoPlayer.currentPosition.toInt() + postEvent(EventBus.AUDIO_PROGRESS, it.durChapterPos) + saveProgress(it) + } delay(1000) } } @@ -286,7 +290,7 @@ class AudioPlayService : BaseService(), private fun loadContent() = with(AudioPlay) { durChapter?.let { chapter -> - if (addLoading(durChapterIndex)) { + if (addLoading(chapter.index)) { val book = AudioPlay.book val bookSource = AudioPlay.bookSource if (book != null && bookSource != null) { @@ -330,19 +334,16 @@ class AudioPlayService : BaseService(), * 加载完成 */ private fun contentLoadFinish(chapter: BookChapter, content: String) { - if (chapter.index == AudioPlay.durChapterIndex) { + if (chapter.index == AudioPlay.book?.durChapterIndex) { subtitle = chapter.title url = content play() } } - private fun saveProgress() { + private fun saveProgress(book: Book) { execute { - AudioPlay.book?.let { - AudioPlay.durChapterPos = exoPlayer.currentPosition.toInt() - appDb.bookDao.upProgress(it.bookUrl, AudioPlay.durChapterPos) - } + appDb.bookDao.upProgress(book.bookUrl, book.durChapterPos) } } 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 4fd7a88de..fa3f83e38 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 @@ -22,8 +22,6 @@ object AudioPlay { var book: Book? = null var durChapter: BookChapter? = null var inBookshelf = false - var durChapterIndex = 0 - var durChapterPos = 0 var bookSource: BookSource? = null val loadingChapters = arrayListOf() @@ -37,18 +35,19 @@ object AudioPlay { upDurChapter(it) } durChapter?.let { - val intent = Intent(context, AudioPlayService::class.java) - intent.action = IntentAction.play - context.startService(intent) + context.startService { + action = IntentAction.play + + } } } } fun upDurChapter(book: Book) { - durChapter = appDb.bookChapterDao.getChapter(book.bookUrl, durChapterIndex) + durChapter = appDb.bookChapterDao.getChapter(book.bookUrl, book.durChapterIndex) postEvent(EventBus.AUDIO_SUB_TITLE, durChapter?.title ?: "") postEvent(EventBus.AUDIO_SIZE, durChapter?.end?.toInt() ?: 0) - postEvent(EventBus.AUDIO_PROGRESS, durChapterPos) + postEvent(EventBus.AUDIO_PROGRESS, book.durChapterPos) } fun pause(context: Context) { @@ -96,8 +95,8 @@ object AudioPlay { fun skipTo(context: Context, index: Int) { Coroutine.async { book?.let { book -> - durChapterIndex = index - durChapterPos = 0 + book.durChapterIndex = index + book.durChapterPos = 0 durChapter = null saveRead(book) play(context) @@ -111,8 +110,8 @@ object AudioPlay { if (book.durChapterIndex <= 0) { return@let } - durChapterIndex-- - durChapterPos = 0 + book.durChapterIndex = book.durChapterIndex - 1 + book.durChapterPos = 0 durChapter = null saveRead(book) play(context) @@ -125,8 +124,8 @@ object AudioPlay { if (book.durChapterIndex >= book.totalChapterNum) { return@let } - durChapterIndex++ - durChapterPos = 0 + book.durChapterIndex = book.durChapterIndex + 1 + book.durChapterPos = 0 durChapter = null saveRead(book) play(context) @@ -142,8 +141,6 @@ object AudioPlay { fun saveRead(book: Book) { book.lastCheckCount = 0 book.durChapterTime = System.currentTimeMillis() - book.durChapterIndex = durChapterIndex - book.durChapterPos = durChapterPos Coroutine.async { appDb.bookChapterDao.getChapter(book.bookUrl, book.durChapterIndex)?.let { book.durChapterTitle = it.title diff --git a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt index acba1ae55..0e5aa459c 100644 --- a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt @@ -54,7 +54,7 @@ class AudioPlayActivity : } private val tocActivityResult = registerForActivityResult(TocActivityResult()) { it?.let { - if (it.first != AudioPlay.durChapterIndex) { + if (it.first != AudioPlay.book?.durChapterIndex) { AudioPlay.skipTo(this, it.first) } } @@ -233,7 +233,6 @@ class AudioPlayActivity : binding.tvAllTime.text = progressTimeFormat.format(it.toLong()) } observeEventSticky(EventBus.AUDIO_PROGRESS) { - AudioPlay.durChapterPos = it if (!adjustProgress) binding.playerProgress.progress = it binding.tvDurTime.text = progressTimeFormat.format(it.toLong()) } diff --git a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt index e55919ad0..e52d0af61 100644 --- a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayViewModel.kt @@ -27,9 +27,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) book?.let { book -> titleData.postValue(book.name) coverData.postValue(book.getDisplayCover()) - durChapterIndex = book.durChapterIndex - durChapterPos = book.durChapterPos - durChapter = appDb.bookChapterDao.getChapter(book.bookUrl, durChapterIndex) + durChapter = appDb.bookChapterDao.getChapter(book.bookUrl, book.durChapterIndex) upDurChapter(book) bookSource = appDb.bookSourceDao.getBookSource(book.origin) if (durChapter == null) { @@ -111,14 +109,13 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) chapters: List ) { execute { - AudioPlay.durChapterIndex = BookHelp.getDurChapter( + book.durChapterIndex = BookHelp.getDurChapter( book.durChapterIndex, oldTocSize, book.durChapterTitle, chapters ) - book.durChapterIndex = AudioPlay.durChapterIndex - book.durChapterTitle = chapters[AudioPlay.durChapterIndex].title + book.durChapterTitle = chapters[book.durChapterIndex].title appDb.bookDao.update(book) appDb.bookChapterDao.insert(*chapters.toTypedArray()) } diff --git a/build.gradle b/build.gradle index 36559ec3b..96c3d0315 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,7 @@ buildscript { maven { url 'https://maven.aliyun.com/repository/gradle-plugin' } } dependencies { - classpath 'com.android.tools.build:gradle:7.0.1' + classpath 'com.android.tools.build:gradle:7.0.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'de.timfreiheit.resourceplaceholders:placeholders:0.4' classpath 'de.undercouch:gradle-download-task:4.1.2'