Copy link to clipboard
Copied
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.
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).
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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).
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
execute stopAll whenever you want to stop all sound
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
I think you can convert your .wav to .mp3
Copy link to clipboard
Copied
Ok! Thanks for the info!
Will try converting the files to mp3.
Cheers,
Ben.
Copy link to clipboard
Copied
Hey Sergey, great script!
Can you please give an example how the script will be if the sound will just start playing automatically and then stop after certain time without any buttons.
Thank you for your help.
Copy link to clipboard
Copied
var req:URLRequest = new URLRequest("yourSoundNameHere.mp3");
var sound:Sound = new Sound();
function soundLoaded(event:Event):void
{
sound.play();
}
sound.addEventListener(Event.COMPLETE, soundLoaded);
sound.load(req);
Copy link to clipboard
Copied
Yestarday i put link to page where you can find info about how to load .mp3... please click to it..
there you can find this code:
var req:URLRequest = new URLRequest("yourCode.mp3");
var s:Sound = new Sound(req);
s.play();
you can use it:)
Copy link to clipboard
Copied
Thanks Sergey,
I was more interested in how to stop the sound after certain time. For example - stop after 10 seconds when MySong.mp3 is 2 minutes long.
Also I don't know how to make the sound start after certain time, for example start after 5 seconds then play for 10 seconds and stop.
Thank you for you help, very much appreciated.
Copy link to clipboard
Copied
Find way how to start playing after some secons:
//2000 this is mean then your sound will start to play after 2 second from start.
//3 this is how many time you repeat your sound
var snd:Sound = new Sound(new URLRequest("manscreamdistance.mp3"));
snd.play(2000, 3);
Now need clear how to stop it:)
If find way will write..