pull/1421/head
gedoor 3 years ago
parent 4de11cb0ce
commit 8e47b3c251
  1. 38
      app/src/main/java/io/legado/app/service/BaseReadAloudService.kt

@ -44,10 +44,16 @@ abstract class BaseReadAloudService : BaseService(),
} }
} }
private lateinit var audioManager: AudioManager private val audioManager: AudioManager by lazy {
private var mFocusRequest: AudioFocusRequestCompat? = null getSystemService(Context.AUDIO_SERVICE) as AudioManager
private var broadcastReceiver: BroadcastReceiver? = null }
private lateinit var mediaSessionCompat: MediaSessionCompat private val mFocusRequest: AudioFocusRequestCompat by lazy {
MediaHelp.getFocusRequest(this)
}
private val mediaSessionCompat: MediaSessionCompat by lazy {
MediaSessionCompat(this, "readAloud")
}
private var hasFocus = requestFocus()
internal val contentList = arrayListOf<String>() internal val contentList = arrayListOf<String>()
internal var nowSpeak: Int = 0 internal var nowSpeak: Int = 0
internal var readAloudNumber: Int = 0 internal var readAloudNumber: Int = 0
@ -55,13 +61,18 @@ abstract class BaseReadAloudService : BaseService(),
internal var pageIndex = 0 internal var pageIndex = 0
private var dsJob: Job? = null private var dsJob: Job? = null
private val broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (AudioManager.ACTION_AUDIO_BECOMING_NOISY == intent.action) {
pauseReadAloud(true)
}
}
}
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
isRun = true isRun = true
pause = false pause = false
audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
mFocusRequest = MediaHelp.getFocusRequest(this)
mediaSessionCompat = MediaSessionCompat(this, "readAloud")
initMediaSession() initMediaSession()
initBroadcastReceiver() initBroadcastReceiver()
upNotification() upNotification()
@ -258,13 +269,6 @@ abstract class BaseReadAloudService : BaseService(),
* 断开耳机监听 * 断开耳机监听
*/ */
private fun initBroadcastReceiver() { private fun initBroadcastReceiver() {
broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (AudioManager.ACTION_AUDIO_BECOMING_NOISY == intent.action) {
pauseReadAloud(true)
}
}
}
val intentFilter = IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY) val intentFilter = IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY)
registerReceiver(broadcastReceiver, intentFilter) registerReceiver(broadcastReceiver, intentFilter)
} }
@ -276,13 +280,18 @@ abstract class BaseReadAloudService : BaseService(),
when (focusChange) { when (focusChange) {
AudioManager.AUDIOFOCUS_GAIN -> { AudioManager.AUDIOFOCUS_GAIN -> {
AppLog.put("重新获得焦点, 恢复播放") AppLog.put("重新获得焦点, 恢复播放")
hasFocus = true
if (!pause) resumeReadAloud() if (!pause) resumeReadAloud()
} }
AudioManager.AUDIOFOCUS_LOSS -> { AudioManager.AUDIOFOCUS_LOSS -> {
AppLog.put("永久丢失焦点") AppLog.put("永久丢失焦点")
if (!hasFocus) {
pauseReadAloud(true)
}
} }
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT -> { AudioManager.AUDIOFOCUS_LOSS_TRANSIENT -> {
AppLog.put("暂时丢失焦点, 暂停播放") AppLog.put("暂时丢失焦点, 暂停播放")
hasFocus = false
if (!pause) pauseReadAloud(false) if (!pause) pauseReadAloud(false)
} }
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> { AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> {
@ -356,4 +365,5 @@ abstract class BaseReadAloudService : BaseService(),
stopSelf() stopSelf()
} }
} }
} }
Loading…
Cancel
Save