pull/525/head
gedoor 4 years ago
parent f5b0e2fec2
commit e297b92c41
  1. 70
      app/src/main/java/io/legado/app/service/AudioPlayService.kt
  2. 30
      app/src/main/java/io/legado/app/service/help/AudioPlay.kt
  3. 3
      app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt

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

@ -32,9 +32,22 @@ object AudioPlay {
} }
fun play(context: Context) { fun play(context: Context) {
val intent = Intent(context, AudioPlayService::class.java) book?.let {
intent.action = IntentAction.play if (durChapter == null) {
context.startService(intent) upDurChapter(it)
}
durChapter?.let {
val intent = Intent(context, AudioPlayService::class.java)
intent.action = IntentAction.play
context.startService(intent)
}
}
}
fun upDurChapter(book: Book) {
durChapter = App.db.bookChapterDao.getChapter(book.bookUrl, durChapterIndex)
postEvent(EventBus.AUDIO_SIZE, durChapter?.end?.toInt() ?: 0)
postEvent(EventBus.AUDIO_PROGRESS, durChapterPos)
} }
fun pause(context: Context) { fun pause(context: Context) {
@ -84,6 +97,7 @@ object AudioPlay {
book?.let { book -> book?.let { book ->
durChapterIndex = index durChapterIndex = index
durChapterPos = 0 durChapterPos = 0
durChapter = null
book.durChapterIndex = durChapterIndex book.durChapterIndex = durChapterIndex
book.durChapterPos = 0 book.durChapterPos = 0
saveRead() saveRead()
@ -103,6 +117,7 @@ object AudioPlay {
} }
durChapterIndex-- durChapterIndex--
durChapterPos = 0 durChapterPos = 0
durChapter = null
book.durChapterIndex = durChapterIndex book.durChapterIndex = durChapterIndex
book.durChapterPos = 0 book.durChapterPos = 0
saveRead() saveRead()
@ -122,6 +137,7 @@ object AudioPlay {
} }
durChapterIndex++ durChapterIndex++
durChapterPos = 0 durChapterPos = 0
durChapter = null
book.durChapterIndex = durChapterIndex book.durChapterIndex = durChapterIndex
book.durChapterPos = 0 book.durChapterPos = 0
saveRead() saveRead()
@ -154,4 +170,12 @@ object AudioPlay {
} }
} }
fun saveDurChapter(audioSize: Long) {
Coroutine.async {
durChapter?.let {
it.end = audioSize
App.db.bookChapterDao.insert(it)
}
}
}
} }

@ -50,7 +50,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
durChapterIndex = count - 1 durChapterIndex = count - 1
} }
durChapter = App.db.bookChapterDao.getChapter(book.bookUrl, durChapterIndex) durChapter = App.db.bookChapterDao.getChapter(book.bookUrl, durChapterIndex)
upDurChapter(book)
chapterSize = count chapterSize = count
} }
} }
@ -85,6 +85,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
} else { } else {
changeDruChapterIndex(cList) changeDruChapterIndex(cList)
} }
AudioPlay.upDurChapter(book)
} else { } else {
toast(R.string.error_load_toc) toast(R.string.error_load_toc)
} }

Loading…
Cancel
Save