Fading Out a Sound Object After 2 Loops

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


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