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