feat: 优化

pull/209/head
kunfei 5 years ago
parent 31d05fd900
commit 0dcc7ca840
  1. 16
      app/src/main/java/io/legado/app/service/BaseReadAloudService.kt

@ -44,7 +44,7 @@ abstract class BaseReadAloudService : BaseService(),
private lateinit var audioManager: AudioManager private lateinit var audioManager: AudioManager
private var mFocusRequest: AudioFocusRequest? = null private var mFocusRequest: AudioFocusRequest? = null
private var broadcastReceiver: BroadcastReceiver? = null private var broadcastReceiver: BroadcastReceiver? = null
private var mediaSessionCompat: MediaSessionCompat? = null private lateinit var mediaSessionCompat: MediaSessionCompat
private var title: String = "" private var title: String = ""
private var subtitle: String = "" private var subtitle: String = ""
internal val contentList = arrayListOf<String>() internal val contentList = arrayListOf<String>()
@ -59,6 +59,7 @@ abstract class BaseReadAloudService : BaseService(),
isRun = true isRun = true
audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
mFocusRequest = MediaHelp.getFocusRequest(this) mFocusRequest = MediaHelp.getFocusRequest(this)
mediaSessionCompat = MediaSessionCompat(this, "readAloud")
initMediaSession() initMediaSession()
initBroadcastReceiver() initBroadcastReceiver()
upNotification() upNotification()
@ -72,7 +73,7 @@ abstract class BaseReadAloudService : BaseService(),
unregisterReceiver(broadcastReceiver) unregisterReceiver(broadcastReceiver)
postEvent(EventBus.ALOUD_STATE, Status.STOP) postEvent(EventBus.ALOUD_STATE, Status.STOP)
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_STOPPED) upMediaSessionPlaybackState(PlaybackStateCompat.STATE_STOPPED)
mediaSessionCompat?.release() mediaSessionCompat.release()
} }
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
@ -202,7 +203,7 @@ abstract class BaseReadAloudService : BaseService(),
* 更新媒体状态 * 更新媒体状态
*/ */
private fun upMediaSessionPlaybackState(state: Int) { private fun upMediaSessionPlaybackState(state: Int) {
mediaSessionCompat?.setPlaybackState( mediaSessionCompat.setPlaybackState(
PlaybackStateCompat.Builder() PlaybackStateCompat.Builder()
.setActions(MediaHelp.MEDIA_SESSION_ACTIONS) .setActions(MediaHelp.MEDIA_SESSION_ACTIONS)
.setState(state, nowSpeak.toLong(), 1f) .setState(state, nowSpeak.toLong(), 1f)
@ -214,13 +215,12 @@ abstract class BaseReadAloudService : BaseService(),
* 初始化MediaSession, 注册多媒体按钮 * 初始化MediaSession, 注册多媒体按钮
*/ */
private fun initMediaSession() { private fun initMediaSession() {
mediaSessionCompat = MediaSessionCompat(this, "readAloud") 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@BaseReadAloudService, mediaButtonEvent) return MediaButtonReceiver.handleIntent(this@BaseReadAloudService, mediaButtonEvent)
} }
}) })
mediaSessionCompat?.setMediaButtonReceiver( mediaSessionCompat.setMediaButtonReceiver(
PendingIntent.getBroadcast( PendingIntent.getBroadcast(
this, this,
0, 0,
@ -233,7 +233,7 @@ abstract class BaseReadAloudService : BaseService(),
PendingIntent.FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT
) )
) )
mediaSessionCompat?.isActive = true mediaSessionCompat.isActive = true
} }
/** /**
@ -323,7 +323,7 @@ abstract class BaseReadAloudService : BaseService(),
) )
builder.setStyle( builder.setStyle(
androidx.media.app.NotificationCompat.MediaStyle() androidx.media.app.NotificationCompat.MediaStyle()
.setMediaSession(mediaSessionCompat?.sessionToken) .setMediaSession(mediaSessionCompat.sessionToken)
.setShowActionsInCompactView(0, 1, 2) .setShowActionsInCompactView(0, 1, 2)
) )
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)

Loading…
Cancel
Save