This commit is contained in:
2026-09-20 15:03:34 +08:00
parent 4dc8899086
commit 375701fcba
2 changed files with 5 additions and 2 deletions
+4 -1
View File
@@ -4,6 +4,7 @@ export const AUDIO_FEEDBACK_SOURCES = Object.freeze({
})
let audioContext = null
let hasStartedPlayback = false
function getAudioContext() {
if (audioContext) return audioContext
@@ -22,9 +23,10 @@ export function playFeedbackSound(type) {
if (!source || !player) return false
try {
player.stop()
if (hasStartedPlayback) player.stop()
player.src = source
player.play()
hasStartedPlayback = true
return true
} catch (_) {
return false
@@ -48,4 +50,5 @@ export function destroyFeedbackSound() {
if (!audioContext) return
try { audioContext.destroy() } catch (_) {}
audioContext = null
hasStartedPlayback = false
}
+1 -1
View File
@@ -19,7 +19,7 @@ test('shared audio feedback reuses one player and switches success and error sou
assert.equal(audio.playErrorSound(), true)
assert.equal(created, 1)
assert.deepEqual(calls, [
'stop', 'play:/static/mp3/success.mp3',
'play:/static/mp3/success.mp3',
'stop', 'play:/static/mp3/fail.mp3'
])
audio.destroyFeedbackSound()