# Problem switching audio?

**URL:** <https://phaser.discourse.group/t/problem-switching-audio/5229>\
**Category:** Phaser 3\
**Created:** [February 17, 2020, 7:28pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229 "2020-02-17T19:28:52Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [February 21, 2020, 6:40am UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/22 "2020-02-21T06:40:02Z")

</div>

> <https://gist.github.com/samme/b959e2b812dd26b92d5f3ded8e62449c>

---

<div class="post-metadata">

**Author:** ![Thundros](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/thundros/32/2057_2.png) [@Thundros](https://phaser.discourse.group/u/Thundros)\
**Post date:** [February 21, 2020, 7:31pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/23 "2020-02-21T19:31:33Z")

</div>

@samme : thank you! this helped alot! Although, it helps me to understand the code better when i use `this. __`. So I went ahead & did that, but for some reason when I call more than 1 `__ switchAudio ( )` it does not switch to the new sound. It just plays both at the same time.

All the background music / sound effects data is located in `'vars.js'`

Here’s a direct link to the `'demo'` : [https://thundros.github.io/soundtest/](https://thundros.github.io/soundtest/)

Here’s a direct link to `vars.js` : [https://thundros.github.io/res/game/js/vars.js](https://thundros.github.io/res/game/js/vars.js)

Here’s a direct link to `sfx.js` : [https://thundros.github.io/res/game/js/sfx.js](https://thundros.github.io/res/game/js/sfx.js)

Here’s a direct link to `'main.js'` : [https://thundros.github.io/res/game/js/main.js](https://thundros.github.io/res/game/js/main.js)

The code I’m looking at is in `sfx.js`

Line{s} : `'75-96'`

The code is being called in `'main.js'`

Line{s} : `'27-39'`

Here’s the code I’m looking at in `'sfx.js'` :

```javascript
this.__switchAudio = function (__objData ) {

	this. __objData =__ objData;

	this. __scene = this.__ objData.scene;
	this. __sound = this.__ objData.sound;
	this. __record = this.__ objData.record;
	this. __currentSound = this.__ objData.currentSound;

	// `this` is the scene

	console.log ( this.__currentSound );

	if ( this.__currentSound ) {
		this.__currentSound.stop ( );
		this.__sound.remove ( this.__currentSound );
	}

	this. __currentSound = this.__ sound.add ( this. __record.key, this.__ record.config );
	this.__currentSound.play ( );

}

```

Here is the code I’m looking at in `'main.js'` :

```javascript
this.__create = function ( ) {

	this.sfx = new sfx ( );

	this.__currentSound = null;

	this.sfx.__switchAudio({ 
		scene : this, 
		sound : this.sound, 
		record : __music [0], 
		currentSound : this.__currentSound, 
	});

	this.sfx.__switchAudio({ 
		scene : this, 
		sound : this.sound, 
		record : __music [1], 
		currentSound : this.__currentSound, 
	});

}

```

Any help as always is GREATLY appreciated!

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [February 21, 2020, 7:43pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/24 "2020-02-21T19:43:51Z")

</div>

That is a working example, in 75 lines. Use it.

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [February 21, 2020, 8:01pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/25 "2020-02-21T20:01:50Z")

</div>

> [@Thundros](#):
>
> Although, it helps me to understand the code better when i use `this.__` .

Don’t do that, you’ll just add bugs.

---

<div class="post-metadata">

**Author:** ![Thundros](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/thundros/32/2057_2.png) [@Thundros](https://phaser.discourse.group/u/Thundros)\
**Post date:** [February 21, 2020, 8:02pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/26 "2020-02-21T20:02:48Z")

</div>

@samme : I don’t understand how that would add bugs.

---

<div class="post-metadata">

**Author:** ![snowbillr](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/snowbillr/32/332_2.png) [@snowbillr](https://phaser.discourse.group/u/snowbillr)\
**Post date:** [February 21, 2020, 8:31pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/27 "2020-02-21T20:31:30Z")

</div>

You introduced a bug when you rewrote the function. In your version of the `switchAudio` function, you assign the passed in audio to `this. __currentSound` before you stop the value that `this.__ currentSound` held from the previous call. So your code never stops the sound value that was passed in the first time the function was called. Look again at @samme’s example which stops the current sound first, then assigns the passed in sound to it so it can be referenced next time.

---

<div class="post-metadata">

**Author:** ![Thundros](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/thundros/32/2057_2.png) [@Thundros](https://phaser.discourse.group/u/Thundros)\
**Post date:** [February 25, 2020, 12:47am UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/29 "2020-02-25T00:47:37Z")

</div>

so how can i get `'PlayAudio ( )'` to still play the background music, but also play say a jump sound when jump is triggered, but only play it once per jump instead of playing 1 sound then overlapping another sound & overlapping another sound?

This is the `'AddAudio ( )'` function & the `'PlayAudio ( )'` function :

```javascript
this.AddAudio = function ( __objData )  

{

	this. __objData =__ objData;

	this. __snd = this.__ objData.sound;
	this. __key = this.__ objData.key;
	this. __config = this.__ objData.config;

	if ( typeof ( this.__objData ) !== 'object' ) { return console.error ( 'ERROR :: { Please ensure you are using an `object` for `objectData` & try again } !' ); }

	this. __currentSound = this.__ snd.add ( this. __key, this.__ config );

	return this.__currentSound;

}

this.PlayAudio = function ( __objData )

{

	this. __objData =__ objData;

	this. __sound = this.__ objData.sound;
	this. __record = this.__ objData.record;

	if ( typeof ( this.__objData ) !== 'object' ) { return console.error ( 'ERROR :: { Please ensure you are using an `object` for `objectData` & try again } !' ); }

	__currentSound = this.AddAudio ({
		sound : this.__sound, 
		key : this.__record.key, 
		config : this.__record.config, 
	});

	__currentSound.play ( );

}

```

This is how they’re called :

```javascript

// *** INSIDE the 'PlayAudio ( )' function : 

__currentSound = this.AddAudio ({
	sound : this.__sound, 
	key : this.__record.key, 
	config : this.__record.config, 
});

// *** OUTSIDE the 'PlayAudio ( )' function : 

__m.PlayAudio ({
	sound : this.__sound, 
	record : __soundfx [0], 
});

```

Any help as always is most appreciated!

---

<div class="post-metadata">

**Author:** ![Thundros](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/thundros/32/2057_2.png) [@Thundros](https://phaser.discourse.group/u/Thundros)\
**Post date:** [February 26, 2020, 6:46pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/33 "2020-02-26T18:46:31Z")

</div>

so i refactored my ENTIRE game to NOT include `'this. __'` after having figured out where to put `__ isPlayerJumping` & having it return correctly, I cannot get it to still play the background music, but when activating any sound effect also playing that ONCE. and then when jumping again, resetting the old jump sound so it doesn’t overlap with the old sound effect. NOTE : that the only reason the functions have `'this.'` are because they are all contained within a central function that gets called as `'mylib = new functionNameHere ( );'`

then the calls look like this :

`'mylib.PlayAudio ({'`  
`'currentSound : music [0]'`  
`'});'`

Here is the code I’m using :

```javascript
this.AddAudio = function ( __objData )  

{

    __objData =__ objData;

    __snd =__ objData.sound;
    __key =__ objData.key;
    __config =__ objData.config;

    if ( typeof ( __objData ) !== 'object' ) { return console.error ( 'ERROR :: { Please ensure you are using an `object` for `objectData` & try again } !' ); }

    __currentSound =__ snd.add ( __key,__ config );

    return __currentSound;

}

this.PlayAudio = function ( __objData )

{

    __objData =__ objData;

    __sound =__ objData.sound;
    __record =__ objData.record;

    if ( typeof ( __objData ) !== 'object' ) { return console.error ( 'ERROR :: { Please ensure you are using an `object` for `objectData` & try again } !' ); }

    __oldSound =__ currentSound;

    if ( __oldSound.isPlaying === true ) {

        this.StopAudio ({
            currentSound : __currentSound, 
        });

        /*
        this.DestroyAudio ({
            sound : __sound, 
            currentSound : __currentSound, 
        });
        */

        __currentSound = this.AddAudio ({
            sound : __sound, 
            key : __record.key, 
            config : __record.config, 
        });

        __currentSound.play ( );

    }

}

```

Any help as always is GREATLY appreciated!

Thank You!

---

<div class="post-metadata">

**Author:** ![Thundros](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/thundros/32/2057_2.png) [@Thundros](https://phaser.discourse.group/u/Thundros)\
**Post date:** [February 27, 2020, 12:18am UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/34 "2020-02-27T00:18:22Z")

</div>

Here’s a video of what it is doing :

[https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/c/c0d708fbf0a6b42677c3b21044b3fe855fd2ba18.mp4](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/c/c0d708fbf0a6b42677c3b21044b3fe855fd2ba18.mp4)

[Previous page](https://phaser.discourse.group/t/problem-switching-audio/5229.md?page=1)
