pull/32/head
kunfei 5 years ago
parent 8c456a8b62
commit ef0d366e26
  1. 19
      app/src/main/java/io/legado/app/help/MediaHelp.kt
  2. 19
      app/src/main/java/io/legado/app/service/ReadAloudService.kt

@ -1,8 +1,13 @@
package io.legado.app.help package io.legado.app.help
import android.content.Context import android.content.Context
import android.media.AudioAttributes
import android.media.AudioFocusRequest
import android.media.AudioManager
import android.media.MediaPlayer import android.media.MediaPlayer
import android.os.Build
import android.support.v4.media.session.PlaybackStateCompat import android.support.v4.media.session.PlaybackStateCompat
import androidx.annotation.RequiresApi
import io.legado.app.R import io.legado.app.R
object MediaHelp { object MediaHelp {
@ -14,6 +19,20 @@ object MediaHelp {
or PlaybackStateCompat.ACTION_STOP or PlaybackStateCompat.ACTION_STOP
or PlaybackStateCompat.ACTION_SEEK_TO) 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) { fun playSilentSound(mContext: Context) {
try { try {
// Stupid Android 8 "Oreo" hack to make media buttons work // Stupid Android 8 "Oreo" hack to make media buttons work

@ -3,7 +3,6 @@ package io.legado.app.service
import android.app.PendingIntent import android.app.PendingIntent
import android.content.* import android.content.*
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.media.AudioAttributes
import android.media.AudioFocusRequest import android.media.AudioFocusRequest
import android.media.AudioManager import android.media.AudioManager
import android.os.Build import android.os.Build
@ -80,7 +79,9 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
isRun = true isRun = true
textToSpeech = TextToSpeech(this, this) textToSpeech = TextToSpeech(this, this)
audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
initFocusRequest() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mFocusRequest = MediaHelp.getFocusRequest(this)
}
initMediaSession() initMediaSession()
initBroadcastReceiver() initBroadcastReceiver()
upMediaSessionPlaybackState() upMediaSessionPlaybackState()
@ -151,20 +152,6 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
mediaSessionCompat?.setMediaButtonReceiver(mediaButtonReceiverPendingIntent) 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() { private fun initBroadcastReceiver() {
broadcastReceiver = object : BroadcastReceiver() { broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {

Loading…
Cancel
Save