pull/508/head
gedoor 5 years ago
parent f123638946
commit 8fc32eb9be
  1. 32
      app/src/main/java/io/legado/app/service/AudioPlayService.kt
  2. 58
      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) {
Coroutine.async {
book?.let { book ->
val isPlay = !AudioPlayService.pause val isPlay = !AudioPlayService.pause
pause(context) pause(context)
status = Status.STOP status = Status.STOP
durChapterIndex = chapterIndex durChapterIndex = index
durPageIndex = 0 durPageIndex = 0
book?.durChapterIndex = durChapterIndex 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) { if (isPlay) {
play(context) play(context)
} }
} }
}
}
fun prev(context: Context) { fun prev(context: Context) {
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) { if (AudioPlayService.isRun) {
val intent = Intent(context, AudioPlayService::class.java) play(context)
intent.action = IntentAction.prev }
context.startService(intent) }
} }
} }
fun next(context: Context) { fun next(context: Context) {
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) { if (AudioPlayService.isRun) {
val intent = Intent(context, AudioPlayService::class.java) play(context)
intent.action = IntentAction.next }
context.startService(intent) }
} }
} }
@ -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