• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

SOLVED [HTML] How do I ensure that music is loaded and ready to play when the game starts?

Xalezar

Member
Hi everyone, so I've created an HTML5 build of my game. I want to make sure that the background music for my title screen, bgm_title, loads before I enter that room otherwise the player will not hear the music and probably start the game and miss hearing bgm_title. What is the best way to accomplish this?

Note: I am using audio groups, and bgm_title is in ag_bgm. All audio groups are loaded right at the start of the game.

Other question: How do I control which assets are loaded first? Background music files are often the largest, so I'd like to make sure a specific one bgm_title is loaded first, so that the audience will for sure hear the background music when they arrive at the title screen.

Current setup:

My background music logic is managed by an object, audio_manager, which continually checks if the current correct background music is playing. If not, then it will first stop whatever is playing then start playing the correct music (or play nothing, if silence was designated). It has a variable bgm_id which gets updated by calling a function audio_set_bgm();.

When the game starts, I'll have an "init" screen that asks the user to click the game area, to enable the browser audio context. After they have clicked it, I execute audio_set_bgm( bgm_title, ... ), the following code is run in the step event:

GML:
if ( clicked ) {

    title = "Loading ..."

    if ( audio_is_playing( bgm_title ) ) {
        instance_destroy( obj_fade_from_black );
        room_goto_ext( rm_title );
    }

}
I tested it a few times and it seems to work in the browser. I.e. the bgm_title asset will load first, so that it plays when the title screen displayed. However, it sometimes randomly gets stuck in this "loading" stage and never seems to proceed.
 
Last edited:

samspade

Member
Are you ensuring that the audio groups are fully loaded in before transferring to a room where they play? For example, I have something like this in my init room:

GML:
///step event of game manager
if (music_and_sound_loaded()) {
    room_goto(rm_main_menu);
}


///script asset
function music_and_sound_loaded() {
    return audio_group_is_loaded(audiogroup_music) && audio_group_is_loaded(audiogroup_sound);
}
So you stay in the init or loading room until the sound is actually done loading.
 
Top