Code Snippets

  

ActionScript Source Code


Welcome to Dream.In.Code
Getting Help is Easy!

Join 131,932 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,764 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: 383

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


There are currently no comments for this snippet. Be the first to comment!

Add comment


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





Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month