From d3e990a87cf8f2115bdf4d790bb5b79e4559c707 Mon Sep 17 00:00:00 2001 From: kts of kettek Date: Sat, 29 Jan 2022 19:46:06 -0800 Subject: [PATCH] Allow disabling and enabling sound --- Engine/src/shared/audio.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Engine/src/shared/audio.ts b/Engine/src/shared/audio.ts index 7995dc5..61e41d1 100644 --- a/Engine/src/shared/audio.ts +++ b/Engine/src/shared/audio.ts @@ -2,6 +2,20 @@ import assets from 'url:../../../Assets/Audio/**/*.ogg' export const audio: Record = 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