优化
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user