user1166091
user1166091

Reputation: 3

Actionscript: Stopping sound in Adobe Flash CS4

I'd like to stop a specific sound in CS4 without stopping all of the sounds at the same time. It would be great to have something simple that I can copy and paste, as I've looked around a lot for an answer to this and gotten a lot of mumbo-jumbo that didn't end up working. Thanks!

Upvotes: 0

Views: 5117

Answers (2)

noponies
noponies

Reputation: 2726

What version of actionscript are you using, AS2 or AS3?

How are you playing your sound? Is it sitting in a keyframe on the timeline? Are you using actionscript to import it and play it from the library?

(Edit with timeline code)

OK, if its on the timeline, you will need to use actionscript to play it, so that you can then later individually target it to stop it.

First, go to your library, right click on your sound and edit its properties. You want to export it for actionscript, and give it a unique actionscript identifier, something like 'MyGreatSound'. No spaces etc, just one word.

Take your sound out of your keyframe.

Next, create a keyframe in your actions layer that corresponds to the start of your sound and paste this in; (this assumes you gave your sound in the library the identifier of 'MyGreatSound')

var myChannel:SoundChannel = new SoundChannel();
var mySound:Sound = new MyGreatSound(); 
myChannel = mySound.play(0);

That will play your sound, as before.

To stop it, create another keyframe that corresponds to the point in the timeline where you want the sound to stop.

Paste in the following;

myChannel.stop();

You might get issues with scope, but give it a go.

Some more examples here: http://www.republicofcode.com/tutorials/flash/as3sound/

Upvotes: 2

Marty
Marty

Reputation: 39456

Have a look at SoundChannel.stop().

There is example code provided that you can use and alter:

package {
    import flash.display.Sprite;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.text.TextField;
    import flash.events.MouseEvent;
    import flash.text.TextFieldAutoSize;

    public class SoundChannel_stopExample extends Sprite {
        private var snd:Sound = new Sound();
        private var channel:SoundChannel = new SoundChannel();
        private var button:TextField = new TextField();

        public function SoundChannel_stopExample() {
            var req:URLRequest = new URLRequest("MySound.mp3");
            snd.load(req);

            button.x = 10;
            button.y = 10;
            button.text = "PLAY";
            button.border = true;
            button.background = true;
            button.selectable = false;
            button.autoSize = TextFieldAutoSize.CENTER;

            button.addEventListener(MouseEvent.CLICK, clickHandler);

            this.addChild(button);
        }

        private function clickHandler(e:MouseEvent):void {
            var pausePosition:int = channel.position;

            if(button.text == "PLAY") {
                channel = snd.play(pausePosition);
                button.text = "PAUSE";
            } 
            else {
                channel.stop();
                button.text = "PLAY";
            }
        }
    }
}

Source: Adobe Livedocs - flash.media.SoundChannel

Essentially you assign a Sound to your SoundChannel, and then stop the entire channel (not the sound itself).

Upvotes: 0

Related Questions