From df31bb23652f959b82ad82723c782b9bb1bfbca2 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 3 Nov 2019 23:03:23 +0800 Subject: [PATCH] up --- .../main/java/io/legado/app/constant/Bus.kt | 2 ++ .../io/legado/app/service/AudioPlayService.kt | 28 +++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/legado/app/constant/Bus.kt b/app/src/main/java/io/legado/app/constant/Bus.kt index d81f8f224..4ebc61a08 100644 --- a/app/src/main/java/io/legado/app/constant/Bus.kt +++ b/app/src/main/java/io/legado/app/constant/Bus.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" } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/service/AudioPlayService.kt b/app/src/main/java/io/legado/app/service/AudioPlayService.kt index 958c7b87d..fab29f096 100644 --- a/app/src/main/java/io/legado/app/service/AudioPlayService.kt +++ b/app/src/main/java/io/legado/app/service/AudioPlayService.kt @@ -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(this, action) }