Skip to content

Commit

Permalink
enhancement: add tts volume slider to settings (not functional yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
elli-hae committed Feb 29, 2024
1 parent a94f835 commit 729a8d9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
10 changes: 10 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/AndroidTtsPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.content.Context
import android.os.Bundle
import android.speech.tts.TextToSpeech
import android.speech.tts.TextToSpeech.ERROR
import android.speech.tts.TextToSpeech.Engine.KEY_PARAM_VOLUME
import androidx.annotation.CheckResult
import com.ichi2.anki.AndroidTtsError.TtsErrorCode
import com.ichi2.compat.UtteranceProgressListenerCompat
Expand Down Expand Up @@ -82,6 +83,11 @@ class AndroidTtsPlayer(private val context: Context, private val voices: List<Tt
return this.voices
}

private fun getTtsVolume(): Float {
val sharedPref = context.getSharedPreferences(context.getString((R.string.tts_volume_preferences)), Context.MODE_PRIVATE)
return sharedPref.getInt("tts_volume", 100) / 100.0f
}

override suspend fun play(tag: TTSTag): TtsCompletionStatus {
val match = voiceForTag(tag)
if (match == null) {
Expand Down Expand Up @@ -119,6 +125,10 @@ class AndroidTtsPlayer(private val context: Context, private val voices: List<Tt
currentUtterance = this
cancelledUtterances.remove(this)
}
val bundle = Bundle()
val volume = getTtsVolume()
bundle.putFloat(KEY_PARAM_VOLUME, volume)

tts.speak(tag.fieldText, TextToSpeech.QUEUE_FLUSH, bundleFlyweight, utteranceId)

continuation.invokeOnCancellation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ class AdvancedSettingsFragment : SettingsFragment() {
return@setOnPreferenceChangeListener true
}

val ttsVolumePref = requirePreference<SeekBarPreference>(R.string.tts_volume_key)
ttsVolumePref.setOnPreferenceChangeListener { _, newValue ->
val newVolume = newValue as Int
val sharedPref = requireContext().getSharedPreferences(getString(R.string.tts_volume_preferences), Context.MODE_PRIVATE)
with(sharedPref.edit()) {
putInt(getString(R.string.tts_volume_key), newVolume)
apply()
}
true // Indicate that the change has been handled
}

// Configure "Reset languages" preference
requirePreference<Preference>(R.string.pref_reset_languages_key).setOnPreferenceClickListener {
AlertDialog.Builder(requireContext()).show {
Expand Down
2 changes: 2 additions & 0 deletions AnkiDroid/src/main/res/values/10-preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@
<string name="double_scrolling_gap_summ">Double the scroll gap with eReader</string>
<string name="swipe_sensitivity" maxLength="41">Swipe sensitivity</string>
<string name="tts" maxLength="41">Text to speech</string>
<string name="tts_volume" maxLength="41">TTS volume</string>
<string name="tts_summ">Reads out question and answer if no sound file is included</string>
<string name="tts_volume_preferences">tts_volume_preferences</string>
<!-- Sync -->
<string name="sync_fetch_missing_media" maxLength="41">Fetch media on sync</string>
<string name="sync_account" maxLength="41">AnkiWeb account</string>
Expand Down
1 change: 1 addition & 0 deletions AnkiDroid/src/main/res/values/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
<string name="enable_api_key">providerEnabled</string>
<string name="thirdparty_apps_key">thirdpartyapps_link</string>
<string name="tts_key">tts</string>
<string name="tts_volume_key">tts_volume</string>
<string name="double_scrolling_gap_key">double_scrolling</string>
<string name="html_javascript_debugging_key">html_javascript_debugging</string>
<string name="card_browser_external_context_menu_key">card_browser_enable_external_context_menu</string>
Expand Down
5 changes: 5 additions & 0 deletions AnkiDroid/src/main/res/xml/preferences_advanced.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,9 @@
android:key="@string/tts_key"
android:summary="@string/tts_summ"
android:title="@string/tts" />
<SeekBarPreference
android:key="@string/tts_volume_key"
android:title="@string/tts_volume"
android:defaultValue="100"
android:max="100" />
</PreferenceScreen>

0 comments on commit 729a8d9

Please sign in to comment.