pull/508/head
gedoor 4 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.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 {

@ -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<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
pause(context)
status = Status.STOP
durChapterIndex = chapterIndex
durChapterIndex = index
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) {
play(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) {
val intent = Intent(context, AudioPlayService::class.java)
intent.action = IntentAction.prev
context.startService(intent)
play(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) {
val intent = Intent(context, AudioPlayService::class.java)
intent.action = IntentAction.next
context.startService(intent)
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
}

Loading…
Cancel
Save