code_junkie

Check if SoundChannel is playing sound

Nov 11th, 2011
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. [Embed(source="song.mp3")]
  2. var Song: Class;
  3.  
  4. var s: Song = new Song();
  5. var ch: SoundChannel = s.play();
  6.  
  7. // how to check if ch is playing?
  8.  
  9. package
  10. {
  11. import flash.events.Event;
  12. import flash.media.Sound;
  13. import flash.media.SoundChannel;
  14.  
  15. public class SoundPlayer
  16. {
  17. [Embed(source="song.mp3")]
  18. private var Song:Class;
  19.  
  20. private var s:Song;
  21. private var ch:SoundChannel;
  22. private var isSoundPlaying:Boolean;
  23.  
  24. public function SoundPlayer()
  25. {
  26. s = new Song();
  27. play();
  28. }
  29.  
  30. public function play():void
  31. {
  32. if(!isPlaying)
  33. {
  34. ch = s.play();
  35. ch.addEventListener(
  36. Event.SOUND_COMPLETE,
  37. handleSoundComplete);
  38. isSoundPlaying = true;
  39. }
  40. }
  41.  
  42. public function stop():void
  43. {
  44. if(isPlaying)
  45. {
  46. ch.stop();
  47. isSoundPlaying = false;
  48. }
  49. }
  50.  
  51. private function handleSoundComplete(ev:Event):void
  52. {
  53. isSoundPlaying = false;
  54. }
  55. }
  56. }
Add Comment
Please, Sign In to add comment