A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: fading sound doesn't work

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    85

    fading sound doesn't work

    Hi,

    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
    Code:
    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.
    Code:
    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);
    					}
    				}
    				
    			}
    					
    		}

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Does your CustomEventSound have a constructor like the one you are calling? There's a lot of parameters there that a typical event doesn't have.

    playSound is clearly not the actual event listener, since it doesn't take an Event parameter. What is your listener, and how does it call playSound? Does your event need to bubble, and does it?

    Finally, please describe in as much detail as you can what the problem actually is. "It doesn't work" is not helpful. If your event listener is not getting called, tell us that. If you are getting errors, tell us that, and post them.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    85
    So this is the CustomEventSound.as-class. The playSound-function works (except for the fade-in-thing), i.e. it receives the event-call; otherwise it wouldn't play my sound, right? Basically that's driving me nuts.

    I discovered that if I change manually the sound volume in my event-call to 0 it still plays the sound (so I can hear it; very strange). I think something overwrites the volume's data but I'm not sure yet. I'll check that out.

    Code:
    public function CustomEventSound(type:String, name:String, isSoundTrack:Boolean = false, fadeIn:Boolean = false,
    			fadeOut:Boolean = false, loops:int = 0, offset:Number = 0, volume:Number = 1, bubbles:Boolean = false, cancelable:Boolean = false)
    		{
    			
    			super(type, bubbles, cancelable);
    			this.name = name;
    			this.loops = loops;
    			this.offset = offset;
    			this.volume = volume;
    			this.isSoundTrack = isSoundTrack;
    			this.fadeIn = fadeIn;
    			this.fadeOut = fadeOut;
    			
    		}
    		
    		public override function clone():Event {
    			
    			return new CustomEventSound(type, name, isSoundTrack, fadeIn, fadeOut, loops, offset, volume, bubbles, cancelable)
    		}
    		
    		public override function toString():String {
    			return formatToString(type, "type", "bubbles", "cancelable", "eventPhase", name, isSoundTrack, fadeIn, fadeOut, loops, offset,
    								  volume);
    		}

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Please show the line where you call addEventListener for whatever function recieves these events, and also that function (it is not playSound).

    Your CustomEventSound constructor does not match up with the new CustomEventSound call you posted. Specifically, you are passing setSoundVolume for the bubbles parameter. Unless setSoundVolume is a boolean, that should cause a compilation error. You probably also don't want to pass 0 for volume, which you are doing.

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    85
    Yes, the parapmeters didn't match. I corrected that problem but it still doesn't work.

    This is the line which receives the event-calls:

    Code:
    game.addEventListener(CustomEventSound.PLAY_SOUND, soundEventListener, false, 0, true);
    This is the function that gets executed when receiving the event-call (this is part of a framework therefore I'm using the override-keyword:

    Code:
    override public function soundEventListener(e:CustomEventSound):void {
    			
    			if (e.type == CustomEventSound.PLAY_SOUND) {
    				soundManager.playSound(e.name, e.isSoundTrack, e.fadeIn, e.fadeOut, e.loops, e.offset, e.volume);
    			} else {
    				soundManager.stopSound(e.name, e.isSoundTrack, e.fadeIn, e.fadeOut);
    			}
    			
    		}

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Is game the instance which has the dispatchEvent call? If not, then in order for the listener to hear the event, two things have to be true: 1) the event must bubble. 2) the dispatching instance must be a display child of the listening instance.

  7. #7
    Member
    Join Date
    Jun 2011
    Posts
    85
    I'm not sure about this part. I will check this out asap...

  8. #8
    Member
    Join Date
    Jun 2011
    Posts
    85
    Problem solved! After hours of editing my code I managed to find a solution on the web finally:

    http://www.zedia.net/2008/fading-out.../#comment-5220

    Vusi Sindane's version runs like a charm. Thanks anyway for your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center