pull/508/head
gedoor 4 years ago
parent 63b48b620a
commit f3ef37edff
  1. 13
      app/src/main/java/io/legado/app/service/AudioPlayService.kt
  2. 17
      app/src/main/java/io/legado/app/service/help/AudioPlay.kt

@ -122,8 +122,8 @@ class AudioPlayService : BaseService(),
upNotification() upNotification()
if (requestFocus()) { if (requestFocus()) {
try { try {
AudioPlay.status = Status.PLAY AudioPlay.status = Status.STOP
postEvent(EventBus.AUDIO_STATE, Status.PLAY) postEvent(EventBus.AUDIO_STATE, Status.STOP)
mediaPlayer.reset() mediaPlayer.reset()
val analyzeUrl = val analyzeUrl =
AnalyzeUrl(url, headerMapF = AudioPlay.headers(), useWebView = true) AnalyzeUrl(url, headerMapF = AudioPlay.headers(), useWebView = true)
@ -197,13 +197,14 @@ class AudioPlayService : BaseService(),
/** /**
* 加载完成 * 加载完成
*/ */
override fun onPrepared(mp: MediaPlayer?) { override fun onPrepared(mp: MediaPlayer) {
if (pause) return
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mediaPlayer.playbackParams = mediaPlayer.playbackParams.apply { speed = playSpeed } mediaPlayer.playbackParams = mediaPlayer.playbackParams.apply { speed = playSpeed }
} else { } else {
mediaPlayer.start() mediaPlayer.start()
} }
AudioPlay.status = Status.PLAY
postEvent(EventBus.AUDIO_STATE, Status.PLAY)
mediaPlayer.seekTo(position) mediaPlayer.seekTo(position)
postEvent(EventBus.AUDIO_SIZE, mediaPlayer.duration) postEvent(EventBus.AUDIO_SIZE, mediaPlayer.duration)
bookChapter?.let { bookChapter?.let {
@ -216,7 +217,7 @@ class AudioPlayService : BaseService(),
/** /**
* 播放出错 * 播放出错
*/ */
override fun onError(mp: MediaPlayer?, what: Int, extra: Int): Boolean { override fun onError(mp: MediaPlayer, what: Int, extra: Int): Boolean {
if (!mediaPlayer.isPlaying) { if (!mediaPlayer.isPlaying) {
AudioPlay.status = Status.STOP AudioPlay.status = Status.STOP
postEvent(EventBus.AUDIO_STATE, Status.STOP) postEvent(EventBus.AUDIO_STATE, Status.STOP)
@ -228,7 +229,7 @@ class AudioPlayService : BaseService(),
/** /**
* 播放结束 * 播放结束
*/ */
override fun onCompletion(mp: MediaPlayer?) { override fun onCompletion(mp: MediaPlayer) {
handler.removeCallbacks(mpRunnable) handler.removeCallbacks(mpRunnable)
AudioPlay.next(this) AudioPlay.next(this)
} }

@ -80,9 +80,6 @@ object AudioPlay {
fun skipTo(context: Context, index: Int) { fun skipTo(context: Context, index: Int) {
Coroutine.async { Coroutine.async {
book?.let { book -> book?.let { book ->
val isPlay = !AudioPlayService.pause
pause(context)
status = Status.STOP
durChapterIndex = index durChapterIndex = index
durPageIndex = 0 durPageIndex = 0
book.durChapterIndex = durChapterIndex book.durChapterIndex = durChapterIndex
@ -91,9 +88,7 @@ object AudioPlay {
App.db.bookChapterDao().getChapter(book.bookUrl, durChapterIndex)?.let { chapter -> App.db.bookChapterDao().getChapter(book.bookUrl, durChapterIndex)?.let { chapter ->
postEvent(EventBus.AUDIO_SUB_TITLE, chapter.title) postEvent(EventBus.AUDIO_SUB_TITLE, chapter.title)
} }
if (isPlay) { play(context)
play(context)
}
} }
} }
} }
@ -104,7 +99,6 @@ object AudioPlay {
if (book.durChapterIndex <= 0) { if (book.durChapterIndex <= 0) {
return@let return@let
} }
pause(context)
durChapterIndex-- durChapterIndex--
durPageIndex = 0 durPageIndex = 0
book.durChapterIndex = durChapterIndex book.durChapterIndex = durChapterIndex
@ -113,9 +107,7 @@ object AudioPlay {
App.db.bookChapterDao().getChapter(book.bookUrl, durChapterIndex)?.let { chapter -> App.db.bookChapterDao().getChapter(book.bookUrl, durChapterIndex)?.let { chapter ->
postEvent(EventBus.AUDIO_SUB_TITLE, chapter.title) postEvent(EventBus.AUDIO_SUB_TITLE, chapter.title)
} }
if (AudioPlayService.isRun) { play(context)
play(context)
}
} }
} }
} }
@ -126,7 +118,6 @@ object AudioPlay {
if (book.durChapterIndex >= book.totalChapterNum) { if (book.durChapterIndex >= book.totalChapterNum) {
return@let return@let
} }
pause(context)
durChapterIndex++ durChapterIndex++
durPageIndex = 0 durPageIndex = 0
book.durChapterIndex = durChapterIndex book.durChapterIndex = durChapterIndex
@ -135,9 +126,7 @@ object AudioPlay {
App.db.bookChapterDao().getChapter(book.bookUrl, durChapterIndex)?.let { chapter -> App.db.bookChapterDao().getChapter(book.bookUrl, durChapterIndex)?.let { chapter ->
postEvent(EventBus.AUDIO_SUB_TITLE, chapter.title) postEvent(EventBus.AUDIO_SUB_TITLE, chapter.title)
} }
if (AudioPlayService.isRun) { play(context)
play(context)
}
} }
} }
} }

Loading…
Cancel
Save