Keeping audio/music playing after changing scenes

Hi,

Was just wondering if it was at all possible to keep music playing whilst/after changing scene?

So if I go from scene 1 through to scene 2 and a song is 58 seconds in, am I able to either

a) get information to the new scene to start at 58 seconds?

or

b) have the music script ‘scene independent’?

Many thanks in advance,

James

Hi @James_Austin,

You can do that by adding the sound component to the app root entity, that way it won’t get destroyed when you dispose the previous scene.

That would be here:

this.app.root.addComponent("sound");
1 Like

Thanks for your quick reply @Leonidas.

I think I almost there. How do I access the sound component once I have changed scenes?

i.e. How do I access components that have been appended to app root entity?

Is there anyway rather than adding a component to the root entity I can just add an entity?

Thanks in advance,

James

I think you can keep using the same notation, that is the root entity of the application, not the scene. So it won’t be affected by scene changes:

this.app.root.sound.play('beep');

Hmm, not sure if there is any other way. You could try creating an entity in code, that isn’t appended to any other entity and add the sound component. For rendering and anything else that entity won’t be of much use, but curious if it can still activate its sound component and play sounds (I haven’t tried that).

2 Likes

Thank you very much yet again.

I realised about 30 seconds ago I was being a bit of an idiot and it was the root ‘ENTITY’ so I can access its components like I would any other entity. Will put all I have learned into practice now and hopefully should be good to go.

Again, many thanks,

James

1 Like

To anyone who may be looking at this in the future, above is the correct approach. Here is step by step what to do.

  1. Create a script, that adds a sound component to the root entity.
this.app.root.addComponent('sound');
  1. Add whatever audio you want to slots in the root entity sound component.
var song = this.app.assets.get(assetID);
    
this.app.root.sound.addSlot('music', {
    asset: song
});
  1. Access this asset as you would with any normal entity.
this.app.root.sound.play('music');

Again, thanks @Leonidas for your help assisting me in this matter.

5 Likes

That’s great, thanks for sharing a detailed solution here @James_Austin.

1 Like