# Bgsound loop + fade in /out

**URL:** <https://forum.kirupa.com/t/bgsound-loop-fade-in-out/254280>\
**Category:** Uncategorized\
**Created:** [March 10, 2008, 4:05pm UTC](https://forum.kirupa.com/t/bgsound-loop-fade-in-out/254280 "2008-03-10T16:05:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaimsitecom](https://avatars.discourse-cdn.com/v4/letter/j/6f9a4e/32.png) [@jaimsitecom](https://forum.kirupa.com/u/jaimsitecom)\
**Post date:** [March 10, 2008, 4:05pm UTC](https://forum.kirupa.com/t/bgsound-loop-fade-in-out/254280/1 "2008-03-10T16:05:07Z")

</div>

am a noob flash designer. somebody gave me an AS2 script that makes the sound fade out when a button is clicked and fades back in to continue at where the last position was stopped.

the problem is, when i click the fade out button, the bgsound won’t loop anymore. but if i won’t touch(click) any of the buttons, the bgsound will loop.

can anybody help me out here please? thanks in advance. below is the said script:

var bgSound:Sound = new Sound();  
bgSound.attachSound(“myTrack”);  
bgSound.start(0,99999);  
var soundStarted:Boolean = true;  
var lastPosition:Number = 0;  
var id:Number;  
fadeIn\_btn.onRelease = fadeInSound;  
function fadeInSound():Void {  
if (id) {  
clearInterval(id);  
}  
bgSound.start(lastPosition);  
var volume = bgSound.getVolume();  
id = setInterval(fadeIn, 5);  
function fadeIn():Void {  
bgSound.setVolume(volume++);  
if (volume\>=100) {  
clearInterval(id);  
}  
}  
}  
fadeOut\_btn.onRelease = fadeOutSound;  
function fadeOutSound():Void {  
if (id) {  
clearInterval(id);  
}  
var volume = bgSound.getVolume();  
id = setInterval(fadeOut, 5);  
function fadeOut():Void {  
bgSound.setVolume(volume–);  
if (volume\<=0) {  
clearInterval(id);  
bgSound.stop();  
lastPosition = Math.round(bgSound.position/1000);  
}  
}  
}
