How to fade out individual sound instead of all?

proyb2
proyb2 used Ask the Experts™
on
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]
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
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

Author

Commented:
I tried your new code, however, the problem is still the same as before.
Commented:
Ok, I think I might have it.

The play() function generates a new SoundChannel every time it is invoked. So, you should keep an array of SoundChannel objects, which you push to and pop from when you spawn or stop a new instance of your sound file.

Here's what I've got:
    private var soundChannelArray:Array = [];
    private var trans:SoundTransform;
    [Embed(source = "assets/sounds/pada.mp3")]
    private var embeddedSound:Class;
    private var s1_1:SoundAsset;
    //button press would execute p1_1
    public function p1_1():void {
        s1_1 = new embeddedSound() as SoundAsset;
        soundChannelArray.push(s1_1.play());
    }
    //Button not press would execute fade1_1
    private function fade1_1():void {
        TweenMax.to(soundChannelArray[soundChannelArray.length-1], .35, {volume:0, onComplete:stop1_1});
    }

    private function stop1_1():void {
        soundChannelArray.pop().stop();
    }

Open in new window

Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Commented:
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.

Author

Commented:
Oh, still the same error thou. I wonder was the Flash player's limitation?
Commented:
From the language ref, http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/media/Sound.html#play()

"The maximum number of sound channels available at once is 32."

That could be it.


Author

Commented:
No solution, I will choose alternative solution for sound development

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial