From b798543156a8d31b1bff154ac0b16817558bcc03 Mon Sep 17 00:00:00 2001 From: Horis <821938089@qq.com> Date: Mon, 19 Sep 2022 11:08:52 +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 --- .../io/legado/app/service/AudioPlayService.kt | 17 +++++++++++++++-- .../legado/app/service/BaseReadAloudService.kt | 17 +++++++++++++++-- .../legado/app/service/HttpReadAloudService.kt | 4 ++-- .../legado/app/service/TTSReadAloudService.kt | 4 ++-- .../app/ui/book/read/page/entities/TextPage.kt | 3 +++ 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/AudioPlayService.kt b/app/src/main/java/io/legado/app/service/AudioPlayService.kt index 2c949fc17..4f782dd7c 100644 --- a/app/src/main/java/io/legado/app/service/AudioPlayService.kt +++ b/app/src/main/java/io/legado/app/service/AudioPlayService.kt @@ -121,6 +121,7 @@ class AudioPlayService : BaseService(), override fun onDestroy() { super.onDestroy() isRun = false + abandonFocus() exoPlayer.release() mediaSessionCompat?.release() unregisterReceiver(broadcastReceiver) @@ -166,9 +167,12 @@ class AudioPlayService : BaseService(), /** * 暂停播放 */ - private fun pause() { + private fun pause(abandonFocus: Boolean = true) { try { pause = true + if (abandonFocus) { + abandonFocus() + } upPlayProgressJob?.cancel() position = exoPlayer.currentPosition.toInt() if (exoPlayer.isPlaying) exoPlayer.pause() @@ -466,7 +470,7 @@ class AudioPlayService : BaseService(), needResumeOnAudioFocusGain = true if (!pause) { needResumeOnAudioFocusGain = true - pause() + pause(false) } } AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> { @@ -549,6 +553,7 @@ class AudioPlayService : BaseService(), } /** + * 请求音频焦点 * @return 音频焦点 */ private fun requestFocus(): Boolean { @@ -558,4 +563,12 @@ class AudioPlayService : BaseService(), return MediaHelp.requestFocus(audioManager, mFocusRequest) } + /** + * 放弃音频焦点 + */ + private fun abandonFocus() { + @Suppress("DEPRECATION") + audioManager.abandonAudioFocus(this) + } + } \ No newline at end of file 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 91f09c082..607f968e4 100644 --- a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt @@ -92,6 +92,7 @@ abstract class BaseReadAloudService : BaseService(), super.onDestroy() isRun = false pause = true + abandonFocus() unregisterReceiver(broadcastReceiver) postEvent(EventBus.ALOUD_STATE, Status.STOP) upMediaSessionPlaybackState(PlaybackStateCompat.STATE_STOPPED) @@ -147,8 +148,11 @@ abstract class BaseReadAloudService : BaseService(), abstract fun playStop() @CallSuper - open fun pauseReadAloud() { + open fun pauseReadAloud(abandonFocus: Boolean = true) { pause = true + if (abandonFocus) { + abandonFocus() + } upNotification() upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PAUSED) postEvent(EventBus.ALOUD_STATE, Status.PAUSE) @@ -228,6 +232,7 @@ abstract class BaseReadAloudService : BaseService(), } /** + * 请求音频焦点 * @return 音频焦点 */ fun requestFocus(): Boolean { @@ -241,6 +246,14 @@ abstract class BaseReadAloudService : BaseService(), return requestFocus } + /** + * 放弃音频焦点 + */ + private fun abandonFocus() { + @Suppress("DEPRECATION") + audioManager.abandonAudioFocus(this) + } + /** * 更新媒体状态 */ @@ -302,7 +315,7 @@ abstract class BaseReadAloudService : BaseService(), AppLog.put("音频焦点暂时丢失并会很快再次获得,暂停朗读") if (!pause) { needResumeOnAudioFocusGain = true - pauseReadAloud() + pauseReadAloud(false) } } 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 91d35b80b..f6f022513 100644 --- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt @@ -271,8 +271,8 @@ class HttpReadAloudService : BaseReadAloudService(), } - override fun pauseReadAloud() { - super.pauseReadAloud() + override fun pauseReadAloud(abandonFocus: Boolean) { + super.pauseReadAloud(abandonFocus) 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 bd2cf2256..6c3465e18 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() { - super.pauseReadAloud() + override fun pauseReadAloud(abandonFocus: Boolean) { + super.pauseReadAloud(abandonFocus) textToSpeech?.stop() } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt index 3c69c1e19..620b18bc9 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt @@ -201,6 +201,9 @@ data class TextPage( val maxIndex = min(lineIndex, lineSize) for (index in 0 until maxIndex) { length += textLines[index].charSize + if (textLines[index].isParagraphEnd) { + length++ + } } return length + columnIndex }