From 0a179d3a58940c333fd2037fcf14b5642d75e243 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 10 Sep 2019 23:13:19 +0800 Subject: [PATCH] up --- .../io/legado/app/constant/RSSKeywords.kt | 19 ++++++++++++++++ .../io/legado/app/service/ReadAloudService.kt | 22 +++++++++++-------- 2 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/io/legado/app/constant/RSSKeywords.kt diff --git a/app/src/main/java/io/legado/app/constant/RSSKeywords.kt b/app/src/main/java/io/legado/app/constant/RSSKeywords.kt new file mode 100644 index 000000000..5a4119e05 --- /dev/null +++ b/app/src/main/java/io/legado/app/constant/RSSKeywords.kt @@ -0,0 +1,19 @@ +package io.legado.app.constant + +object RSSKeywords { + + const val RSS_ITEM = "item" + const val RSS_ITEM_TITLE = "title" + const val RSS_ITEM_LINK = "link" + const val RSS_ITEM_AUTHOR = "dc:creator" + const val RSS_ITEM_CATEGORY = "category" + const val RSS_ITEM_THUMBNAIL = "media:thumbnail" + const val RSS_ITEM_ENCLOSURE = "enclosure" + const val RSS_ITEM_DESCRIPTION = "description" + const val RSS_ITEM_CONTENT = "content:encoded" + const val RSS_ITEM_PUB_DATE = "pubDate" + const val RSS_ITEM_TIME = "time" + const val RSS_ITEM_URL = "url" + const val RSS_ITEM_TYPE = "type" + const val RSS_ITEM_GUID = "guid" +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/service/ReadAloudService.kt b/app/src/main/java/io/legado/app/service/ReadAloudService.kt index cdad6785d..c6c2cec12 100644 --- a/app/src/main/java/io/legado/app/service/ReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/ReadAloudService.kt @@ -147,13 +147,14 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, } initMediaSession() initBroadcastReceiver() - upMediaSessionPlaybackState() + upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING) upSpeechRate() ReadAloudNotification.upNotification(this) } override fun onDestroy() { super.onDestroy() + upMediaSessionPlaybackState(PlaybackStateCompat.STATE_STOPPED) clearTTS() unregisterReceiver(broadcastReceiver) postEvent(Bus.ALOUD_STATE, Status.STOP) @@ -231,6 +232,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, return } if (requestFocus()) { + upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING) postEvent(Bus.ALOUD_STATE, Status.PLAY) ReadAloudNotification.upNotification(this) for (i in nowSpeak until contentList.size) { @@ -322,7 +324,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, } /** - * 初始化MediaSession + * 初始化MediaSession, 注册多媒体按钮 */ private fun initMediaSession() { val mComponent = ComponentName(packageName, MediaButtonReceiver::class.java.name) @@ -333,17 +335,21 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, mediaButtonIntent, PendingIntent.FLAG_CANCEL_CURRENT ) - mediaSessionCompat = - MediaSessionCompat(this, tag, mComponent, mediaButtonReceiverPendingIntent) + mediaSessionCompat = MediaSessionCompat( + this, tag, mComponent, + mediaButtonReceiverPendingIntent + ) mediaSessionCompat?.setCallback(object : MediaSessionCompat.Callback() { override fun onMediaButtonEvent(mediaButtonEvent: Intent): Boolean { return MediaButtonReceiver.handleIntent(this@ReadAloudService, mediaButtonEvent) } }) mediaSessionCompat?.setMediaButtonReceiver(mediaButtonReceiverPendingIntent) + mediaSessionCompat?.isActive = true } private fun initBroadcastReceiver() { + //断开耳机监听 broadcastReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val action = intent.action @@ -360,6 +366,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, if (pause) postEvent(Bus.ALOUD_STATE, Status.PAUSE) this.pause = pause textToSpeech?.stop() + upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PAUSED) ReadAloudNotification.upNotification(this) } @@ -386,14 +393,11 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, return request == AudioManager.AUDIOFOCUS_REQUEST_GRANTED } - private fun upMediaSessionPlaybackState() { + private fun upMediaSessionPlaybackState(state: Int) { mediaSessionCompat?.setPlaybackState( PlaybackStateCompat.Builder() .setActions(MediaHelp.MEDIA_SESSION_ACTIONS) - .setState( - if (speak) PlaybackStateCompat.STATE_PLAYING else PlaybackStateCompat.STATE_PAUSED, - nowSpeak.toLong(), 1f - ) + .setState(state, nowSpeak.toLong(), 1f) .build() ) }