From 0c50b867ad9d0b5e792535c13b4372e73e98acec Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 19 Mar 2022 22:55:27 +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 --- .../main/java/io/legado/app/constant/PreferKey.kt | 1 + .../main/java/io/legado/app/help/config/AppConfig.kt | 12 +++++++++++- .../io/legado/app/service/HttpReadAloudService.kt | 6 ++++-- .../io/legado/app/service/TTSReadAloudService.kt | 5 +++-- .../app/ui/book/read/config/ReadAloudDialog.kt | 3 +-- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/io/legado/app/constant/PreferKey.kt b/app/src/main/java/io/legado/app/constant/PreferKey.kt index 79dae28d8..98b0bc7d6 100644 --- a/app/src/main/java/io/legado/app/constant/PreferKey.kt +++ b/app/src/main/java/io/legado/app/constant/PreferKey.kt @@ -27,6 +27,7 @@ object PreferKey { const val precisionSearch = "precisionSearch" const val readAloudByPage = "readAloudByPage" const val ttsEngine = "appTtsEngine" + const val ttsFollowSys = "ttsFollowSys" const val ttsSpeechRate = "ttsSpeechRate" const val prevKeys = "prevKeyCodes" const val nextKeys = "nextKeyCodes" diff --git a/app/src/main/java/io/legado/app/help/config/AppConfig.kt b/app/src/main/java/io/legado/app/help/config/AppConfig.kt index ed658e4a7..b069eddb8 100644 --- a/app/src/main/java/io/legado/app/help/config/AppConfig.kt +++ b/app/src/main/java/io/legado/app/help/config/AppConfig.kt @@ -169,12 +169,22 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener { } } + var ttsFlowSys: Boolean + get() = appCtx.getPrefBoolean(PreferKey.ttsFollowSys, true) + set(value) { + appCtx.putPrefBoolean(PreferKey.ttsFollowSys, value) + } + + const val defaultSpeechRate = 5 + var ttsSpeechRate: Int - get() = appCtx.getPrefInt(PreferKey.ttsSpeechRate, 5) + get() = appCtx.getPrefInt(PreferKey.ttsSpeechRate, defaultSpeechRate) set(value) { appCtx.putPrefInt(PreferKey.ttsSpeechRate, value) } + val speechRatePlay: Int get() = if (ttsFlowSys) defaultSpeechRate else ttsSpeechRate + var chineseConverterType: Int get() = appCtx.getPrefInt(PreferKey.chineseConverterType) set(value) { 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 399d339b2..46ef94b91 100644 --- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt @@ -41,6 +41,7 @@ class HttpReadAloudService : BaseReadAloudService(), private val ttsFolderPath: String by lazy { cacheDir.absolutePath + File.separator + "httpTTS" + File.separator } + private var speechRate: Int = AppConfig.speechRatePlay private val cacheFiles = hashSetOf() private var task: Coroutine<*>? = null private var playIndexJob: Job? = null @@ -113,7 +114,7 @@ class HttpReadAloudService : BaseReadAloudService(), contentList.forEachIndexed { index, content -> ensureActive() val fileName = - md5SpeakFileName(httpTts.url, AppConfig.ttsSpeechRate.toString(), content) + md5SpeakFileName(httpTts.url, speechRate.toString(), content) val speakText = content.replace(AppPattern.notReadAloudRegex, "") if (hasSpeakFile(fileName)) { //已经下载好的语音缓存 if (index == nowSpeak) { @@ -131,7 +132,7 @@ class HttpReadAloudService : BaseReadAloudService(), val analyzeUrl = AnalyzeUrl( httpTts.url, speakText = speakText, - speakSpeed = AppConfig.ttsSpeechRate, + speakSpeed = speechRate, source = httpTts, headerMapF = httpTts.getHeaderMap(true) ) @@ -317,6 +318,7 @@ class HttpReadAloudService : BaseReadAloudService(), override fun upSpeechRate(reset: Boolean) { task?.cancel() exoPlayer.stop() + speechRate = AppConfig.speechRatePlay downloadAudio() } 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 110a3816d..866339854 100644 --- a/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt @@ -106,13 +106,14 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener * 更新朗读速度 */ override fun upSpeechRate(reset: Boolean) { - if (this.getPrefBoolean("ttsFollowSys", true)) { + if (AppConfig.ttsFlowSys) { if (reset) { clearTTS() initTts() } } else { - textToSpeech?.setSpeechRate((AppConfig.ttsSpeechRate + 5) / 10f) + val speechRate = (AppConfig.ttsSpeechRate + 5) / 10f + textToSpeech?.setSpeechRate(speechRate) } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/ReadAloudDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/ReadAloudDialog.kt index 8fdc2e84a..136a2541c 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/ReadAloudDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/ReadAloudDialog.kt @@ -22,7 +22,6 @@ import io.legado.app.ui.widget.seekbar.SeekBarChangeListener import io.legado.app.utils.ColorUtils import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.observeEvent -import io.legado.app.utils.putPrefBoolean import io.legado.app.utils.viewbindingdelegate.viewBinding @@ -110,7 +109,7 @@ class ReadAloudDialog : BaseDialogFragment(R.layout.dialog_read_aloud) { llCatalog.setOnClickListener { callBack?.openChapterList() } llToBackstage.setOnClickListener { callBack?.finish() } cbTtsFollowSys.setOnCheckedChangeListener { _, isChecked -> - requireContext().putPrefBoolean("ttsFollowSys", isChecked) + AppConfig.ttsFlowSys = isChecked seekTtsSpeechRate.isEnabled = !isChecked upTtsSpeechRate() }