Created
February 28, 2023 16:46
-
-
Save juj/79e039fdf7bcaed3dafb5b313dd2c179 to your computer and use it in GitHub Desktop.
Hypothetical Emscripten <audio> based player
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
mergeInto(LibraryManager.library, { | |
preloaded_audio: {}, | |
preload_audio__deps: ['preloaded_audio'], | |
preload_audio: function(id, url) { | |
let audio = new Audio(UTF8ToString(url)); | |
_preloaded_audio[id] = audio; | |
audio.preload = 'auto'; | |
}, | |
play_audio: function(id, loop) { | |
let audio = _preloaded_audio[id]; | |
audio.loop = !!loop; | |
audio.play().catch(e => { | |
let deferPlay = () => { | |
audio.play().then(() => { | |
document.body.removeEventListener( | |
'keydown', deferPlay); | |
}); | |
}; | |
document.body.addEventListener( | |
'keydown', deferPlay); | |
}); | |
}, | |
}); |
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
extern "C" { | |
void preload_audio(int audioId, const char *url); | |
void play_audio(int audioId, bool loop); | |
} | |
enum { | |
AUDIO_BG_MUSIC = 0, | |
AUDIO_SFX1, | |
AUDIO_SFX2 | |
}; | |
int main() { | |
char *urls[]={"music.mp3","sfx1.wav","sfx2.wav"}; | |
for(int i=0;i<sizeof(urls)/sizeof(urls[0]);++i) | |
preload_audio(i, urls[i]); | |
play_audio(AUDIO_BG_MUSIC, /*loop=*/true); | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment