From 8e47b3c25121a54c79496a40c7446adc88ea139f Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 9 Nov 2021 09:36:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/service/BaseReadAloudService.kt | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt index 5c832ec14..9823148b4 100644 --- a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt @@ -44,10 +44,16 @@ abstract class BaseReadAloudService : BaseService(), } } - private lateinit var audioManager: AudioManager - private var mFocusRequest: AudioFocusRequestCompat? = null - private var broadcastReceiver: BroadcastReceiver? = null - private lateinit var mediaSessionCompat: MediaSessionCompat + private val audioManager: AudioManager by lazy { + getSystemService(Context.AUDIO_SERVICE) as AudioManager + } + 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() internal var nowSpeak: Int = 0 internal var readAloudNumber: Int = 0 @@ -55,13 +61,18 @@ abstract class BaseReadAloudService : BaseService(), internal var pageIndex = 0 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() { super.onCreate() isRun = true pause = false - audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager - mFocusRequest = MediaHelp.getFocusRequest(this) - mediaSessionCompat = MediaSessionCompat(this, "readAloud") initMediaSession() initBroadcastReceiver() upNotification() @@ -258,13 +269,6 @@ abstract class BaseReadAloudService : BaseService(), * 断开耳机监听 */ 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) registerReceiver(broadcastReceiver, intentFilter) } @@ -276,13 +280,18 @@ abstract class BaseReadAloudService : BaseService(), when (focusChange) { AudioManager.AUDIOFOCUS_GAIN -> { AppLog.put("重新获得焦点, 恢复播放") + hasFocus = true if (!pause) resumeReadAloud() } AudioManager.AUDIOFOCUS_LOSS -> { AppLog.put("永久丢失焦点") + if (!hasFocus) { + pauseReadAloud(true) + } } AudioManager.AUDIOFOCUS_LOSS_TRANSIENT -> { AppLog.put("暂时丢失焦点, 暂停播放") + hasFocus = false if (!pause) pauseReadAloud(false) } AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> { @@ -356,4 +365,5 @@ abstract class BaseReadAloudService : BaseService(), stopSelf() } } + } \ No newline at end of file