Jump to content

Load sounds, after the game started


Recommended Posts

Hi, in not so far past, there was ugly bug in Chrome for Android, where decoding of music took ages. So, in my games I loaded only short intro music in preloader and rest of the music was loaded on background during gameplay.

Here is class (Typescript) I used for it. I had musics named Music0 to Music3 (value of Global.MAX_MUSIC = 4) What it does is: it creates new Phaser.Loader in conctructor. When I call loadMusic, it adds all music files to loader and starts loading. Whenever file is loaded, it calls onFileLoaded callback in which I can do whatever I need with fresh loaded file. In game I am playing all musics one after another starting from random one. Last method returns name of music to play - it checks whether it is loaded and decoded - it either return next music or any music available for playing or in worst case null if no music is ready.

module Shards {

    export class MusicManager {

        private _game: Phaser.Game;
        private _loader: Phaser.Loader;

        private _musicToPlay: number;

        // -------------------------------------------------------------------------
        public constructor(game: Phaser.Game) {
            this._game = game;
            this._loader = new Phaser.Loader(game);

            this._musicToPlay = -1;

        // -------------------------------------------------------------------------
        public loadMusic(): void {
            // load music
            var path = "assets/";
            for (var i = 0; i < Global.MAX_MUSIC; i++) {
                var music = "Music" + i;
                console.log("loading music ..." + music);

                /* if (this.game.device.ie && this.game.device.ieVersion <= 10) {
                    this._loader.audio(music, [path + music + ".mp3"]);
                } else */ {
                    this._loader.audio(music, [path + music + ".ogg", path + music + ".m4a"]);

            this._loader.onFileComplete.add(this.onFileComplete, this);

        // -------------------------------------------------------------------------
        public onFileComplete(progress: number, cacheKey: string, success: boolean, totalLoaded: number, totalFiles: number): void {
            //console.log("file loaded " + cacheKey + " adding to audio");

            var music = this._game.add.audio(cacheKey);
            Utils.AudioUtils.addMusic(cacheKey, music);

        // -------------------------------------------------------------------------
        public getMusic(): string {
            var index = this._musicToPlay;
            if (index === -1) {
                index = this._game.rnd.integerInRange(0, Global.MAX_MUSIC - 1);
                this._musicToPlay = index;

            do {
                var musicName = "Music" + index;
                console.log("request to play music ..." + musicName);

                if (this._game.cache.getSound(musicName) && this._game.cache.isSoundDecoded(musicName)) {
                    // index for next music
                    this._musicToPlay = (index + 1) % Global.MAX_MUSIC;
                    console.log("returning music " + musicName);
                    return musicName;

                index = (index + 1) % Global.MAX_MUSIC;
            } while (index !== this._musicToPlay);

            return null;


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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