Add audio caching to prevent the browser from imploding
This commit is contained in:
parent
f83ddcd81d
commit
a4c05bcc16
|
@ -41,6 +41,8 @@ for (let [key, value] of (Object.entries(audio))) {
|
|||
audio.src = value
|
||||
}
|
||||
|
||||
let audioCache: HTMLAudioElement[] = []
|
||||
|
||||
let playingSongAudio: HTMLAudioElement
|
||||
export function playSong(name: string) {
|
||||
if (!enabled) return
|
||||
|
@ -88,14 +90,23 @@ export function playSong(name: string) {
|
|||
}
|
||||
}
|
||||
|
||||
export function playSound(name: string, volume: number): HTMLAudioElement {
|
||||
let s = new Audio()
|
||||
if (!enabled) return s
|
||||
export function playSound(name: string, volume: number): HTMLAudioElement|undefined {
|
||||
if (!enabled) return
|
||||
let s = audioCache.pop()
|
||||
if (!s) {
|
||||
s = new Audio()
|
||||
s.autoplay = true
|
||||
s.preload = ''
|
||||
s.addEventListener('ended', () => {
|
||||
audioCache.push(s as HTMLAudioElement)
|
||||
})
|
||||
} else {
|
||||
console.log('reused')
|
||||
}
|
||||
|
||||
|
||||
s.src = audio[name]
|
||||
s.volume = volume
|
||||
s.autoplay = true
|
||||
s.preload = ''
|
||||
let promise = s.play()
|
||||
if (promise !== undefined) {
|
||||
promise.then(() => {
|
||||
|
|
Loading…
Reference in New Issue
Block a user