diff --git a/src/lib/components/SoundControl.svelte b/src/lib/components/SoundControl.svelte new file mode 100644 index 0000000..7cfad9e --- /dev/null +++ b/src/lib/components/SoundControl.svelte @@ -0,0 +1,59 @@ + + +{#if resolvedSoundPreference !== undefined} + +{/if} + + diff --git a/src/lib/components/icons/SpeakerMute.svelte b/src/lib/components/icons/SpeakerMute.svelte new file mode 100644 index 0000000..9a31b75 --- /dev/null +++ b/src/lib/components/icons/SpeakerMute.svelte @@ -0,0 +1,14 @@ + + + diff --git a/src/lib/components/icons/SpeakerWave.svelte b/src/lib/components/icons/SpeakerWave.svelte new file mode 100644 index 0000000..6b7b1e4 --- /dev/null +++ b/src/lib/components/icons/SpeakerWave.svelte @@ -0,0 +1,14 @@ + + + diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 821ef28..3a45d2e 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -1,3 +1,4 @@ export const THEME_KEY = 'connect4:theme'; +export const SOUND_KEY = 'connect4:sound'; export const BOARD_VALUE_FOR_PLAYER1 = 1; export const BOARD_VALUE_FOR_PLAYER2 = 2; diff --git a/src/lib/store/index.ts b/src/lib/store/index.ts new file mode 100644 index 0000000..ae6bfd7 --- /dev/null +++ b/src/lib/store/index.ts @@ -0,0 +1,3 @@ +import { writable } from 'svelte/store'; + +export const soundEnabled = writable(true); diff --git a/src/lib/utils/sounds.ts b/src/lib/utils/sounds.ts index 075faa7..33249cf 100644 --- a/src/lib/utils/sounds.ts +++ b/src/lib/utils/sounds.ts @@ -1,3 +1,6 @@ +import { soundEnabled } from '$lib/store'; +import { get } from 'svelte/store'; + let coinDropSounds: HTMLAudioElement[]; let winningSound: HTMLAudioElement; @@ -21,15 +24,17 @@ export function initSounds() { } export function playCoinDrop(rowNumber: number) { - // https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement/Audio#determining_when_playback_can_begin const coinDropSound = coinDropSounds[rowNumber]; - if (coinDropSound.readyState >= 2) { + // https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement/Audio#determining_when_playback_can_begin + if (coinDropSound.readyState >= 2 && get(soundEnabled)) { + coinDropSound.volume = 0.75; coinDropSound.play(); } } export function playWinningCelebration() { - if (winningSound.readyState >= 2) { + if (winningSound.readyState >= 2 && get(soundEnabled)) { + winningSound.volume = 0.5; winningSound.play(); } } diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 0127c54..28fb1fc 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -6,6 +6,7 @@ import Footer from '$lib/components/Footer.svelte'; import GoogleAnalytics from '$lib/components/GoogleAnalytics.svelte'; import MetaTags from '$lib/components/MetaTags.svelte'; + import SoundControl from '$lib/components/SoundControl.svelte'; @@ -20,6 +21,7 @@
+
diff --git a/static/audio/winning-celebration.mp3 b/static/audio/winning-celebration.mp3 index 5aac18d..79e3ad2 100644 Binary files a/static/audio/winning-celebration.mp3 and b/static/audio/winning-celebration.mp3 differ