pull/34/head
kunfei 5 years ago
parent 1b66ef0a75
commit d8dc48e435
  1. 30
      app/src/main/java/io/legado/app/service/BaseReadAloudService.kt

@ -1,7 +1,10 @@
package io.legado.app.service package io.legado.app.service
import android.app.PendingIntent import android.app.PendingIntent
import android.content.* import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.media.AudioFocusRequest import android.media.AudioFocusRequest
import android.media.AudioManager import android.media.AudioManager
@ -11,6 +14,7 @@ import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat import android.support.v4.media.session.PlaybackStateCompat
import androidx.annotation.CallSuper import androidx.annotation.CallSuper
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseService import io.legado.app.base.BaseService
import io.legado.app.constant.Action import io.legado.app.constant.Action
@ -234,23 +238,25 @@ abstract class BaseReadAloudService : BaseService(),
* 初始化MediaSession, 注册多媒体按钮 * 初始化MediaSession, 注册多媒体按钮
*/ */
private fun initMediaSession() { private fun initMediaSession() {
val mediaButtonIntent = Intent(Intent.ACTION_MEDIA_BUTTON).apply {
component = ComponentName(packageName, MediaButtonReceiver::class.java.name)
}
val mediaButtonReceiverPendingIntent = PendingIntent.getBroadcast(
this,
0,
mediaButtonIntent,
PendingIntent.FLAG_CANCEL_CURRENT
)
mediaSessionCompat = MediaSessionCompat(this, TTSReadAloudService.tag) mediaSessionCompat = MediaSessionCompat(this, TTSReadAloudService.tag)
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(mediaButtonReceiverPendingIntent) mediaSessionCompat?.setMediaButtonReceiver(
PendingIntent.getBroadcast(
this,
0,
Intent(
Intent.ACTION_MEDIA_BUTTON,
null,
App.INSTANCE,
MediaButtonReceiver::class.java
),
PendingIntent.FLAG_CANCEL_CURRENT
)
)
mediaSessionCompat?.isActive = true mediaSessionCompat?.isActive = true
} }

Loading…
Cancel
Save