# Repeat sound issue?

**URL:** <https://phaser.discourse.group/t/repeat-sound-issue/5347>\
**Category:** Phaser 3\
**Created:** [February 27, 2020, 3:36pm UTC](https://phaser.discourse.group/t/repeat-sound-issue/5347 "2020-02-27T15:36:03Z")\
**Posts on this page:** 3\
**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 27, 2020, 3:36pm UTC](https://phaser.discourse.group/t/repeat-sound-issue/5347/1 "2020-02-27T15:36:04Z")

</div>

so I’m trying to get my jump sound to play once per jump. Problem is, 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 ( );

    }

}

```

how it is called :

```javascript
// Transition to jump if jump key{s} are pressed

if ( __isPlayerJumping === false && ( this.__keys.w.isDown || this.__keys.up.isDown ) )

{

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

	__JumpUp ({
		stateMachine : this.__stateMachine, 
		keys : this.__keys, 
		scene : this.__scene, 
		entity : this.__entity, 
		minJumpHeightY : this.__minJumpHeightY, 
		maxJumpHeightY : this.__maxJumpHeightY, 
		minVelocityY : this.__minVelocityY, 
		maxVelocityY : this.__maxVelocityY, 
		minAccelerationY : this.__minAcclerationY, 
		maxAccelerationY : this.__maxAcclerationY, 
	});

}

if ( __m.isOnFloor ( this.__entity.body ) === true )

{

	__isPlayerJumping = false;

	__ActivateJump ({
		stateMachine : this.stateMachine, 
		scene : this.__scene, 
		entity : this.__entity, 
		sound : this.__sound, 
		minJumpHeightY : this.__minJumpHeightY, 
		maxJumpHeightY : this.__maxJumpHeightY, 
		minVelocityY : this.__minVelocityY, 
		maxVelocityY : this.__maxVelocityY, 
		minAccelerationY : this.__minAccelerationY, 
		maxAccelerationY : this.__maxAccelerationY, 
	});

}

if ( __m.isOnFloor ( this.__entity.body ) === false )

{

	__isPlayerJumping = true;

	__ActivateFall ( {
		stateMachine : this.stateMachine, 
		scene : this.__scene, 
		entity : this.__entity, 
		minVelocityY : this.__minVelocityY, 
		maxVelocityY : this.__maxVelocityY, 
		minAccelerationY : this.__minAccelerationY, 
		maxAccelerationY : this.__maxAccelerationY, 
	});

}

```

Here’s a small video of the issue :

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

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 28, 2020, 2:12pm UTC](https://phaser.discourse.group/t/repeat-sound-issue/5347/2 "2020-02-28T14:12:12Z")

</div>

someone can help? ☹

---

<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 29, 2020, 5:42pm UTC](https://phaser.discourse.group/t/repeat-sound-issue/5347/3 "2020-02-29T17:42:46Z")

</div>

> [@Thundros](#):
>
> Problem is, 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.

You need to handle music and sound effects separately. Only 2 variables are necessary.

## Music

```javascript
var currentMusic = this.sound.add('music1');

currentMusic.play();

// …
// Switch:

if (currentMusic) {
  currentMusic.stop();
}

currentMusic = this.sound.add('music2');

currentMusic.play();

```

## Sound Effects

```javascript
var jumpSound = this.sound.add('jump');

// …

// Jump!
if (!jumpSound.isPlaying) {
  jumpSound.play();
}

```
