play audio on web on start
This commit is contained in:
18
audio.sex
18
audio.sex
@@ -15,6 +15,7 @@
|
||||
|
||||
;; NULL when no music is playing
|
||||
(var current-music (* Music) NULL)
|
||||
(var audio-unlocked bool false)
|
||||
|
||||
(fn load-sound ((file (* const char))) Sound
|
||||
(var sound Sound (LoadSound file))
|
||||
@@ -70,7 +71,24 @@
|
||||
(pub fn audio-play-fishing-music () void
|
||||
(switch-music (& fishing-music)))
|
||||
|
||||
;; Browsers keep the audio context suspended until the first user gesture.
|
||||
;; Restart the current track once after that gesture so the music actually begins.
|
||||
#+emscripten
|
||||
(pub fn audio-unlock () void
|
||||
(if audio-unlocked
|
||||
(return))
|
||||
(= audio-unlocked true)
|
||||
(if (!= current-music NULL)
|
||||
(do
|
||||
(StopMusicStream (* current-music))
|
||||
(PlayMusicStream (* current-music)))))
|
||||
|
||||
#-emscripten
|
||||
(pub fn audio-unlock () void
|
||||
(return))
|
||||
|
||||
(pub fn audio-play-menu-click () void
|
||||
(audio-unlock)
|
||||
(if (IsSoundValid menu-click)
|
||||
(PlaySound menu-click)))
|
||||
|
||||
|
||||
5
menu.sex
5
menu.sex
@@ -67,7 +67,10 @@
|
||||
(= clicked true)
|
||||
(.quit (. state app-state))))
|
||||
|
||||
;; Any menu key press is a UI interaction too
|
||||
;; Any menu key press or click is a UI interaction too
|
||||
(var key-pressed bool (!= 0 (GetKeyPressed)))
|
||||
(var mouse-pressed bool (IsMouseButtonPressed MOUSE-BUTTON-LEFT))
|
||||
(if (|| mouse-pressed key-pressed)
|
||||
(audio-unlock))
|
||||
(if (|| clicked key-pressed)
|
||||
(audio-play-menu-click)))
|
||||
|
||||
11
shell.html
11
shell.html
@@ -47,6 +47,17 @@
|
||||
<canvas id="canvas" tabindex="-1" oncontextmenu="event.preventDefault()"></canvas>
|
||||
<p>Instructions go here.</p>
|
||||
<script>
|
||||
// Browsers suspend audio until the first user gesture. miniaudio only
|
||||
// installs click/touchend handlers, so make keyboard/pointer input
|
||||
// unlock the audio context too.
|
||||
const unlockEvents = ['keydown', 'pointerdown'];
|
||||
const unlockAudio = () => {
|
||||
if (!window.miniaudio || typeof window.miniaudio.unlock !== 'function') return;
|
||||
window.miniaudio.unlock();
|
||||
unlockEvents.forEach((type) => removeEventListener(type, unlockAudio, true));
|
||||
};
|
||||
unlockEvents.forEach((type) => addEventListener(type, unlockAudio, true));
|
||||
|
||||
var Module = {
|
||||
canvas: document.getElementById('canvas'),
|
||||
print: console.log.bind(console),
|
||||
|
||||
Reference in New Issue
Block a user