Allow disabling and enabling sound
This commit is contained in:
parent
51bd80f697
commit
d3e990a87c
|
@ -2,6 +2,20 @@ import assets from 'url:../../../Assets/Audio/**/*.ogg'
|
||||||
|
|
||||||
export const audio: Record<string, string> = assets
|
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
|
// Preload 'em
|
||||||
for (let [key, value] of (Object.entries(assets) as [string,string][])) {
|
for (let [key, value] of (Object.entries(assets) as [string,string][])) {
|
||||||
let audio = new Audio()
|
let audio = new Audio()
|
||||||
|
@ -14,6 +28,7 @@ for (let [key, value] of (Object.entries(assets) as [string,string][])) {
|
||||||
|
|
||||||
let playingSongAudio: HTMLAudioElement
|
let playingSongAudio: HTMLAudioElement
|
||||||
export function playSong(name: string) {
|
export function playSong(name: string) {
|
||||||
|
if (!enabled) return
|
||||||
// Replace old audio and start crossfading.
|
// Replace old audio and start crossfading.
|
||||||
if (playingSongAudio && !playingSongAudio.paused) {
|
if (playingSongAudio && !playingSongAudio.paused) {
|
||||||
;((playingSongAudio: HTMLAudioElement) => {
|
;((playingSongAudio: HTMLAudioElement) => {
|
||||||
|
@ -60,6 +75,7 @@ export function playSong(name: string) {
|
||||||
|
|
||||||
export function playSound(name: string): HTMLAudioElement {
|
export function playSound(name: string): HTMLAudioElement {
|
||||||
let s = new Audio()
|
let s = new Audio()
|
||||||
|
if (!enabled) return s
|
||||||
|
|
||||||
s.src = audio[name]
|
s.src = audio[name]
|
||||||
s.autoplay = true
|
s.autoplay = true
|
||||||
|
|
Loading…
Reference in New Issue
Block a user