Continuing music into the next scene


#1

Is it possible to add a sound file that plays continuously, even when you switch scenes? As of now, I can only make the song start from the beginning of each new scene.


#2

Hi @Mark_Davis!

currently there’s no continuous music between scenes possible without coding it.

Below you can find a code snippet you can copy into every scene to make your music pick up where it left off. Make sure to upload your music to the Upload -> Audio upload - tab and replace the audio ID from the example with your own. If you are using stage sounds, remove them to avoid playing back the sound twice.

//Replace the soundId with your own music!
let soundId = "6O3KD00ze0AwTCveq7k0guZFVabtZ5iiV7jMthEFwAW";

Scene.loadSound(soundId, function (sound) {
    playMusic(sound);
});

function playMusic(music : Sound) {
    let currentPosition = Space.getProperty("trackPosition") || '0';
    
    music.play(true);
    music.setCurrentPosition(parseFloat(currentPosition));

    Scene.onExit(function() {
        Space.setProperty("trackPosition", music.currentPosition().toString());
    });
}

Here’s the example in action: https://cospac.es/edu/4CmI