Jump to content

Loading audio outside preload ()


mrvinegar
 Share

Recommended Posts

I would suggest just adding a preload function to the state you need the audio for. However you can use game.load.audio like you're trying, but if it's not in a preload function you'll have to also start the loader yourself too (and listen for the complete event).

Link to comment
Share on other sites

  • 1 year later...

I'm a little late to the party, but for anyone else looking for how to do what Rich is describing, here's how I accomplished it:

playMusic: function () {  this.asyncLoad('song', 'song.mp3', function () {    var music = game.add.audio('song');    music.play();  });},asyncLoad: function(cacheKey, path, onLoad) {  this.load.audio(cacheKey, path);  this.load.start(); // start the loader  var index = window.setInterval(function() {    if (game.load.isLoading) {      return;    }    onLoad();    window.clearInterval(index);  }, 1000);}

Music is the last & largest item loaded, so it suffices to check isLoading (or hasLoaded) on some interval.

this here refers to the current state. game is a global but could also have been this.game (though that would probably require binding the callbacks).

(My other assets are small, and I don't need the music to start immediately.)

Link to comment
Share on other sites

  • 3 years later...
On 9/19/2015 at 4:25 AM, johncip said:

I'm a little late to the party, but for anyone else looking for how to do what Rich is describing, here's how I accomplished it:


playMusic: function () {  this.asyncLoad('song', 'song.mp3', function () {    var music = game.add.audio('song');    music.play();  });},asyncLoad: function(cacheKey, path, onLoad) {  this.load.audio(cacheKey, path);  this.load.start(); // start the loader  var index = window.setInterval(function() {    if (game.load.isLoading) {      return;    }    onLoad();    window.clearInterval(index);  }, 1000);}

Music is the last & largest item loaded, so it suffices to check isLoading (or hasLoaded) on some interval.

this here refers to the current state. game is a global but could also have been this.game (though that would probably require binding the callbacks).

(My other assets are small, and I don't need the music to start immediately.)

@johncip and @rich

It works, but it removes all over progress on a game which we get before the loading

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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