Link to home
Start Free TrialLog in
Avatar of proyb2
proyb2

asked on

How to fade out individual sound instead of all?

Tie this code to a virtual piano keyboard which is a mx:button.

I tried to press the button repetitive as fast as I can, it will throw an error exception and cause the AIR application to hang.

I found that it would not throw an error if I stop the sound channel when it completely fade out. However, I wish the function could fade out and stop the sound individually, not stop o1_1 together. How do I do that?

private var s1_1:Sound = new Sound();
private var o1_1:SoundChannel = new SoundChannel();

private static const a1_1:String = "1.mp3";

//button press would execute p1_1
private function p1_1():void{
s1_1 = new Sound();
s1_1.load(new URLRequest(a1_1));
o1_1=s1_1.play();
}

//Button not press would execute fade1_1
private function fade1_1():void{
TweenMax.to(o1_1, .35, {volume:0, onComplete:stop1_1});
}

private function stop1_1():void {
//if I add, this code would only stop the sound altogether rather than individual. I wish to have each of the instance to fade our and stop itself should the user press and release.
    o1_1.stop();
}

Open in new window


My error exception in Flex builder 3 using SDK 3.5 and AIR 2.0:
[SWF] snd.swf - 1,022,361 bytes after decompression
TypeError: Error #1009: Cannot access a property or method of a null object reference.
     at snd/st1_1()[...snd.mxml:44]
     at snd/___snd_Button1_mouseUp()[...snd.mxml:67]
Avatar of petiex
petiex
Flag of United States of America image

Since you are using a URLRequest to load the sound file, it might be that the null object reference results from trying to access the file via the play() function before it has been loaded.

You could try listening for the loadComplete event before attempting to play, but I think it might be better to embed the sound in the application using mx.core.SoundAsset, which is an extension of flash.media.Sound that lets you do embedding.

You would want to place the sound file in a directory on the source path, and then the code might look something like this:
[Embed(source = "assets/sounds/pada.mp3")]
    private var embeddedSound:Class;
//private var s1_1:Sound = new Sound();
    private var o1_1:SoundChannel;
    private var s1_1:SoundAsset;
    public function SoundTest() {
        o1_1 =   new SoundChannel();
        super();
    }


private static const a1_1:String = "1.mp3";

//button press would execute p1_1
public function p1_1():void{
s1_1 = new embeddedSound() as SoundAsset;
//s1_1.load(new URLRequest(a1_1));
o1_1=s1_1.play();
}

Open in new window

Avatar of proyb2
proyb2

ASKER

I tried your new code, however, the problem is still the same as before.
SOLUTION
Avatar of petiex
petiex
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can leave out that SoundTransform variable. It's a really fun thing to play with, though, especially setting the pan value to very large positive or negative numbers.
Avatar of proyb2

ASKER

Oh, still the same error thou. I wonder was the Flash player's limitation?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of proyb2

ASKER

No solution, I will choose alternative solution for sound development