Skip to main content
Participating Frequently
December 23, 2009
解決済み

stop sound with action script?

  • December 23, 2009
  • 返信数 3.
  • 22695 ビュー

Hi,

Im a beginner with action script, and is there a way to stop a sound using actionscript?

My sound is called "CRICKET.mp3", if thats necessary to know.

このトピックへの返信は締め切られました。
解決に役立った回答 kglad

you can always use:

SoundMixer.stopAll();

to stop all sounds.  but, if you want to stop one particular sound, you should apply stop() to your sound's soundchannel (created when the play() method is applied to your sound).

返信数 3

sergey_landar
Inspiring
December 23, 2009

simple example with 2 buttons 'play' and 'stop', pls try to use it to clear how this work..

create 2 buttons and give them instance names: play_btn and stop_btn

put your .mp3 file to the same folder with your .fla file

and write code:

var req:URLRequest = new URLRequest("CRICKET.mp3");
var sound:Sound = new Sound();
var controller:SoundChannel;

function soundLoaded(event:Event):void
{
     controller = sound.play();
     controller.stop();
     play_btn.addEventListener(MouseEvent.CLICK, playSound);
     stop_btn.addEventListener(MouseEvent.CLICK, stopSound);
}

function playSound(event:MouseEvent):void
{
     controller = sound.play();
}

function stopSound(event:MouseEvent):void
{
     controller.stop();
}

sound.addEventListener(Event.COMPLETE, soundLoaded);
sound.load(req);

Benjamin Waller
Known Participant
December 23, 2009

Hi Sergey,

I tried to do what you said about playing and stopping sound using that code you posted but it didn't work for me!!

I have a wav sound file in the same folder as the .fla file but nothing! it doesn't show any errors in the code so I'm confused!!

Please have a look at the files I've attached!

Ben.

sergey_landar
Inspiring
December 23, 2009

Although there are various sound file formats used to encode digital audio, ActionScript 3.0, Flash Player and AIR support sound files that are stored in the mp3 format. They cannot directly load or play sound files in other formats like WAV or AIFF.

http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7d27.html#WS5b3ccc516d4fbf351e63e3d118a9b8f2ae-7fed

I think you can convert your .wav to .mp3

kglad
Community Expert
kgladCommunity Expert解決!
Community Expert
December 23, 2009

you can always use:

SoundMixer.stopAll();

to stop all sounds.  but, if you want to stop one particular sound, you should apply stop() to your sound's soundchannel (created when the play() method is applied to your sound).

Inspiring
August 10, 2024

hello man

how do i proceed in order to paste the SoundMixere.stopAll();

where do i place it in action script?

can you give an example?

love your helps

 

kglad
Community Expert
Community Expert
August 10, 2024

execute stopAll whenever you want to stop all sound

Inspiring
December 23, 2009

How are you playing the sound? Are you using a script or is it on the timeline? If script, could you share a bit of it so we know how you're starting the file?