pull/2319/head
kunfei 2 years ago
parent 97bae57675
commit fc84a5b9b2
  1. 7
      app/src/main/java/io/legado/app/help/MediaHelp.kt
  2. 2
      app/src/main/java/io/legado/app/service/AudioPlayService.kt
  3. 2
      app/src/main/java/io/legado/app/service/BaseReadAloudService.kt

@ -10,6 +10,7 @@ import androidx.media.AudioManagerCompat
import io.legado.app.R import io.legado.app.R
object MediaHelp { object MediaHelp {
const val MEDIA_SESSION_ACTIONS = (PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS const val MEDIA_SESSION_ACTIONS = (PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS
or PlaybackStateCompat.ACTION_REWIND or PlaybackStateCompat.ACTION_REWIND
or PlaybackStateCompat.ACTION_PLAY or PlaybackStateCompat.ACTION_PLAY
@ -32,18 +33,20 @@ object MediaHelp {
or PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE or PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE
or PlaybackStateCompat.ACTION_SET_CAPTIONING_ENABLED) or PlaybackStateCompat.ACTION_SET_CAPTIONING_ENABLED)
fun getFocusRequest(audioFocusChangeListener: AudioManager.OnAudioFocusChangeListener): AudioFocusRequestCompat { fun buildAudioFocusRequestCompat(
audioFocusChangeListener: AudioManager.OnAudioFocusChangeListener
): AudioFocusRequestCompat {
val mPlaybackAttributes = AudioAttributesCompat.Builder() val mPlaybackAttributes = AudioAttributesCompat.Builder()
.setUsage(AudioAttributesCompat.USAGE_MEDIA) .setUsage(AudioAttributesCompat.USAGE_MEDIA)
.setContentType(AudioAttributesCompat.CONTENT_TYPE_MUSIC) .setContentType(AudioAttributesCompat.CONTENT_TYPE_MUSIC)
.build() .build()
return AudioFocusRequestCompat.Builder(AudioManagerCompat.AUDIOFOCUS_GAIN) return AudioFocusRequestCompat.Builder(AudioManagerCompat.AUDIOFOCUS_GAIN)
.setAudioAttributes(mPlaybackAttributes) .setAudioAttributes(mPlaybackAttributes)
//.setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(audioFocusChangeListener) .setOnAudioFocusChangeListener(audioFocusChangeListener)
.build() .build()
} }
/** /**
* @return 音频焦点 * @return 音频焦点
*/ */

@ -62,7 +62,7 @@ class AudioPlayService : BaseService(),
} }
private val mFocusRequest: AudioFocusRequestCompat by lazy { private val mFocusRequest: AudioFocusRequestCompat by lazy {
MediaHelp.getFocusRequest(this) MediaHelp.buildAudioFocusRequestCompat(this)
} }
private val exoPlayer: ExoPlayer by lazy { private val exoPlayer: ExoPlayer by lazy {
ExoPlayer.Builder(this).setLoadControl( ExoPlayer.Builder(this).setLoadControl(

@ -56,7 +56,7 @@ abstract class BaseReadAloudService : BaseService(),
} }
private val mFocusRequest: AudioFocusRequestCompat by lazy { private val mFocusRequest: AudioFocusRequestCompat by lazy {
MediaHelp.getFocusRequest(this) MediaHelp.buildAudioFocusRequestCompat(this)
} }
private val mediaSessionCompat: MediaSessionCompat by lazy { private val mediaSessionCompat: MediaSessionCompat by lazy {
MediaSessionCompat(this, "readAloud") MediaSessionCompat(this, "readAloud")

Loading…
Cancel
Save