|
|
|
@ -31,7 +31,6 @@ import io.legado.app.receiver.MediaButtonReceiver |
|
|
|
|
import io.legado.app.service.help.AudioPlay |
|
|
|
|
import io.legado.app.ui.audio.AudioPlayActivity |
|
|
|
|
import io.legado.app.utils.postEvent |
|
|
|
|
import kotlinx.coroutines.Dispatchers.IO |
|
|
|
|
import kotlinx.coroutines.Dispatchers.Main |
|
|
|
|
import kotlinx.coroutines.launch |
|
|
|
|
import kotlinx.coroutines.withContext |
|
|
|
@ -62,7 +61,6 @@ class AudioPlayService : BaseService(), |
|
|
|
|
private var position = 0 |
|
|
|
|
private val dsRunnable: Runnable = Runnable { doDs() } |
|
|
|
|
private var mpRunnable: Runnable = Runnable { upPlayProgress() } |
|
|
|
|
private var bookChapter: BookChapter? = null |
|
|
|
|
private var playSpeed: Float = 1f |
|
|
|
|
|
|
|
|
|
override fun onCreate() { |
|
|
|
@ -85,8 +83,9 @@ class AudioPlayService : BaseService(), |
|
|
|
|
IntentAction.play -> { |
|
|
|
|
AudioPlay.book?.let { |
|
|
|
|
title = it.name |
|
|
|
|
subtitle = AudioPlay.durChapter?.title ?: "" |
|
|
|
|
position = it.durChapterPos |
|
|
|
|
loadContent(it.durChapterIndex) |
|
|
|
|
loadContent() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
IntentAction.pause -> pause(true) |
|
|
|
@ -210,12 +209,7 @@ class AudioPlayService : BaseService(), |
|
|
|
|
postEvent(EventBus.AUDIO_SIZE, mediaPlayer.duration) |
|
|
|
|
handler.removeCallbacks(mpRunnable) |
|
|
|
|
handler.post(mpRunnable) |
|
|
|
|
execute { |
|
|
|
|
bookChapter?.let { |
|
|
|
|
it.end = mediaPlayer.duration.toLong() |
|
|
|
|
App.db.bookChapterDao.insert(it) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
AudioPlay.saveDurChapter(mediaPlayer.duration.toLong()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -270,45 +264,31 @@ class AudioPlayService : BaseService(), |
|
|
|
|
handler.postDelayed(mpRunnable, 1000) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun loadContent(index: Int) { |
|
|
|
|
AudioPlay.book?.let { book -> |
|
|
|
|
if (addLoading(index)) { |
|
|
|
|
launch(IO) { |
|
|
|
|
App.db.bookChapterDao.getChapter(book.bookUrl, index)?.let { chapter -> |
|
|
|
|
if (index == AudioPlay.durChapterIndex) { |
|
|
|
|
bookChapter = chapter |
|
|
|
|
subtitle = chapter.title |
|
|
|
|
postEvent(EventBus.AUDIO_SIZE, chapter.end?.toInt() ?: 0) |
|
|
|
|
postEvent(EventBus.AUDIO_PROGRESS, position) |
|
|
|
|
} |
|
|
|
|
loadContent(chapter) |
|
|
|
|
} ?: removeLoading(index) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun loadContent(chapter: BookChapter) { |
|
|
|
|
val book = AudioPlay.book |
|
|
|
|
val webBook = AudioPlay.webBook |
|
|
|
|
if (book != null && webBook != null) { |
|
|
|
|
webBook.getContent(book, chapter, scope = this@AudioPlayService) |
|
|
|
|
.onSuccess { content -> |
|
|
|
|
removeLoading(chapter.index) |
|
|
|
|
if (content.isEmpty()) { |
|
|
|
|
withContext(Main) { |
|
|
|
|
toast("未获取到资源链接") |
|
|
|
|
private fun loadContent() = with(AudioPlay) { |
|
|
|
|
durChapter?.let { chapter -> |
|
|
|
|
if (addLoading(durChapterIndex)) { |
|
|
|
|
val book = AudioPlay.book |
|
|
|
|
val webBook = AudioPlay.webBook |
|
|
|
|
if (book != null && webBook != null) { |
|
|
|
|
webBook.getContent(book, chapter, scope = this@AudioPlayService) |
|
|
|
|
.onSuccess { content -> |
|
|
|
|
if (content.isEmpty()) { |
|
|
|
|
withContext(Main) { |
|
|
|
|
toast("未获取到资源链接") |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
contentLoadFinish(chapter, content) |
|
|
|
|
} |
|
|
|
|
}.onError { |
|
|
|
|
contentLoadFinish(chapter, it.localizedMessage ?: toString()) |
|
|
|
|
}.onFinally { |
|
|
|
|
removeLoading(chapter.index) |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
contentLoadFinish(chapter, content) |
|
|
|
|
} |
|
|
|
|
}.onError { |
|
|
|
|
contentLoadFinish(chapter, it.localizedMessage ?: toString()) |
|
|
|
|
} else { |
|
|
|
|
removeLoading(chapter.index) |
|
|
|
|
toast("book or source is null") |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
toast("book or source is null") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|