diff --git a/app/src/main/java/io/legado/app/help/MediaHelp.kt b/app/src/main/java/io/legado/app/help/MediaHelp.kt index d2de56c2d..a7235a342 100644 --- a/app/src/main/java/io/legado/app/help/MediaHelp.kt +++ b/app/src/main/java/io/legado/app/help/MediaHelp.kt @@ -1,8 +1,13 @@ package io.legado.app.help import android.content.Context +import android.media.AudioAttributes +import android.media.AudioFocusRequest +import android.media.AudioManager import android.media.MediaPlayer +import android.os.Build import android.support.v4.media.session.PlaybackStateCompat +import androidx.annotation.RequiresApi import io.legado.app.R object MediaHelp { @@ -14,6 +19,20 @@ object MediaHelp { or PlaybackStateCompat.ACTION_STOP or PlaybackStateCompat.ACTION_SEEK_TO) + + @RequiresApi(Build.VERSION_CODES.O) + fun getFocusRequest(audioFocusChangeListener: AudioManager.OnAudioFocusChangeListener): AudioFocusRequest { + val mPlaybackAttributes = AudioAttributes.Builder() + .setUsage(AudioAttributes.USAGE_MEDIA) + .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) + .build() + return AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) + .setAudioAttributes(mPlaybackAttributes) + .setAcceptsDelayedFocusGain(true) + .setOnAudioFocusChangeListener(audioFocusChangeListener) + .build() + } + fun playSilentSound(mContext: Context) { try { // Stupid Android 8 "Oreo" hack to make media buttons work diff --git a/app/src/main/java/io/legado/app/service/ReadAloudService.kt b/app/src/main/java/io/legado/app/service/ReadAloudService.kt index f1d4d61cb..0edc9c55d 100644 --- a/app/src/main/java/io/legado/app/service/ReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/ReadAloudService.kt @@ -3,7 +3,6 @@ package io.legado.app.service import android.app.PendingIntent import android.content.* import android.graphics.BitmapFactory -import android.media.AudioAttributes import android.media.AudioFocusRequest import android.media.AudioManager import android.os.Build @@ -80,7 +79,9 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage isRun = true textToSpeech = TextToSpeech(this, this) audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager - initFocusRequest() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + mFocusRequest = MediaHelp.getFocusRequest(this) + } initMediaSession() initBroadcastReceiver() upMediaSessionPlaybackState() @@ -151,20 +152,6 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage mediaSessionCompat?.setMediaButtonReceiver(mediaButtonReceiverPendingIntent) } - private fun initFocusRequest() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - val mPlaybackAttributes = AudioAttributes.Builder() - .setUsage(AudioAttributes.USAGE_MEDIA) - .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) - .build() - mFocusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) - .setAudioAttributes(mPlaybackAttributes) - .setAcceptsDelayedFocusGain(true) - .setOnAudioFocusChangeListener(this) - .build() - } - } - private fun initBroadcastReceiver() { broadcastReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) {