Advertisement
angelhdz12

Sound Control

May 25th, 2015
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var music:Sound = new Sound();
  2. music.load(new URLRequest("music.mp3"));
  3. var channel:SoundChannel = new SoundChannel();
  4.  
  5. music.addEventListener(Event.COMPLETE, soundComplete);
  6. function soundComplete(e:Event):void
  7. {
  8.     //Remove COMPLETE event listener
  9.     music.removeEventListener(Event.COMPLETE, soundComplete);
  10.     //Play the sound
  11.     channel = music.play();
  12.     //Add the ENTER_FRAME event listener
  13.     addEventListener(Event.ENTER_FRAME, checkPosition);
  14. };
  15.  
  16. function checkPosition(e:Event):void
  17. {
  18.     //Divide the channel current position by the lenght of the loaded sound
  19.     //multiply by 100, and round to get the percentage.
  20.     var percentage:int = Math.floor((channel.position / music.length) * 100);
  21.    
  22.     //If percentage == 50, the sound is at half.
  23.     if(percentage == 50)
  24.     {
  25.         trace("Sound is at half");
  26.     }
  27.     trace(percentage+ "%");
  28. };
  29.  
  30. channel.addEventListener(Event.SOUND_COMPLETE, function(e:Event):void
  31. {
  32. //Sound finished playing, what to do?
  33. //Remove the checkPosition event listener
  34. removeEventListener(Event.ENTER_FRAME, checkPosition);
  35. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement