Chat LIVE With Programming Experts! There Are 23 Online Right Now...

 

Code Snippets

  

ActionScript Source Code


Welcome to Dream.In.Code
Become an Expert!

Join 244,287 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 979 people online right now. Registration is fast and FREE... Join Now!





Play/Pause/Stop Sound-AS3

This snippet will set up the code necessary to play/pause and stop a sound in actionscript 3.

Submitted By: Kerplope
Actions:
Rating:
Views: 9,165

Language: ActionScript

Last Modified: October 8, 2008
Instructions: You will need to create two buttons one that will be your play/pause button and another that that will be your stop button. Also be sure to define the path of the sound file.

Snippet


  1. //number that is redefined when the pause button is hit
  2. var pausePoint:Number = 0.00;
  3. //a true or false value that is used to check whether the sound is currently playing
  4. var isPlaying:Boolean;
  5.  
  6. //think of the soundchannel as a speaker system and the sound as an mp3 player
  7. var soundChannel:SoundChannel = new SoundChannel();
  8. var sound:Sound = new Sound(new URLRequest("SOUNDPATH/EVENDEEPER/SOUND.mp3"));
  9.  
  10.  
  11. //you should set the xstop and xplay values to match the instance names of your stop button and play/pause buttons
  12. xstop.addEventListener(MouseEvent.CLICK, clickStop);
  13. xplay.addEventListener(MouseEvent.CLICK, clickPlayPause);
  14.  
  15. soundChannel = sound.play();
  16. isPlaying = true;
  17.  
  18. function clickPlayPause(evt:MouseEvent) {
  19.         if (isPlaying) {
  20.                 pausePoint = soundChannel.position;
  21.                 soundChannel.stop();
  22.                 isPlaying = false;
  23.         } else if (!isPlaying) {
  24.                 soundChannel = sound.play(pausePoint);
  25.                 isPlaying = true;
  26.         }
  27. }
  28.  
  29. function clickStop(evt:MouseEvent) {
  30.         if (isPlaying) {
  31.                 soundChannel.stop();
  32.                 isPlaying = false;
  33.         }
  34.         pausePoint = 0.00;
  35. }

Copy & Paste


Comments


iggibiggy 2009-05-16 10:09:01

Thanks for the Code mate!! but i have one question for you...i have 4 play and stop buttons each to play and stop different sounds, but when i click on any play button it just play 1 type of sound....i have already made different play and stop functions for each sound and i have change all the URLRequest path to different sound file... but my 4 play buttons still play the same sound file....how do you code for playing 2 or more sound with each sound assigned a play button and when clicking on the button plays different sounds?


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month