Continuing music into the next scene

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.

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

2 Likes

Hi Mark–it’s been two years, but could you possibly allow me to remix your game you shared? I am very confused as to what the text in red means, and I’m trying to help a 3rd grader!

Kelly,

   I don't know if you received my message, but again, I don't actually have an available project to share with you.  It was a student's project from 2 years ago that is no longer in my database.  However, you should be able to add the code above as suggested by Stefan.

   Hope this helps,
                   Mark

Thanks Mark! I’ll check it out!

Here’s the updated version of @Stefan’s code:

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

Sound.load(soundId, function (sound) {
    playMusic(sound);
});

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

    music.play(true);
    music.currentPosition = parseFloat(currentPosition);

    Application.onSceneExit(function() {
        Space.setProperty("trackPosition", music.currentPosition.toString());
    });
}
1 Like

There is no music that plays in this example. I have tried this script and the updated script, but it doesn’t work. Can anyone assist?

Thanks,

Melissa

Hi,

I am unable to get this script to work with my space. Can you please assist?

Thanks,

Melissa

Hi @rosentech,

You need to replace the soundId string with the ID of the music you want to use:

  1. Go to Upload > Sound
  2. Click the 3-dot menu on the sound you want to use, and select Copy ID
  3. Select the string in the code (e.g. “gzGCdNYwaMpNMwPxOkVkHVTJ9lc0ExrEis89Eyazz9Q”), and paste the new ID in.

Do this for all scenes. I did this with your CoSpace and it worked fine.

Hope that helps! Let me know if you have any further questions.

Geoff @ TechLeap

Hi Geoff,

My sound is already in there. It is in the link I posted. My sound ID is “r1/zwgpdbM8GZdTNlA1QqlmtjaYYE6anFLBcoubRz6W2zf”

I will try a new sound and see.

Regards,

Melissa Rosen

Did it work the 2nd time?