# Fading Out a Sound Object After 2 Loops

**URL:** <https://forum.kirupa.com/t/fading-out-a-sound-object-after-2-loops/209622>\
**Category:** flash\
**Created:** [December 10, 2006, 11:48pm UTC](https://forum.kirupa.com/t/fading-out-a-sound-object-after-2-loops/209622 "2006-12-10T23:48:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pherank](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@Pherank](https://forum.kirupa.com/u/Pherank)\
**Post date:** [December 10, 2006, 11:48pm UTC](https://forum.kirupa.com/t/fading-out-a-sound-object-after-2-loops/209622/1 "2006-12-10T23:48:04Z")

</div>

I’m trying to figure out how I can fade out a sound object after it has looped twice. Right now the sound fades 5 seconds before the end of the first loop (since the fade code is based on the duration of the sound clip). Seems like there needs to be a way to count when the duration has been reached two times and then the fade code is executed. Note: the linkage identifier of the sound clip is “mainPageLoop”.

```auto

var mainLoop:Sound = new Sound();
mainLoop.attachSound("mainPageLoop");
//mainLoop.start(); // Plays once
mainLoop.start(0, 2); // Starts at beginning and loops 2 times.
mainLoopVolume = 100;
mainLoop.setVolume(mainLoopVolume);
mainLoop.onSoundComplete = function() {
    tMessage.text = "Sound has finished playing.";
};
this.createTextField("tMessage", 1, 10, 10, 200, 20);
tMessage.border = true;
this.onEnterFrame = function() {
    songDuration = _root.mainLoop.duration/1000;
    songPosition = _root.mainLoop.position/1000;
    //trace(songPosition);
    fadeEnd = (songDuration-5);
    if (fadeEnd<=songPosition && mainLoopVolume>0) {
        if (mainLoopVolume<=1) {
            mainLoopVolume = 0;
        }
        _root.mainLoop.setVolume(mainLoopVolume);
        //mainLoopVolume = mainLoopVolume-1;
        mainLoopVolume--;
    }
};

```
