BdR

Audio source already exists, when a sound is played consecutively

Recommended Posts

I'm a bit confused about loading and playing sound effects.

My game is setup in different states, first the Preloader state makes sure all images and sounds are loaded. The GameState is the main game, this state is re-started for each next level. There are different levels but the state is the same, it just changes a _levelIndex variable and uses the same state.

The GameState adds the needed audio to the game in the .create() function, and this create() function is called every time the GameState is started. See code below

mygame.Preloader.prototype = {
	preload: function(){

		this.loadingbar_bg = this.add.sprite(80, 512, "loadingbar_bg");
		this.loadingbar_fill = this.add.sprite(80, 512, "loadingbar_fill");
		this.load.setPreloadSprite(this.loadingbar_fill);

		// load sounds
		this.load.audio("button", ["snd/button.mp3", "snd/button.ogg"]);
		this.load.audio("punch",  ["snd/punch.mp3",  "snd/punch.ogg"]);
		this.load.audio("coin",   ["snd/coin.mp3",   "snd/coin.ogg"]);
	},
	create: function() {
		this.state.start("MainGame");
	},
};
	
mygame.GameState.prototype = {

	create: function() {
		this.stage.backgroundColor = "#f0f";
		// etc.
		
		// sound effects
		this.sound1 = this.game.add.audio("button");
		this.sound2 = this.game.add.audio("punch");
		this.sound3 = this.game.add.audio("coin");
		//etc.
	},

	update: function() {
		if (hitFace) this.sound2.play();
	},
	
	doNextLevel: function() {
		this.sound1.play();
		this._levelIndex++; // next level
		this.state.start("MainGame"); // restart this state
	},
	//etc.
};

 

The problem is that when I play the punch sound a couple of times in a row, the console gives this warning: Phaser.Sound: Audio source already exists which Phaser raises in code here. This warning appears even when the GameState is started for the first time.

So my questions are:
1) What does this message "Audio source already exists" mean ? Or should I ignore it?
2) if I want to use a sound in a state, do I have to re-add it every time the state is started and .create() is called ?
3) also somewhat related, if I want to use the same sound sample in multiple different States (menu, game, options etc.) do I have to game.add.audio() it for each state?

Share this post


Link to post
Share on other sites

Thanks, but no that is not it. It's just an example code snippet. The actual code in my game is correctly calling it just once.

There is always a few seconds between playing the sound effect again, and by then the previous sample playback is already finished. But still the "Phaser.Sound: Audio source already exists" warning appears.

Share this post


Link to post
Share on other sites

I've made a sandbox with the StateManager restarting a State and using samples -> http://phaser.io/sandbox/lARYwwpN

However the sandbox is running Phaser 2.6.2 so I couldn't reproduce the warning "Audio source already exists" because that warning is not in the 2.6.2 release. Anyway, I use mp3 and ogg samples for the sound effects and I suspect it has to do with decoding the sounds.

My question is: do I have to decode the sound samples every time the player starts (or restarts) a level i.e. restart the GameState? In other words, if the GameState will be .create() each time a level is (re)started, will the decoded samples be destroyed and have to be reloaded each time? That seems wasteful, what is the best way to do this?

 

Share this post


Link to post
Share on other sites
4 hours ago, BdR said:

My question is: do I have to decode the sound samples every time the player starts (or restarts) a level i.e. restart the GameState? In other words, if the GameState will be .create() each time a level is (re)started, will the decoded samples be destroyed and have to be reloaded each time? That seems wasteful, what is the best way to do this?

 

@BdR , No. If you load sound assets in a state with the option "autodecode" (example: this.game.load.audio('blaster', 'audio/SoundEffects/blaster.mp3', true)), then the decoded sounds will be stored in cache and available for other states. You can use a state to load the common assets only one time (like in the typical loading screen with progress bar).

Regards.

Share this post


Link to post
Share on other sites

@onlycape The autoDecode is optional and defaults to true, so that is already the situation in my first post.

I guess my quesion boils down to this: If my game state is like so:

mygame.GameState.prototype = {
    create: function() {
        // add sound effects
        this.sound1 = this.game.add.audio("button");
        //etc.
    },

What happens when I do game.add.audio("button") in the .create() method?
Specifically what will happen when the State is restarted like 50 times or more?
Will Phaser load & reserve memory etc. 50 times for the same sample?
Will this impact overall performance?

Share this post


Link to post
Share on other sites
On 6/13/2018 at 3:54 PM, BdR said:

What does this message "Audio source already exists" mean ? Or should I ignore it?

It looks like this warning is spurious in this case, sorry about that.

Share this post


Link to post
Share on other sites

You need to use add.sound() only once per sound per game. The sounds aren't removed from the manager when you change or restart states.

Use game.debug.sound() to see how many are held in the manager.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.