Last active
August 1, 2025 03:25
-
-
Save KonnorRogers/57018c4c92708ba1b77540d64c81afa6 to your computer and use it in GitHub Desktop.
Stub to add to DragonRuby for unlocking audio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// end of dragonruby-html5-loader.js ... | |
// .dragonruby/stubs/html5/dragonruby-html5-loader.js | |
const audioContext = new AudioContext() | |
let hasPlayed = false | |
function startAudioContext(context){ | |
// this accomplishes the iOS specific requirement | |
var buffer = context.createBuffer(1, 1, context.sampleRate) | |
var source = context.createBufferSource() | |
source.buffer = buffer | |
source.connect(context.destination) | |
source.start(0) | |
// resume the audio context | |
if (context.resume){ | |
context.resume() | |
} | |
} | |
function unlockAudio () { | |
if (!hasPlayed) { | |
startAudioContext(audioContext) | |
hasPlayed = true | |
} | |
} | |
// iOS safari doesn't seem to consider "canvas" interactive for audio purposes. | |
const fullScreenButton = document.createElement("button") | |
// We cannot let the opacity be 0 or visibility: hidden; because Safari flags the button as not being an "interactive" | |
fullScreenButton.style = "position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; opacity: 0.01;" | |
document.body.append(fullScreenButton) | |
fullScreenButton.addEventListener("click", unlockAudio) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment