0

I have this problem when I try to fade in sound with the function below. It just doesn't work and I can't figure out where the problem is. I would like to fade in a looping sound but nothing happens. I appreciate any hint I can get to solve this problem. Thanks in advance.

this is used to play a sound via events

dispatchEvent(new CustomEventSound(CustomEventSound.PLAY_SOUND, Main.SOUND_AMBIENT, false, true, false, 999999, 0, 0, setSoundVolume));

this is the function to play a sound. the stop-function is almost identical to this one.

public function playSound(soundName:String, isSoundTrack:Boolean = false, fadeIn:Boolean = false, fadeOut:Boolean = false,
                              loops:int = 1, offset:Number = 0, volume:Number = 1):void {

        if (fadeIn) {
            tempSoundTransform.volume = 0;
        } else {
            tempSoundTransform.volume = volume;

        }
        tempSound = sounds[soundName];

        if (isSoundTrack) {
            if (soundTrackChannel != null) {
                soundTrackChannel.stop();
            }

            soundTrackChannel = tempSound.play(offset, loops);                              
            soundTrackChannel.soundTransform = tempSoundTransform;

            if (fadeIn) {
                for (var i:int = 0; i < fadeInInc; i++) {
                    tempSoundTransform.volume += 1 / fadeInInc;
                    soundTrackChannel.soundTransform = tempSoundTransform;  
                    if (tempSoundTransform.volume >= 1) {
                        tempSoundTransform.volume = 1;

                    }
                }
            }

        } else {
            soundChannels[soundName] = tempSound.play(offset, loops);
            soundChannels[soundName].soundTransform = tempSoundTransform;

            if (fadeIn) {
                for (var i:int = 0; i < fadeInInc; i++) {

                    tempSoundTransform.volume += 1/fadeInInc;
                    soundChannels[soundName].soundTransform = tempSoundTransform;   
                    if (tempSoundTransform.volume >= 1) {
                        tempSoundTransform.volume = 1;

                    }

                    trace("tempSoundTransform.volume " + tempSoundTransform.volume);
                }
            }

        }

    }
drpelz
  • 811
  • 11
  • 43
  • Not sure how does it work. I guess you call function based on first parameter in event (in this case CustomEventSound.PLAY_SOUND) and pass rest of params. But except the first one you have 8 params, and playSound function requires only 7, which one you are using? – 3vilguy Jan 09 '12 at 23:03
  • Yes. There was an error in the parameters and I corrected it. Now they match but it still doesn't fade. – drpelz Jan 10 '12 at 01:45

1 Answers1

3

I think your problem is caused by "for" statements. Program will wait while for statement will end and set values after that. If you want to increase volume value parallely you should do it on ENTER_FRAME event or try Sounds fade in/out with ActionScript 3

Community
  • 1
  • 1
3vilguy
  • 981
  • 1
  • 7
  • 20
  • Hm...my initial thought was it starts playing the sound and I have to fade my sound with a for-statement and apply that in every cycle to my volume. I traced the volume and it "fades", i.e. increases the volume-value in every frame. But you can't hear anything. My sound that's looping is very short, i.e. less than a second. Is that causing the problem? – drpelz Jan 10 '12 at 01:42
  • 2
    That's not the point. Do some tests. Add TextField to stage and do a loop statement `for(var i:int = 0; i < 10000000; i++) { trace(i); txtFld.text = i; }` and tell me if you can see in textfield that numbers are increasing... – 3vilguy Jan 10 '12 at 09:54
  • Problem solved.:) Solution: [link]http://www.zedia.net/2008/fading-out-volume-using-tweenlite/comment-page-1/#comment-5220 – drpelz Jan 11 '12 at 02:53