# 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:** 20\
**Page:** 1

<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 17, 2020, 7:28pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/1 "2020-02-17T19:28:52Z")

</div>

so I’m trying to create a sound with `this.sound.add` in 1 function, & play it with another function, but for some reason I’m getting `'this.sys.game.globals.model is undefined'`.

I have created a small demo in order to figure this out. Why is the above undefined? Here is the direct link to the `'main project'` : [https://thundros.github.io/sound-test/](https://thundros.github.io/sound-test/) | Here is the direct link to `'Model.js'` : [https://thundros.github.io/sound-test/res/menu-system/Model.js](https://thundros.github.io/sound-test/res/menu-system/Model.js) | Here is the direct link to `'main.js'` : [https://thundros.github.io/sound-test/res/game/js/main.js](https://thundros.github.io/sound-test/res/game/js/main.js) | Here is the direct link to `'sfx.js'` : [https://thundros.github.io/sound-test/res/game/js/sfx.js](https://thundros.github.io/sound-test/res/game/js/sfx.js)

Any help as always is most DEFINITELY appreciated!

Thank you!

---

<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 17, 2020, 8:30pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/2 "2020-02-17T20:30:10Z")

</div>

> [@Thundros](#):
>
> this.sys.game.globals.model is undefined

Phaser doesn’t define `this.sys.game.globals`, so do 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 17, 2020, 8:31pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/3 "2020-02-17T20:31:20Z")

</div>

@samme : how would i do so?

---

<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 17, 2020, 8:34pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/4 "2020-02-17T20:34:38Z")

</div>

I don’t know what it’s supposed to be. But you need something like

```javascript
this.sys.game.globals = { model: {} };

```

There’s already [this.sys.game.registry](https://photonstorm.github.io/phaser3-docs/Phaser.Game.html#registry) though, you can use that.

---

<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 17, 2020, 10:22pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/5 "2020-02-17T22:22:27Z")

</div>

@samme : I decided to stop using that old code & i created a new code.

i have come up with my own. how do i get it to switch to the sound i want say when i go to the next level?

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

{

    this. __objData =__ objData;

    this. __snd = this.__ objData.sound;
    this. __soundID = this.__ objData.soundID;
    this. __soundData = this.__ objData.soundData;

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

    console.error ( 'Loaded Sound ID' + ' :: ' + this.__soundID );
    console.error ( 'Loaded Sound Data' + ' :: ' + JSON.stringify ( this.__soundData ) );

    this. __sound = this.__ snd.add ( this. __soundID, this.__ soundData );

    return {

        sounds : {

            sound: this.__sound, 
            soundID : this.__soundID, 
            soundData : this.__soundData, 

        }, 

    };

}

this.IsSoundPlaying = function ( __sound )

{

    this. __sound =__ sound;

    return this.__sound.isPlaying;

}

this.PlayAudio = function ( __objData )

{

    this. __objData =__ objData;

    this. __snd = this.__ objData.sound;
    this. __soundID = this.__ objData.soundID;
    this. __soundData = this.__ objData.soundData;

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

    this.__soundPlaying = ( this.IsSoundPlaying ( this.__sound ) );
    console.error ( 'this. __soundPlaying :: ' + this.__ soundPlaying );

    this.StopAudio ( this.__sound );
    this.__sound.play ( );

}

this.StopAudio = function ( __sound )

{

    this. __sound =__ sound;

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

    this.__sound.stop ( );

}

```

the way it is called :

```javascript
// Create & Load our BGM{s}

this.__BGMTrack = [

	'bgMusic', 'lvl1Music', 'lvl1MusicA', 
	'lvl2Music', 

];

this.__BGMData = [

	{ 
		volume : 0.1, loop : true, 
	}, 

	{
		volume : 0.1, loop : true, 
	}, 

	{
		volume : 0.03, loop : true, 
	}, 

	{
		volume : 0.1, loop : true, 
	}, 

];

// Create & Load our sound{s}

this.__SFXTrack = [

	'jumpA', 

];

this.__SFXData = [

	{ 
		volume : 0.1, loop : true, 
	}, 

];

__BGMAudio =__ m.LoadAudio ( {
	sound : this.sound, 
	soundID : this.__BGMTrack [3], 
	soundData : this.__BGMData [3], 
}, 1 );

__m.PlayAudio ( {
	sound : __BGMAudio, 
}, 1 );

/*

__SFXAudio =__ m.LoadAudio ({
	sys : this.sys, 
	sound : this.sound, 
	soundID : this.__SFXTrack [0], 
	soundData : this.__SFXData [0], 
}, 1 );

*/

```

---

<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 18, 2020, 12:12am UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/6 "2020-02-18T00:12:25Z")

</div>

ok, so my code works fine to an extent, but for some reason using multiple  
`'__m.PlayAudio ( )'` calls, it only switches in the game. Not the title screen.

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

{

    this. __objData =__ objData;

    this. __snd = this.__ objData.sound;
    this. __soundID = this.__ objData.soundID;

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

    this.__soundPlaying = ( this.IsSoundPlaying ( this.__sound ) );
    console.error ( 'this. __soundPlaying :: ' + this.__ soundPlaying );

    if ( this.IsSoundPlaying ( this.__sound ) === true ) {

        this.StopAudio ( this.__sound );
        this.__sound.play ( this.__soundID );

    }

    else {

        this.__sound.play ( this.__soundID );

    }

}

```

but to no avail.  
I want it to stop the old playing sound & start the new 1.  
for some reason, it’s letting the other sounds play on top of the old sound.

---

<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 18, 2020, 8:37am UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/7 "2020-02-18T08:37:15Z")

</div>

why is it doing this? the function is correct…

---

<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 18, 2020, 9:53pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/8 "2020-02-18T21:53:26Z")

</div>

ok, so I see what my sound manager is doing. It’s not stopping the 1st playing sound after calling `__m.PlayAudio ( )` twice.  
It should stop the 1st track & play the chosen track.

---

<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 18, 2020, 11:48pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/9 "2020-02-18T23:48:39Z")

</div>

i did this. it seems to be picking up on the sounds in the game but not the title screen… so the title screen is playing simultaneously with the game sounds. but the game sounds are not playing simultaneously with other game sounds.

all i do is :

```javascript
this.IsSoundPlaying = function ( __sound )

{

	this. __sound =__ sound;

	return this.__sound.isPlaying;

}

```

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

{

    this. __objData =__ objData;

    this. __scene = this.__ objData.scene;
    this. __snd = this.__ objData.sound;
    this. __soundID = this.__ objData.soundID;

    console.log ( this.IsSoundPlaying ( this.__snd ) );

    if ( this.IsSoundPlaying ( this.__snd ) === true ) {

        this.StopAudio ( this.__snd );

        this.__snd.play ({
            seek : 0, 
        });

    }

    if ( this.IsSoundPlaying ( this.__snd ) === false ) {

        this.__snd.play ({
            seek : 0, 
        });

    }

}

```

then when i call :

```javascript
__m.PlayAudio ( {
    scene : this.__scene, 
    sound : __BGMAudio, 
    soundID : __BGMAudio.soundID, 
}, 1 );

```

more than once in different scenes, it doesn’t stop the currently playing track like it’s supposed to. It only plays the 1st in-game sound then when the player jumps, it cancels the level’s theme & switches to jumping.  
it shouldn’t switch to jumping. it should allow jumping to play along side the level’s theme, but themes should not be able to play on top of other themes.

---

<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 20, 2020, 4:03pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/11 "2020-02-20T16:03:12Z")

</div>

can someone please help me?

tired of being ignored… ☹

---

<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 20, 2020, 5:42pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/12 "2020-02-20T17:42:42Z")

</div>

I don’t know. I’m not sure how it’s supposed to work.

`IsSoundPlaying()` looks wrong to me. I don’t understand why it’s assigning to `this.__sound`.

If you just want to play one sound at a time in some context, I think the core logic is

```javascript
var current, next;

if (current.isPlaying) {
  current.stop();
  this.sound.remove(current);
}

current = next;

current.play();

```

If you want to play several sounds at one time in some context, then there’s no need to store the current one playing.

---

<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 20, 2020, 5:44pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/13 "2020-02-20T17:44:30Z")

</div>

@samme : When detecting another sound attempting to play, it should stop the old sound & play the new one.

---

<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 20, 2020, 5:47pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/14 "2020-02-20T17:47:52Z")

</div>

@samme : also, what does `'current'` mean & what does `'next'` mean? I need to know what these parameters link to.

---

<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 20, 2020, 5:50pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/15 "2020-02-20T17:50:40Z")

</div>

A sound, from `this.add.sound('KEY')`.

---

<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 20, 2020, 5:51pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/16 "2020-02-20T17:51:18Z")

</div>

@samme : ok, so 1 is the currently playing key & the other is the new key? got 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 20, 2020, 5:58pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/17 "2020-02-20T17:58:41Z")

</div>

It has to be an actual [Sound](https://photonstorm.github.io/phaser3-docs/Phaser.Sound.BaseSound.html), not the key itself.

---

<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 20, 2020, 7:11pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/18 "2020-02-20T19:11:11Z")

</div>

@samme : this is sorta’ working. it’s still not switching between tracks without stopping the old currently playing sound{s} ☹

* * *

The function{s} :

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

	this. __objData =__ objData;

	return Object.keys ( this.__objData ).length;

}

this.LoadAudio = function ( __objData )  

{

	this. __objData =__ objData;

	this. __snd = this.__ objData.sound;
	this. __soundID = this.__ objData.soundID;
	this. __soundData = this.__ objData.soundData;

	this. __sound = this.__ snd.add ( this. __soundID, this.__ soundData );

	return this.__sound;

}

this.PlayAudio = function ( __objData )

{

	this. __objData =__ objData;

	this. __scene = this.__ objData.scene;
	this. __snd = this.__ objData.sound;
	this. __soundID = this.__ objData.soundID;

	console.log ( this.IsSoundPlaying ( this.__snd ) );

	if ( this.IsSoundPlaying ( this.__snd ) === true ) {
		this.StopAudio ( this.__snd );
		this.__snd.play ({
			seek : 0, 
		});
	}

	if ( this.IsSoundPlaying ( this.__snd ) === false ) {
		this.__snd.play ({
			seek : 0, 
		});
	}

}

this.SwitchAudio = function ( sound, current, next )

{

	this.__sound = sound;
	this.__current = current;
	this.__next = next;

	if ( this.__current.isPlaying ) {
		this.__current.stop ( );
		this.__sound.remove ( this.__current );
	}

	this. __current = this.__ next;

	this.__current.play ( );

}

```

* * *

The way it is called :

```javascript
this.__BGMTrack = [];
this.__BGMData = [];

this.__SFXTrack = [];
this.__SFXData = [];

this.__BGMAudio = [];
this.__SFXAudio = [];

// Load Background Music File{s}

this.__BGM_Data = {
	ids : [
		'bgMusic', 'lvl1Music', 'lvl1MusicA', 
		'lvl2Music', 
	], 
	files : [
		'assets/TownTheme.mp3', 
		'assets/TownTheme2.mp3', 
		'assets/bgm/lvl-1/SMILE.mp3', 
		'assets/bgm/lvl-1/smb-overworld-remix.mp3', 
	], 
};

this.__SFX_Data = {
	ids : [
		'jumpA', 
	], 
	files : [
		'assets/bgm/lvl-1/SMILE.mp3', 
	], 
};

this.__BGM_Data_Length = ( this.countJSObjectData ( this.__BGM_Data.files ) );
this.__SFX_Data_Length = ( this.countJSObjectData ( this.__SFX_Data.files ) );

for ( this. __i = 0; this.__ i < ( this.__BGM_Data_Length ); this.__i++ ) {

	console.log ( 'Data IDs :: ' + this.__BGM_Data.ids [this.__i] );
	console.log ( 'Data Files :: ' + this.__BGM_Data.files [this.__i] );

	this.load.audio ( this.__BGM_Data.ids [this.__i], [
		this.__BGM_Data.files [this.__i], 
	] );

}

for ( this. __j = 0; this.__ j < ( this.__SFX_Data_Length ); this.__j++ ) {

	console.log ( 'Data IDs :: ' + this.__SFX_Data.ids [this.__j] );
	console.log ( 'Data Files :: ' + this.__SFX_Data.files [this.__j] );

	this.load.audio ( this.__SFX_Data.ids [this.__j], [
		this.__SFX_Data.files [this.__j], 
	] );

}

// Create & Load our BGM{s}

this.__BGMTrack = [

	'bgMusic', 'lvl1Music', 'lvl1MusicA', 
	'lvl2Music', 

];

this.__BGMData = [

	{ 
		volume : 0.1, loop : true, 
	}, 

	{
		volume : 0.1, loop : true, 
	}, 

	{
		volume : 0.03, loop : true, 
	}, 

	{
		volume : 0.1, loop : true, 
	}, 

];

// Create & Load our sound{s}

this.__SFXTrack = [

	'jumpA', 

];

this.__SFXData = [

	{ 
		volume : 0.1, loop : true, 
	}, 

];

for ( this. __i = 0; this.__ i < 3; this.__i++ ) {

	__BGMAudio [this.__i] = __m.LoadAudio ( {
		sound : this.sound, 
		soundID : __BGMTrack [this.__i], 
		soundData : __BGMData [this.__i], 
	}, 1 );

}

for ( this. __ii = 0; this.__ ii < 1; this.__ii++ ) {

	__SFXAudio [this.__ii] = __m.LoadAudio ({
		sys : this.sys, 
		sound : this.sound, 
		soundID : __SFXTrack [this.__ii], 
		soundData : __SFXData [this.__ii], 
	}, 1 );

}

// Start playing 1st BGM audio track in BGM Array : 

__m.PlayAudio ( {
	scene : this.__scene, 
	sound : __BGMAudio [0], 
	soundID : __BGMAudio.soundID, 
}, 1 );

// Switch this from 1st BGM audio file in Array 
// to 1st SFX audio file in Array on purpose : 

__m.SwitchAudio ( this.sound,__BGMAudio [0], __SFXAudio [0] );

```

---

<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 20, 2020, 9:13pm UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/19 "2020-02-20T21:13:20Z")

</div>

@samme : this sorta’ works but it’s still not stopping the other track & starting the new 1. It’s just starting the new 1.

---

<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, 12:03am UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/20 "2020-02-21T00:03:56Z")

</div>

@samme : what am I doing wrong here?

I’m getting :

```javascript
Error: There is no audio asset with key "bgMusic" in the audio cache phaser.js:83761:19

```

But I have all the right files in the right folders media-wise & i’m calling their correct locations in code as well.

```javascript
this.__BGMTrack = [];
this.__BGMData = [];

this.__SFXTrack = [];
this.__SFXData = [];

this.__BGMAudio = [];
this.__SFXAudio = [];

this.countJSObjectData = function ( __objData ) {

	this. __objData =__ objData;

	return Object.keys ( this.__objData ).length;

}

this.__preload = function ( ) {

	// Load Background Music File{s}

	this.__BGM_Data = {
		ids : [
			'bgMusic', 'lvl1Music', 'lvl1MusicA', 
			'lvl2Music', 
		], 
		files : [
			'assets/TownTheme.mp3', 
			'assets/TownTheme2.mp3', 
			'assets/bgm/lvl-1/SMILE.mp3', 
			'assets/bgm/lvl-1/smb-overworld-remix.mp3', 
		], 
	};

	this.__SFX_Data = {
		ids : [
			'jumpA', 
		], 
		files : [
			'assets/bgm/lvl-1/SMILE.mp3', 
		], 
	};

	// Create & Load our BGM{s}

	this.__BGMTrack = [

		'bgMusic', 'lvl1Music', 'lvl1MusicA', 
		'lvl2Music', 

	];

	this.__BGMData = [

		{ 
			volume : 0.1, loop : true, 
		}, 

		{
			volume : 0.1, loop : true, 
		}, 

		{
			volume : 0.03, loop : true, 
		}, 

		{
			volume : 0.1, loop : true, 
		}, 

	];

	// Create & Load our sound{s}

	this.__SFXTrack = [

		'jumpA', 

	];

	this.__SFXData = [

		{ 
			volume : 0.1, loop : true, 
		}, 

	];

	this.__BGM_Data_Length = ( countJSObjectData ( this.__BGM_Data.files ) );
	this.__SFX_Data_Length = ( countJSObjectData ( this.__SFX_Data.files ) );

	for ( this. __i = 0; this.__ i < ( this.__BGM_Data_Length ); this.__i++ ) {

		console.log ( 'Data IDs :: ' + this.__BGM_Data.ids [this.__i] );

	}

	for ( this. __i = 0; this.__ i < ( this.__SFX_Data_Length ); this.__i++ ) {

		console.log ( 'Data IDs :: ' + this.__SFX_Data.ids [this.__i] );

	}

	for ( this. __i = 0; this.__ i < ( this.__BGM_Data_Length ); this.__i++ ) {

		console.log ( 'Data Files :: ' + this.__BGM_Data.files [this.__i] );

	}

	for ( this. __i = 0; this.__ i < ( this.__SFX_Data_Length ); this.__i++ ) {

		console.log ( 'Data Files :: ' + this.__SFX_Data.files [this.__i] );

	}

	console.error ( this.__BGM_Data_Length );
	console.error ( this.__SFX_Data_Length );

	this.LoadAudio = function ( __objData )

	{

		this. __objData =__ objData;
		this. __masterKey = this.__ objData.masterKey;

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

		this. __snd = this.__ objData.sound;
		this. __soundID = this.__ objData.soundID;
		this. __soundData = this.__ objData.soundData;

		console.error (

			'this. __soundID' + ' :: ', this.__ soundID + '\r\n' + 
			'this.__soundData' + ' :: ', JSON.stringify ( this.__soundData )

		);

		this. __sound = this.__ snd.add ( this. __soundID, this.__ soundData );

		console.error (

			'this. __snd' + ' :: ', this.__ snd

		);

		return this.__sound;

	}

	this.IsSoundPlaying = function ( __sound )

	{

		this. __sound =__ sound;

		return this.__sound.isPlaying;

	}

	this.StopAudio = function ( __sound )

	{

		this. __sound =__ sound;

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

		this.__sound.stop ( );

	}

	this.PlayAudio = function ( __objData )

	{

		this. __objData =__ objData;

		this. __scene = this.__ objData.scene;
		this. __snd = this.__ objData.sound;
		this. __soundID = this.__ objData.soundID;

		console.log ( this.IsSoundPlaying ( this.__snd ) );

		if ( this.IsSoundPlaying ( this.__snd ) === true ) {
			this.StopAudio ( this.__snd );
			this.__snd.play ({
				seek : 0, 
			});
		}

		if ( this.IsSoundPlaying ( this.__snd ) === false ) {
			this.__snd.play ({
				seek : 0, 
			});
		}

	}

	this.SwitchAudio = function ( sound, current, next )

	{

		this.__sound = sound;
		this.__current = current;
		this.__next = next;

		if ( this.__current.isPlaying ) {
			this.__current.stop ( );
			this.__sound.remove ( this.__current );
		}

		this. __current = this.__ next;

		this.__current.play ( );

	}

	for ( this. __i = 0; this.__ i <= ( this.__BGM_Data_Length ); this.__i++ ) {

		__BGMAudio [this.__i] = this.LoadAudio ({
			sys : this.sys, 
			sound : this.sound, 
			soundID : this.__BGMTrack [this.__i], 
			soundData : this.__BGMData [this.__i], 
		});

		console.error ( ' __BGMAudio' + ' :: ',__ BGMAudio );

	}

	for ( this. __i = 0; this.__ i <= ( this.__SFX_Data_Length ); this.__i++ ) {

		__SFXAudio [this.__i] = this.LoadAudio ({
			sys : this.sys, 
			sound : this.sound, 
			soundID : this.__SFXTrack [this.__i], 
			soundData : this.__SFXData [this.__i], 
		});

		console.error ( ' __SFXAudio' + ' :: ',__ SFXAudio );

	}

}

this.__create = function ( ) {

	// Start playing 1st BGM audio track in BGM Array : 

	/*

	this.PlayAudio ( {
		scene : this.__scene, 
		sound : __BGMAudio [0], 
		soundID : __BGMAudio [0].soundID, 
	}, 1 );

	// Switch this from 1st BGM audio file in Array 
	// to 1st SFX audio file in Array on purpose : 

	// this.SwitchAudio ( this.sound, __BGMAudio [0],__SFXAudio [0] );
	
	*/

}

this.__update = function ( ) {

}

this.__config = {

	type : Phaser.AUTO, width : 800, height : 600, 

	physics : {

		default : 'arcade', 

		arcade : {

			gravity : { 

				y : 0, 

			}

		}

	}, 

	scene : {

		preload : this.__preload, 

			create : this.__create, 

		update : this.__update, 

	}

}

this.__game = new Phaser.Game ( this.__config );

```

---

<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, 12:54am UTC](https://phaser.discourse.group/t/problem-switching-audio/5229/21 "2020-02-21T00:54:09Z")

</div>

Look in the **Network** tab to see which URLs were requested.

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