pull/46/head
kunfei 5 years ago
parent a66a921e1e
commit 9095783fc4
  1. 2
      app/src/main/java/io/legado/app/constant/Action.kt
  2. 12
      app/src/main/java/io/legado/app/service/AudioPlayService.kt
  3. 4
      app/src/main/java/io/legado/app/service/help/AudioPlay.kt
  4. 2
      app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt

@ -11,5 +11,5 @@ object Action {
const val prevParagraph = "prevParagraph" const val prevParagraph = "prevParagraph"
const val nextParagraph = "nextParagraph" const val nextParagraph = "nextParagraph"
const val upTtsSpeechRate = "upTtsSpeechRate" const val upTtsSpeechRate = "upTtsSpeechRate"
const val upProgress = "upProgress" const val adjustProgress = "adjustProgress"
} }

@ -78,6 +78,7 @@ class AudioPlayService : BaseService(),
Action.resume -> resume() Action.resume -> resume()
Action.addTimer -> addTimer() Action.addTimer -> addTimer()
Action.setTimer -> setTimer(intent.getIntExtra("minute", 0)) Action.setTimer -> setTimer(intent.getIntExtra("minute", 0))
Action.adjustProgress -> adjustProgress(intent.getIntExtra("position", position))
else -> stopSelf() else -> stopSelf()
} }
} }
@ -107,6 +108,7 @@ class AudioPlayService : BaseService(),
private fun pause(pause: Boolean) { private fun pause(pause: Boolean) {
this.pause = pause this.pause = pause
position = mediaPlayer.currentPosition
mediaPlayer.pause() mediaPlayer.pause()
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PAUSED) upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PAUSED)
postEvent(Bus.AUDIO_STATE, Status.PAUSE) postEvent(Bus.AUDIO_STATE, Status.PAUSE)
@ -116,14 +118,24 @@ class AudioPlayService : BaseService(),
private fun resume() { private fun resume() {
pause = false pause = false
mediaPlayer.start() mediaPlayer.start()
mediaPlayer.seekTo(position)
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING) upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING)
postEvent(Bus.AUDIO_STATE, Status.PLAY) postEvent(Bus.AUDIO_STATE, Status.PLAY)
upNotification() upNotification()
} }
private fun adjustProgress(position: Int) {
if (mediaPlayer.isPlaying) {
mediaPlayer.seekTo(position)
} else {
this.position = position
}
}
override fun onPrepared(mp: MediaPlayer?) { override fun onPrepared(mp: MediaPlayer?) {
if (pause) return if (pause) return
mp?.start() mp?.start()
mp?.seekTo(position)
postEvent(Bus.AUDIO_SIZE, mp?.duration) postEvent(Bus.AUDIO_SIZE, mp?.duration)
handler.post(mpRunnable) handler.post(mpRunnable)
} }

@ -41,10 +41,10 @@ object AudioPlay {
} }
} }
fun upProgress(context: Context, position: Int) { fun adjustProgress(context: Context, position: Int) {
if (AudioPlayService.isRun) { if (AudioPlayService.isRun) {
val intent = Intent(context, AudioPlayService::class.java) val intent = Intent(context, AudioPlayService::class.java)
intent.action = Action.upProgress intent.action = Action.adjustProgress
intent.putExtra("position", position) intent.putExtra("position", position)
context.startService(intent) context.startService(intent)
} }

@ -66,7 +66,7 @@ class AudioPlayActivity : VMBaseActivity<AudioPlayViewModel>(R.layout.activity_a
override fun onStopTrackingTouch(seekBar: SeekBar?) { override fun onStopTrackingTouch(seekBar: SeekBar?) {
adjustProgress = false adjustProgress = false
AudioPlay.upProgress(this@AudioPlayActivity, player_progress.progress) AudioPlay.adjustProgress(this@AudioPlayActivity, player_progress.progress)
} }
}) })
} }

Loading…
Cancel
Save