pull/508/head
gedoor 4 years ago
parent f123638946
commit 8fc32eb9be
  1. 32
      app/src/main/java/io/legado/app/service/AudioPlayService.kt
  2. 74
      app/src/main/java/io/legado/app/service/help/AudioPlay.kt

@ -91,8 +91,8 @@ class AudioPlayService : BaseService(),
} }
IntentAction.pause -> pause(true) IntentAction.pause -> pause(true)
IntentAction.resume -> resume() IntentAction.resume -> resume()
IntentAction.prev -> moveToPrev() IntentAction.prev -> AudioPlay.prev(this)
IntentAction.next -> moveToNext() IntentAction.next -> AudioPlay.next(this)
IntentAction.adjustSpeed -> upSpeed(intent.getFloatExtra("adjust", 1f)) IntentAction.adjustSpeed -> upSpeed(intent.getFloatExtra("adjust", 1f))
IntentAction.addTimer -> addTimer() IntentAction.addTimer -> addTimer()
IntentAction.setTimer -> setTimer(intent.getIntExtra("minute", 0)) IntentAction.setTimer -> setTimer(intent.getIntExtra("minute", 0))
@ -230,7 +230,7 @@ class AudioPlayService : BaseService(),
*/ */
override fun onCompletion(mp: MediaPlayer?) { override fun onCompletion(mp: MediaPlayer?) {
handler.removeCallbacks(mpRunnable) handler.removeCallbacks(mpRunnable)
moveToNext() AudioPlay.next(this)
} }
private fun setTimer(minute: Int) { 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() { private fun saveProgress() {
launch(IO) { launch(IO) {
AudioPlay.book?.let { AudioPlay.book?.let {

@ -4,12 +4,14 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import io.legado.app.App import io.legado.app.App
import io.legado.app.constant.EventBus
import io.legado.app.constant.IntentAction import io.legado.app.constant.IntentAction
import io.legado.app.constant.Status import io.legado.app.constant.Status
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.service.AudioPlayService import io.legado.app.service.AudioPlayService
import io.legado.app.utils.postEvent
object AudioPlay { object AudioPlay {
var titleData = MutableLiveData<String>() var titleData = MutableLiveData<String>()
@ -75,31 +77,65 @@ object AudioPlay {
} }
} }
fun skipTo(context: Context, chapterIndex: Int) { fun skipTo(context: Context, index: Int) {
val isPlay = !AudioPlayService.pause Coroutine.async {
pause(context) book?.let { book ->
status = Status.STOP val isPlay = !AudioPlayService.pause
durChapterIndex = chapterIndex pause(context)
durPageIndex = 0 status = Status.STOP
book?.durChapterIndex = durChapterIndex durChapterIndex = index
if (isPlay) { durPageIndex = 0
play(context) 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) { fun prev(context: Context) {
if (AudioPlayService.isRun) { Coroutine.async {
val intent = Intent(context, AudioPlayService::class.java) book?.let { book ->
intent.action = IntentAction.prev if (book.durChapterIndex <= 0) {
context.startService(intent) 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) { fun next(context: Context) {
if (AudioPlayService.isRun) { Coroutine.async {
val intent = Intent(context, AudioPlayService::class.java) book?.let { book ->
intent.action = IntentAction.next pause(context)
context.startService(intent) 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?.let { book ->
book.lastCheckCount = 0 book.lastCheckCount = 0
book.durChapterTime = System.currentTimeMillis() book.durChapterTime = System.currentTimeMillis()
book.durChapterIndex = AudioPlay.durChapterIndex book.durChapterIndex = durChapterIndex
book.durChapterPos = AudioPlay.durPageIndex book.durChapterPos = durPageIndex
App.db.bookChapterDao().getChapter(book.bookUrl, book.durChapterIndex)?.let { App.db.bookChapterDao().getChapter(book.bookUrl, book.durChapterIndex)?.let {
book.durChapterTitle = it.title book.durChapterTitle = it.title
} }

Loading…
Cancel
Save