diff --git a/app/src/main/java/com/osfans/trime/data/soundeffect/SoundEffectManager.kt b/app/src/main/java/com/osfans/trime/data/soundeffect/SoundEffectManager.kt index 1d32fa437a..66882262cb 100644 --- a/app/src/main/java/com/osfans/trime/data/soundeffect/SoundEffectManager.kt +++ b/app/src/main/java/com/osfans/trime/data/soundeffect/SoundEffectManager.kt @@ -8,6 +8,7 @@ import com.charleskorn.kaml.Yaml import com.charleskorn.kaml.YamlConfiguration import com.osfans.trime.data.base.DataManager import com.osfans.trime.data.prefs.AppPrefs +import com.osfans.trime.ime.keyboard.InputFeedbackManager import com.osfans.trime.util.FileUtils import timber.log.Timber import java.io.File @@ -62,6 +63,7 @@ object SoundEffectManager { } activeSoundEffect = effect soundEffectPref = name + InputFeedbackManager.reloadSoundEffects() } fun init() { diff --git a/app/src/main/java/com/osfans/trime/ime/keyboard/InputFeedbackManager.kt b/app/src/main/java/com/osfans/trime/ime/keyboard/InputFeedbackManager.kt index 469749f0ee..d93d9d28d5 100644 --- a/app/src/main/java/com/osfans/trime/ime/keyboard/InputFeedbackManager.kt +++ b/app/src/main/java/com/osfans/trime/ime/keyboard/InputFeedbackManager.kt @@ -22,6 +22,7 @@ import com.osfans.trime.data.soundeffect.SoundEffectManager import splitties.systemservices.audioManager import splitties.systemservices.vibrator import timber.log.Timber +import java.util.concurrent.ConcurrentHashMap /** * Manage the key press effects, such as vibration, sound, speaking and so on. @@ -33,18 +34,19 @@ object InputFeedbackManager { private var soundPool: SoundPool? = null private var effectPlayProgress = 0 - private val cachedSoundIds = SparseIntArray(30) + private val cachedSoundIds = SparseIntArray() + + private val loadedSounds = ConcurrentHashMap() + private var effectHash = 0 fun init(context: Context) { try { tts = TextToSpeech(context, null) soundPool = - SoundPool - .Builder() + SoundPool.Builder() .setMaxStreams(3) .setAudioAttributes( - AudioAttributes - .Builder() + AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) .build(), @@ -55,19 +57,35 @@ object InputFeedbackManager { } private fun cacheSoundId() { + if (!soundEffectEnabled) return + + if (SoundEffectManager.activeSoundEffect == null) { + SoundEffectManager.init() + } + + val paths = SoundEffectManager.activeAudioPaths + val hash = paths.hashCode() + + if (hash == effectHash) return + cachedSoundIds.clear() - SoundEffectManager.activeAudioPaths.forEachIndexed { i, path -> - val id = soundPool?.load(path, 1) ?: 0 - if (id != 0 && !cachedSoundIds.containsValue(id)) { - cachedSoundIds.put(i, id) + + paths.forEachIndexed { i, path -> + val soundId = loadedSounds.getOrPut(path) { soundPool?.load(path, 1) ?: 0 } + if (soundId != 0 && !cachedSoundIds.containsValue(soundId)) { + cachedSoundIds.put(i, soundId) } } + + effectHash = hash } fun startInput() { - if (SoundEffectManager.activeSoundEffect == null) { - SoundEffectManager.init() - } + cacheSoundId() + } + + fun reloadSoundEffects() { + effectHash = 0 cacheSoundId() } @@ -131,7 +149,6 @@ object InputFeedbackManager { break } } - Timber.d("without melody: index: $index, sounds.size=${sounds.size}") index } } @@ -212,5 +229,9 @@ object InputFeedbackManager { tts = null soundPool?.release() soundPool = null + + loadedSounds.clear() + cachedSoundIds.clear() + effectHash = 0 } }