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

Loading…
Cancel
Save