# Fade out Sound outside of its duration

**URL:** <https://phaser.discourse.group/t/fade-out-sound-outside-of-its-duration/8228>\
**Category:** Phaser 3\
**Created:** [November 26, 2020, 11:32am UTC](https://phaser.discourse.group/t/fade-out-sound-outside-of-its-duration/8228 "2020-11-26T11:32:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![drohrlick](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/drohrlick/32/4401_2.png) [@drohrlick](https://phaser.discourse.group/u/drohrlick)\
**Post date:** [November 26, 2020, 11:32am UTC](https://phaser.discourse.group/t/fade-out-sound-outside-of-its-duration/8228/1 "2020-11-26T11:32:10Z")

</div>

Hello!

I just started using Phaser 3.

I have a sound file. Let’s say it’s 5 seconds in duration.

I immediately play the sound with:

`sound.play(id, { volume: 1});`

After let’s say after 3 seconds, I now want to fade out the sound:

```
const currentSound = sound.get(id);
this.tweens.add({
   targets: currentSound,
   volume: 0,
   duration: 1000,
});

```

This is great since the sound plays for 3 seconds and then fades away by 4 seconds.

However, when I increase the duration of the fade out to something beyond the sound file’s duration (say a duration of 8000), I get an error in the updateTweenData:

`TypeError: Cannot set property 'volume' of null`

I believe it’s because the sound has finished but the tween is still attempting to adjust its volume.

Is there a good way to guard against tween durations outside of the sound’s length?  
Is there a way I can get the current “elapsed” time of the sound and adjust my tween’s duration to something within the remaining time of my sound?

Thanks!

---

<div class="post-metadata">

**Author:** ![jjcapellan](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jjcapellan/32/542_2.png) [@jjcapellan](https://phaser.discourse.group/u/jjcapellan)\
**Post date:** [November 26, 2020, 5:21pm UTC](https://phaser.discourse.group/t/fade-out-sound-outside-of-its-duration/8228/2 "2020-11-26T17:21:45Z")

</div>

Hi @drohrlick,  
If you play the sound from the sound manager, it is destroyed on stop.  
You have two options:  
1 ) Listen to the sound [stop event](https://photonstorm.github.io/phaser3-docs/Phaser.Sound.Events.html#event:STOP) and stop the tween from the listener.

```javascript
const currentSound = sound.get(id);
let tween = this.tweens.add({
   targets: currentSound,
   volume: 0,
   duration: 1000,
});
currentSound.on('stop', () => {
    tween.stop();
});

```

2 ) Add the sound to the scene and play it:

```javascript
const mysound = this.sound.add('mysoundkey');
mysound.play(); // sound is not destroyed on stop

```

Good luck!

---

<div class="post-metadata">

**Author:** ![drohrlick](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/drohrlick/32/4401_2.png) [@drohrlick](https://phaser.discourse.group/u/drohrlick)\
**Post date:** [November 26, 2020, 8:30pm UTC](https://phaser.discourse.group/t/fade-out-sound-outside-of-its-duration/8228/3 "2020-11-26T20:30:56Z")

</div>

Great advice!

From the way I was using my sound, I had to make use of the ‘complete’ event instead of the ‘stop’ event. But from there, I was able to make sure the tween stops.

Thanks!
