Allow disabling and enabling sound

This commit is contained in:
Ketchetwahmeegwun T. Southall 2022-01-29 19:46:06 -08:00
parent 51bd80f697
commit d3e990a87c

View File

@ -2,6 +2,20 @@ import assets from 'url:../../../Assets/Audio/**/*.ogg'
export const audio: Record<string, string> = assets
export let enabled = true
export function enableSound() {
enabled = true
if (playingSongAudio) {
playingSongAudio.play()
}
}
export function disableSound() {
enabled = false
if (playingSongAudio) {
playingSongAudio.pause()
}
}
// Preload 'em
for (let [key, value] of (Object.entries(assets) as [string,string][])) {
let audio = new Audio()
@ -14,6 +28,7 @@ for (let [key, value] of (Object.entries(assets) as [string,string][])) {
let playingSongAudio: HTMLAudioElement
export function playSong(name: string) {
if (!enabled) return
// Replace old audio and start crossfading.
if (playingSongAudio && !playingSongAudio.paused) {
;((playingSongAudio: HTMLAudioElement) => {
@ -60,6 +75,7 @@ export function playSong(name: string) {
export function playSound(name: string): HTMLAudioElement {
let s = new Audio()
if (!enabled) return s
s.src = audio[name]
s.autoplay = true