Flash AS3 Sound, play, stop and pause [EN]
Click on the radio to start or pause the music. Click on the plug to stop the music. Music by Alphonso Steverink – We are all distorted.
This is the code i used to make the swf above:
stop(); // declaring variables var pausePoint:Number = 0.00;//Make a pausepoint, for resuming playback var soundClip:Sound = new Sound();//Make a soundobject var sndChannel:SoundChannel = new SoundChannel();//Make a soundchannel var isPlaying = false;//a variable for checking if the music is playing soundClip.load(new URLRequest("name.mp3"));//loading an mp3 into the soundobject StopKnop.addEventListener(MouseEvent.CLICK, onClickStop);//button for stopping the music SpeelOn.addEventListener(MouseEvent.CLICK, onClickPauze);//button to pause or play the music function onClickStop(evt:MouseEvent)//the stop function { if (isPlaying==true)//check if the sound is playing, if true then continue { sndChannel.stop();//stop soundchannel from playing isPlaying = false;//set playing var on false StopKnop.visible = false;//hide the stop button } pausePoint = 0.00;//return pausepoint to 0 } function onClickPauze(evt:MouseEvent)//the pause function { if (isPlaying==true)//check if the sound is playing, if true then continue { pausePoint = sndChannel.position;//remember the pause position sndChannel.stop();//stop soundchannel from playing isPlaying = false;//set playing var on false } else { sndChannel = soundClip.play(pausePoint);//resume playing from pausepoint isPlaying = true;//set playing var on true, sound is playing StopKnop.visible = true;//hide the stop button } } |
To stop all sounds playing on either a timeline or through actionscript, use the following code:
import flash.media.SoundMixer; SoundMixer.stopAll(); |
In short, if you would like to play a sound on your timeline use the following code:
var soundClip:Sound = new Sound();//Make a soundobject var sndChannel:SoundChannel = new SoundChannel();//Make a soundchannel soundClip.load(new URLRequest("name.mp3"));//loading an mp3 into the soundobject sndChannel=soundClip.play();//play the sound |
If you want to re-use this code for more sounds, don’t forget to adjust the var names “soundClip” and “sndChannel”.
It is possible that flash doesn’t play the mp3, depending on the used codec flash can give a streamerror. Use adobe photobooth or audacity to make a new mp3 without any compression. I always use mp3’s instead of other formats. In my opinion those work most of the time.
To stop the above sound from playing use:
sndChannel.stop();//channel stoppen |
If you would like to loop the sound, use the folowing code:
sndChannel=soundClip.play(0,999); |
The last number after the play function states how many times your sound is going to loop. 999 is the maximum you can enter.
When you want to put a soundeffect on a button, you will need the following code:
var soundClip:Sound = new Sound();//Make a soundobject var sndChannel:SoundChannel = new SoundChannel();//Make a soundchannel soundClip.load(new URLRequest("name.mp3"));//loading an mp3 into the soundobject knopMc.addEventListener(MouseEvent.CLICK, PlaySound);//listener for the button function PlaySound(evt:MouseEvent)//the function for the button { sndChannel=soundClip.play();//playing the sound gotoAndStop("framelabel");//other function } |
You can also import the sound into the library. When the sound is imported in the library, right click on the sound. Choose: properties, go to the actionscript tab and tick the export for actionscript box. Choose a logical name for the Class. The code needed to play that sound is:
var mySound:Sound = new backgroundSound(); mySound.play(); |
mySound = is the sound object, this has to be an unique name
backgroundSound = the class name you have entered in the properties window for your sound.