pull/525/head
gedoor 5 years ago
parent f5b0e2fec2
commit e297b92c41
  1. 40
      app/src/main/java/io/legado/app/service/AudioPlayService.kt
  2. 24
      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,32 +264,14 @@ 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)) {
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 book = AudioPlay.book
val webBook = AudioPlay.webBook val webBook = AudioPlay.webBook
if (book != null && webBook != null) { if (book != null && webBook != null) {
webBook.getContent(book, chapter, scope = this@AudioPlayService) webBook.getContent(book, chapter, scope = this@AudioPlayService)
.onSuccess { content -> .onSuccess { content ->
removeLoading(chapter.index)
if (content.isEmpty()) { if (content.isEmpty()) {
withContext(Main) { withContext(Main) {
toast("未获取到资源链接") toast("未获取到资源链接")
@ -305,12 +281,16 @@ class AudioPlayService : BaseService(),
} }
}.onError { }.onError {
contentLoadFinish(chapter, it.localizedMessage ?: toString()) contentLoadFinish(chapter, it.localizedMessage ?: toString())
}.onFinally {
removeLoading(chapter.index) removeLoading(chapter.index)
} }
} else { } else {
removeLoading(chapter.index)
toast("book or source is null") toast("book or source is null")
} }
} }
}
}
private fun addLoading(index: Int): Boolean { private fun addLoading(index: Int): Boolean {
synchronized(this) { synchronized(this) {

@ -32,10 +32,23 @@ object AudioPlay {
} }
fun play(context: Context) { fun play(context: Context) {
book?.let {
if (durChapter == null) {
upDurChapter(it)
}
durChapter?.let {
val intent = Intent(context, AudioPlayService::class.java) val intent = Intent(context, AudioPlayService::class.java)
intent.action = IntentAction.play intent.action = IntentAction.play
context.startService(intent) 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) {
if (AudioPlayService.isRun) { if (AudioPlayService.isRun) {
@ -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