优化
This commit is contained in:
@@ -4,6 +4,7 @@ export const AUDIO_FEEDBACK_SOURCES = Object.freeze({
|
|||||||
})
|
})
|
||||||
|
|
||||||
let audioContext = null
|
let audioContext = null
|
||||||
|
let hasStartedPlayback = false
|
||||||
|
|
||||||
function getAudioContext() {
|
function getAudioContext() {
|
||||||
if (audioContext) return audioContext
|
if (audioContext) return audioContext
|
||||||
@@ -22,9 +23,10 @@ export function playFeedbackSound(type) {
|
|||||||
if (!source || !player) return false
|
if (!source || !player) return false
|
||||||
|
|
||||||
try {
|
try {
|
||||||
player.stop()
|
if (hasStartedPlayback) player.stop()
|
||||||
player.src = source
|
player.src = source
|
||||||
player.play()
|
player.play()
|
||||||
|
hasStartedPlayback = true
|
||||||
return true
|
return true
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
return false
|
return false
|
||||||
@@ -48,4 +50,5 @@ export function destroyFeedbackSound() {
|
|||||||
if (!audioContext) return
|
if (!audioContext) return
|
||||||
try { audioContext.destroy() } catch (_) {}
|
try { audioContext.destroy() } catch (_) {}
|
||||||
audioContext = null
|
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(audio.playErrorSound(), true)
|
||||||
assert.equal(created, 1)
|
assert.equal(created, 1)
|
||||||
assert.deepEqual(calls, [
|
assert.deepEqual(calls, [
|
||||||
'stop', 'play:/static/mp3/success.mp3',
|
'play:/static/mp3/success.mp3',
|
||||||
'stop', 'play:/static/mp3/fail.mp3'
|
'stop', 'play:/static/mp3/fail.mp3'
|
||||||
])
|
])
|
||||||
audio.destroyFeedbackSound()
|
audio.destroyFeedbackSound()
|
||||||
|
|||||||
Reference in New Issue
Block a user