From d441042af85143de052b2a960424fb3fa1bd942a Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 15 Sep 2022 20:02:18 +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 | 25 +++++++++---------- .../app/service/HttpReadAloudService.kt | 10 ++++---- .../legado/app/service/TTSReadAloudService.kt | 4 +-- 3 files changed, 19 insertions(+), 20 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 230d06de3..5d88f58e8 100644 --- a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt @@ -60,18 +60,18 @@ abstract class BaseReadAloudService : BaseService(), private val mediaSessionCompat: MediaSessionCompat by lazy { MediaSessionCompat(this, "readAloud") } - private var audioFocusLossTransient = false internal val contentList = arrayListOf() internal var nowSpeak: Int = 0 internal var readAloudNumber: Int = 0 internal var textChapter: TextChapter? = null internal var pageIndex = 0 + private var needResumeOnAudioFocusGain = false 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) + pauseReadAloud() } } } @@ -105,7 +105,7 @@ abstract class BaseReadAloudService : BaseService(), intent.getIntExtra("pageIndex", ReadBook.durPageIndex), intent.getIntExtra("startPos", 0) ) - IntentAction.pause -> pauseReadAloud(true) + IntentAction.pause -> pauseReadAloud() IntentAction.resume -> resumeReadAloud() IntentAction.upTtsSpeechRate -> upSpeechRate(true) IntentAction.prevParagraph -> prevP() @@ -138,6 +138,7 @@ abstract class BaseReadAloudService : BaseService(), open fun play() { isRun = true pause = false + needResumeOnAudioFocusGain = false upNotification() postEvent(EventBus.ALOUD_STATE, Status.PLAY) } @@ -145,8 +146,8 @@ abstract class BaseReadAloudService : BaseService(), abstract fun playStop() @CallSuper - open fun pauseReadAloud(pause: Boolean) { - BaseReadAloudService.pause = pause + open fun pauseReadAloud() { + pause = true upNotification() upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PAUSED) postEvent(EventBus.ALOUD_STATE, Status.PAUSE) @@ -278,9 +279,7 @@ abstract class BaseReadAloudService : BaseService(), override fun onAudioFocusChange(focusChange: Int) { when (focusChange) { AudioManager.AUDIOFOCUS_GAIN -> { - - audioFocusLossTransient = false - if (!pause) { + if (needResumeOnAudioFocusGain) { AppLog.put("音频焦点获得,继续朗读") resumeReadAloud() } else { @@ -289,14 +288,14 @@ abstract class BaseReadAloudService : BaseService(), } AudioManager.AUDIOFOCUS_LOSS -> { AppLog.put("音频焦点丢失,暂停朗读") - if (audioFocusLossTransient) { - pauseReadAloud(true) - } + pauseReadAloud() } AudioManager.AUDIOFOCUS_LOSS_TRANSIENT -> { AppLog.put("音频焦点暂时丢失并会很快再次获得,暂停朗读") - audioFocusLossTransient = true - if (!pause) pauseReadAloud(false) + if (!pause) { + needResumeOnAudioFocusGain = true + pauseReadAloud() + } } AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> { // 短暂丢失焦点,这种情况是被其他应用申请了短暂的焦点希望其他声音能压低音量(或者关闭声音)凸显这个声音(比如短信提示音), diff --git a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt index f408aa2f7..91d35b80b 100644 --- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt @@ -171,7 +171,7 @@ class HttpReadAloudService : BaseReadAloudService(), toastOnUi("js错误\n${it.localizedMessage}") it.printOnDebug() cancel() - pauseReadAloud(true) + pauseReadAloud() } is SocketTimeoutException, is ConnectException -> { downloadErrorNo++ @@ -179,7 +179,7 @@ class HttpReadAloudService : BaseReadAloudService(), val msg = "tts超时或连接错误超过5次\n${it.localizedMessage}" AppLog.put(msg, it) toastOnUi(msg) - pauseReadAloud(true) + pauseReadAloud() } else { downloadAudio() } @@ -192,7 +192,7 @@ class HttpReadAloudService : BaseReadAloudService(), if (downloadErrorNo > 5) { AppLog.put("TTS服务器连续5次错误,已暂停阅读。") toastOnUi("TTS服务器连续5次错误,已暂停阅读。") - pauseReadAloud(true) + pauseReadAloud() } else { AppLog.put("TTS下载音频出错,使用无声音频代替。\n朗读文本:$content") createSilentSound(fileName) @@ -271,8 +271,8 @@ class HttpReadAloudService : BaseReadAloudService(), } - override fun pauseReadAloud(pause: Boolean) { - super.pauseReadAloud(pause) + override fun pauseReadAloud() { + super.pauseReadAloud() kotlin.runCatching { playIndexJob?.cancel() exoPlayer.pause() diff --git a/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt b/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt index f6b4b9e08..bd2cf2256 100644 --- a/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt @@ -121,8 +121,8 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener /** * 暂停朗读 */ - override fun pauseReadAloud(pause: Boolean) { - super.pauseReadAloud(pause) + override fun pauseReadAloud() { + super.pauseReadAloud() textToSpeech?.stop() }