Problem switching audio?

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/ | Here is the direct link to '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 | Here is the direct link to 'sfx.js' : https://thundros.github.io/sound-test/res/game/js/sfx.js

Any help as always is most DEFINITELY appreciated!

Thank you!

Phaser doesn’t define this.sys.game.globals, so do you?

@samme : how would i do so?

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

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

There’s already this.sys.game.registry though, you can use that.

@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?

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 :

// 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 );

*/

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.

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.

why is it doing this? the function is correct…

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.

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 :

this.IsSoundPlaying = function ( __sound )

{

	this.__sound = __sound;

	return this.__sound.isPlaying;

}
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 :

__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.

can someone please help me?

tired of being ignored… :frowning:

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

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.

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

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

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

@samme : ok, so 1 is the currently playing key & the other is the new key? got it.

It has to be an actual Sound, not the key itself.

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


The function{s} :

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 :

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 ] );

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

@samme : what am I doing wrong here?

I’m getting :

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.

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 );

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