pull/46/head
kunfei 5 years ago
parent b73bb1bfc4
commit df31bb2365
  1. 2
      app/src/main/java/io/legado/app/constant/Bus.kt
  2. 28
      app/src/main/java/io/legado/app/service/AudioPlayService.kt

@ -13,4 +13,6 @@ object Bus {
const val UP_CONFIG = "upConfig"
const val OPEN_CHAPTER = "openChapter"
const val REPLACE = "replace"
const val AUDIO_NEXT = "audioNext"
const val AUDIO_CUR_POS = "audioCurPos"
}

@ -24,6 +24,10 @@ import io.legado.app.help.MediaHelp
import io.legado.app.receiver.MediaButtonReceiver
import io.legado.app.ui.book.read.ReadBookActivity
import io.legado.app.utils.postEvent
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
class AudioPlayService : BaseService(), AudioManager.OnAudioFocusChangeListener,
@ -59,6 +63,12 @@ class AudioPlayService : BaseService(), AudioManager.OnAudioFocusChangeListener,
initMediaSession()
initBroadcastReceiver()
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING)
launch(IO) {
while (isActive) {
delay(1000)
postEvent(Bus.AUDIO_CUR_POS, mediaPlayer.currentPosition)
}
}
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
@ -84,20 +94,27 @@ class AudioPlayService : BaseService(), AudioManager.OnAudioFocusChangeListener,
super.onDestroy()
isRun = false
mediaSessionCompat?.release()
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_STOPPED)
}
private fun play(url: String) {
if (requestFocus()) {
mediaPlayer.reset()
mediaPlayer.setDataSource(url)
mediaPlayer.prepareAsync()
}
}
private fun pause(pause: Boolean) {
this.pause = pause
mediaPlayer.pause()
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PAUSED)
}
private fun resume() {
pause = false
mediaPlayer.start()
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING)
}
override fun onPrepared(mp: MediaPlayer?) {
@ -111,7 +128,7 @@ class AudioPlayService : BaseService(), AudioManager.OnAudioFocusChangeListener,
}
override fun onCompletion(mp: MediaPlayer?) {
postEvent(Bus.AUDIO_NEXT, 1)
}
private fun setTimer(minute: Int) {
@ -287,6 +304,13 @@ class AudioPlayService : BaseService(), AudioManager.OnAudioFocusChangeListener,
startForeground(112201, notification)
}
/**
* @return 音频焦点
*/
fun requestFocus(): Boolean {
return MediaHelp.requestFocus(audioManager, this, mFocusRequest)
}
private fun thisPendingIntent(action: String): PendingIntent? {
return IntentHelp.servicePendingIntent<AudioPlayService>(this, action)
}

Loading…
Cancel
Save