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()
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()
)
}

Loading…
Cancel
Save