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 2cae122be..9d086e5c6 100644 --- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt @@ -6,6 +6,7 @@ import android.content.Intent import android.media.MediaPlayer import io.legado.app.constant.Bus import io.legado.app.utils.getPrefInt +import io.legado.app.utils.getPrefString import io.legado.app.utils.postEvent import kotlinx.coroutines.launch import org.jetbrains.anko.toast @@ -31,8 +32,8 @@ class HttpReadAloudService : BaseReadAloudService(), private fun getAudioPath(): String { val audioText = contentList[nowSpeak] - val spd = getPrefInt("ttsSpeechRate", 5) + 5 - val per = getPrefInt("ttsSpeechPer") + val spd = (getPrefInt("ttsSpeechRate", 25) + 5) / 5 + val per = getPrefString("ttsSpeechPer") ?: "0" return "http://tts.baidu.com/text2audio?idx=1&tex=$audioText&cuid=baidu_speech_demo&cod=2&lan=zh&ctp=1&pdt=1&spd=$spd&per=$per&vol=5&pit=5&_res_tag_=audio" } diff --git a/app/src/main/java/io/legado/app/ui/readbook/config/ReadAloudConfigDialog.kt b/app/src/main/java/io/legado/app/ui/readbook/config/ReadAloudConfigDialog.kt index 377ad0242..40661d5f4 100644 --- a/app/src/main/java/io/legado/app/ui/readbook/config/ReadAloudConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/readbook/config/ReadAloudConfigDialog.kt @@ -8,6 +8,8 @@ import android.view.View import android.view.ViewGroup import android.widget.LinearLayout import androidx.fragment.app.DialogFragment +import androidx.preference.ListPreference +import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import io.legado.app.R import io.legado.app.constant.Bus @@ -15,6 +17,7 @@ import io.legado.app.help.ReadAloud import io.legado.app.lib.theme.ATH import io.legado.app.service.BaseReadAloudService import io.legado.app.ui.readbook.Help +import io.legado.app.utils.getPrefString import io.legado.app.utils.postEvent class ReadAloudConfigDialog : DialogFragment() { @@ -55,7 +58,8 @@ class ReadAloudConfigDialog : DialogFragment() { } class ReadAloudPreferenceFragment : PreferenceFragmentCompat(), - SharedPreferences.OnSharedPreferenceChangeListener { + SharedPreferences.OnSharedPreferenceChangeListener, + Preference.OnPreferenceChangeListener { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { addPreferencesFromResource(R.xml.pref_config_aloud) @@ -64,6 +68,7 @@ class ReadAloudConfigDialog : DialogFragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) ATH.applyEdgeEffectColor(listView) + bindPreferenceSummaryToValue(findPreference("ttsSpeechPer")) } override fun onResume() { @@ -93,5 +98,29 @@ class ReadAloudConfigDialog : DialogFragment() { } } + override fun onPreferenceChange(preference: Preference?, newValue: Any?): Boolean { + val stringValue = newValue.toString() + + if (preference is ListPreference) { + val index = preference.findIndexOfValue(stringValue) + // Set the summary to reflect the new value. + preference.setSummary(if (index >= 0) preference.entries[index] else null) + } else { + // For all other preferences, set the summary to the value's + preference?.summary = stringValue + } + return true + } + + private fun bindPreferenceSummaryToValue(preference: Preference?) { + preference?.apply { + onPreferenceChangeListener = this@ReadAloudPreferenceFragment + onPreferenceChange( + this, + context.getPrefString(key) + ) + } + } + } } \ No newline at end of file diff --git a/app/src/main/res/xml/pref_config_aloud.xml b/app/src/main/res/xml/pref_config_aloud.xml index afccbe028..d142c526c 100644 --- a/app/src/main/res/xml/pref_config_aloud.xml +++ b/app/src/main/res/xml/pref_config_aloud.xml @@ -17,6 +17,7 @@