Skip to content

Commit

Permalink
fix: failed to load sound effects
Browse files Browse the repository at this point in the history
再次弹出键盘丢失按键音效
  • Loading branch information
nopdan authored and WhiredPlanck committed Apr 26, 2024
1 parent fc8e3aa commit 1197273
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ object SoundEffectManager {

@JvmStatic
fun switchSound(name: String) {
if (getSound(name) == null) {
Timber.w("Unknown sound package name: $name")
return
getSound(name).let {
if (it == null) {
Timber.w("Unknown sound package name: $name")
return
}
currentSoundEffect = it
AppPrefs.defaultInstance().keyboard.customSoundPackage = name
}
AppPrefs.defaultInstance().keyboard.customSoundPackage = name
currentSoundEffect = getSound(name)!!
}

fun init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
Timber.d("Running Trime.onCreate")
ColorManager.init(resources.configuration)
textInputManager = TextInputManager(this@TrimeInputMethodService, rime)
InputFeedbackManager.init(this@TrimeInputMethodService)
InputFeedbackManager.init()
restartSystemStartTimingSync()
try {
for (listener in eventListeners) {
Expand Down Expand Up @@ -465,7 +465,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
Timber.d("onStartInputView: restarting=%s", restarting)
lifecycleScope.launch(Dispatchers.Main) {
rime.runOnReady {
InputFeedbackManager.loadSoundEffects()
InputFeedbackManager.loadSoundEffects(this@TrimeInputMethodService)
InputFeedbackManager.resetPlayProgress()
for (listener in eventListeners) {
listener.onStartInputView(attribute, restarting)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@ object InputFeedbackManager {
private var lastPressedKeycode = 0
private val soundIds: MutableList<Int> = mutableListOf()

fun init(context: Context) {
fun init() {
runCatching {
tts = TextToSpeech(context, null)
soundPool =
SoundPool.Builder()
.setMaxStreams(1)
.setAudioAttributes(
AudioAttributes.Builder()
.setLegacyStreamType(AudioManager.STREAM_SYSTEM)
.build(),
).build()
SoundEffectManager.init()
}.getOrElse {
Timber.w(it, "Failed to initialize InputFeedbackManager")
}
}

fun loadSoundEffects() {
fun loadSoundEffects(context: Context) {
tts = TextToSpeech(context, null)
soundPool =
SoundPool.Builder()
.setMaxStreams(1)
.setAudioAttributes(
AudioAttributes.Builder()
.setLegacyStreamType(AudioManager.STREAM_SYSTEM)
.build(),
).build()
SoundEffectManager.getActiveSoundFilePaths().onSuccess { path ->
soundIds.clear()
soundIds.addAll(path.map { soundPool?.load(it, 1) ?: 0 })
Expand Down

0 comments on commit 1197273

Please sign in to comment.