pull/32/head
kunfei 5 years ago
parent b81bac5a52
commit 0a179d3a58
  1. 19
      app/src/main/java/io/legado/app/constant/RSSKeywords.kt
  2. 22
      app/src/main/java/io/legado/app/service/ReadAloudService.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"
}

@ -147,13 +147,14 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener,
} }
initMediaSession() initMediaSession()
initBroadcastReceiver() initBroadcastReceiver()
upMediaSessionPlaybackState() upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING)
upSpeechRate() upSpeechRate()
ReadAloudNotification.upNotification(this) ReadAloudNotification.upNotification(this)
} }
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_STOPPED)
clearTTS() clearTTS()
unregisterReceiver(broadcastReceiver) unregisterReceiver(broadcastReceiver)
postEvent(Bus.ALOUD_STATE, Status.STOP) postEvent(Bus.ALOUD_STATE, Status.STOP)
@ -231,6 +232,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener,
return return
} }
if (requestFocus()) { if (requestFocus()) {
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING)
postEvent(Bus.ALOUD_STATE, Status.PLAY) postEvent(Bus.ALOUD_STATE, Status.PLAY)
ReadAloudNotification.upNotification(this) ReadAloudNotification.upNotification(this)
for (i in nowSpeak until contentList.size) { for (i in nowSpeak until contentList.size) {
@ -322,7 +324,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener,
} }
/** /**
* 初始化MediaSession * 初始化MediaSession, 注册多媒体按钮
*/ */
private fun initMediaSession() { private fun initMediaSession() {
val mComponent = ComponentName(packageName, MediaButtonReceiver::class.java.name) val mComponent = ComponentName(packageName, MediaButtonReceiver::class.java.name)
@ -333,17 +335,21 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener,
mediaButtonIntent, PendingIntent.FLAG_CANCEL_CURRENT mediaButtonIntent, PendingIntent.FLAG_CANCEL_CURRENT
) )
mediaSessionCompat = mediaSessionCompat = MediaSessionCompat(
MediaSessionCompat(this, tag, mComponent, mediaButtonReceiverPendingIntent) this, tag, mComponent,
mediaButtonReceiverPendingIntent
)
mediaSessionCompat?.setCallback(object : MediaSessionCompat.Callback() { mediaSessionCompat?.setCallback(object : MediaSessionCompat.Callback() {
override fun onMediaButtonEvent(mediaButtonEvent: Intent): Boolean { override fun onMediaButtonEvent(mediaButtonEvent: Intent): Boolean {
return MediaButtonReceiver.handleIntent(this@ReadAloudService, mediaButtonEvent) return MediaButtonReceiver.handleIntent(this@ReadAloudService, mediaButtonEvent)
} }
}) })
mediaSessionCompat?.setMediaButtonReceiver(mediaButtonReceiverPendingIntent) mediaSessionCompat?.setMediaButtonReceiver(mediaButtonReceiverPendingIntent)
mediaSessionCompat?.isActive = true
} }
private fun initBroadcastReceiver() { private fun initBroadcastReceiver() {
//断开耳机监听
broadcastReceiver = object : BroadcastReceiver() { broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
val action = intent.action val action = intent.action
@ -360,6 +366,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener,
if (pause) postEvent(Bus.ALOUD_STATE, Status.PAUSE) if (pause) postEvent(Bus.ALOUD_STATE, Status.PAUSE)
this.pause = pause this.pause = pause
textToSpeech?.stop() textToSpeech?.stop()
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PAUSED)
ReadAloudNotification.upNotification(this) ReadAloudNotification.upNotification(this)
} }
@ -386,14 +393,11 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener,
return request == AudioManager.AUDIOFOCUS_REQUEST_GRANTED return request == AudioManager.AUDIOFOCUS_REQUEST_GRANTED
} }
private fun upMediaSessionPlaybackState() { private fun upMediaSessionPlaybackState(state: Int) {
mediaSessionCompat?.setPlaybackState( mediaSessionCompat?.setPlaybackState(
PlaybackStateCompat.Builder() PlaybackStateCompat.Builder()
.setActions(MediaHelp.MEDIA_SESSION_ACTIONS) .setActions(MediaHelp.MEDIA_SESSION_ACTIONS)
.setState( .setState(state, nowSpeak.toLong(), 1f)
if (speak) PlaybackStateCompat.STATE_PLAYING else PlaybackStateCompat.STATE_PAUSED,
nowSpeak.toLong(), 1f
)
.build() .build()
) )
} }

Loading…
Cancel
Save