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.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")
}
}
}

@ -32,9 +32,22 @@ object AudioPlay {
}
fun play(context: Context) {
val intent = Intent(context, AudioPlayService::class.java)
intent.action = IntentAction.play
context.startService(intent)
book?.let {
if (durChapter == null) {
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) {
@ -84,6 +97,7 @@ object AudioPlay {
book?.let { book ->
durChapterIndex = index
durChapterPos = 0
durChapter = null
book.durChapterIndex = durChapterIndex
book.durChapterPos = 0
saveRead()
@ -103,6 +117,7 @@ object AudioPlay {
}
durChapterIndex--
durChapterPos = 0
durChapter = null
book.durChapterIndex = durChapterIndex
book.durChapterPos = 0
saveRead()
@ -122,6 +137,7 @@ object AudioPlay {
}
durChapterIndex++
durChapterPos = 0
durChapter = null
book.durChapterIndex = durChapterIndex
book.durChapterPos = 0
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
}
durChapter = App.db.bookChapterDao.getChapter(book.bookUrl, durChapterIndex)
upDurChapter(book)
chapterSize = count
}
}
@ -85,6 +85,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
} else {
changeDruChapterIndex(cList)
}
AudioPlay.upDurChapter(book)
} else {
toast(R.string.error_load_toc)
}

Loading…
Cancel
Save