Jump to content

Sync SoundJS preloader with Phaser preloader


oliversb
 Share

Recommended Posts

I decided to use SoundJS for playing sounds? What would be the easiest way to sync the Phaser preloader with SoundJS sound loading. Would it be to use an event handler for when SoundJS has completed loading the sounds and then start preloading stuff with phaser, would it be I load the sound with Phaser and then (somehow) play it with SoundJS or is there better and easier ways of doing this?

 

If this is too difficult then please help me with the 'powerful sound engine' question, thanks.

Link to comment
Share on other sites

Haven't synced those before.. but obviously waiting SoundJS to load before Phaser starts to load doesn't sound too good (I would slap any fellow coder if he does something which blocks that much on purpose :D). If SoundJS has some event handler or callback "stuff is now loaded" I believe that you can use it as your advantage and use some flag (var loaded....) to determine when it is actually done.. later on, because you cant be really sure which of the stuff loads first (phaser or soundsjs stuff) - I would wrap everything in my phaser state update (and draw?) loop inside:  if(loaded) { .... } this way, I believe that you won't start updating or drawing the game before everything is actually loaded. Alternatively, you can just do this at top of update or draw method: if(!loaded) { return; } ... dunno if this helps, shooting at the dark here, but I cant be too wrong  ;)

 

Edit: Ahh, after answering I realized that this might not be enough because you probably need to block executing of phaser create method until sounds has been loaded.. maybe someone can give better insight how do you solve that one.. obviously create could run if soundjs is loaded, but if not, this could be solved with some timer which asks after certain time "...if loaded -> create..." sounds way toooooo poor though. damn it  :(

Link to comment
Share on other sites

Haven't synced those before.. but obviously waiting SoundJS to load before Phaser starts to load doesn't sound too good (I would slap any fellow coder if he does something which blocks that much on purpose :D). If SoundJS has some event handler or callback "stuff is now loaded" I believe that you can use it as your advantage and use some flag (var loaded....) to determine when it is actually done.. later on, because you cant be really sure which of the stuff loads first (phaser or soundsjs stuff) - I would wrap everything in my phaser state update (and draw?) loop inside:  if(loaded) { .... } this way, I believe that you won't start updating or drawing the game before everything is actually loaded. Alternatively, you can just do this at top of update or draw method: if(!loaded) { return; } ... dunno if this helps, shooting at the dark here, but I cant be too wrong  ;)

 

Edit: Ahh, after answering I realized that this might not be enough because you probably need to block executing of phaser create method until sounds has been loaded.. maybe someone can give better insight how do you solve that one.. obviously create could run if soundjs is loaded, but if not, this could be solved with some timer which asks after certain time "...if loaded -> create..." sounds way toooooo poor though. damn it  :(

Thanks for trying anyway.

Link to comment
Share on other sites

Haven't synced those before.. but obviously waiting SoundJS to load before Phaser starts to load doesn't sound too good (I would slap any fellow coder if he does something which blocks that much on purpose :D). If SoundJS has some event handler or callback "stuff is now loaded" I believe that you can use it as your advantage and use some flag (var loaded....) to determine when it is actually done.. later on, because you cant be really sure which of the stuff loads first (phaser or soundsjs stuff) - I would wrap everything in my phaser state update (and draw?) loop inside:  if(loaded) { .... } this way, I believe that you won't start updating or drawing the game before everything is actually loaded. Alternatively, you can just do this at top of update or draw method: if(!loaded) { return; } ... dunno if this helps, shooting at the dark here, but I cant be too wrong  ;)

 

Edit: Ahh, after answering I realized that this might not be enough because you probably need to block executing of phaser create method until sounds has been loaded.. maybe someone can give better insight how do you solve that one.. obviously create could run if soundjs is loaded, but if not, this could be solved with some timer which asks after certain time "...if loaded -> create..." sounds way toooooo poor though. damn it  :(

In regard to blocking create, couldn't I just use a separate state when preloading?? Thanks

Link to comment
Share on other sites

Yeah that's how I'd probably go about it - make a boot state and preload your Phaser assets, When your Phaser assets have preloaded (and the state has automatically advanced to 'create') you could then set off your SoundJS preloader, and when that's complete. advance from the boot state to your game or menu state.

Link to comment
Share on other sites

Yeah that's how I'd probably go about it - make a boot state and preload your Phaser assets, When your Phaser assets have preloaded (and the state has automatically advanced to 'create') you could then set off your SoundJS preloader, and when that's complete. advance from the boot state to your game or menu state.

 

Yeah, that will take more load time than letting them to load in parallel.. However, maybe it is those trade-offs which are needed to be made if using soundjs... Im actually curious why to use soundjs if phaser has the sound stuff as well.  :) ?

Link to comment
Share on other sites

Phaser doesn't make panning and other advanced sound stuff fool proof yet, and only has basic abstractions for things like volume and cue points. I think it's perfectly valid to use other dedicated libraries to enhance the parts of Phaser that are at the moment fairly simplistic.

 

The reason for setting SoundJS off in create is if you set SoundJS off in the preload state, you'd then have to take account of race situations where either one could finish first. By controlling the preload in stages, you ensure that you only have to cleanly handle one complete event yourself. I guess it will be a little slower to load but not that much slower as you're still downloading the same quantity of assets and the same amount of bandwidth is being used minus some negligible overhead.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...