Sound Manager Class


/ Published in: ActionScript 3
Save to your folder(s)

This is a sound manager class for a small project I've been working on for the past two days. Nothing really major or new, but just some simple functionality including play/stop/forward/previous functions that may help prove useful to someone. If you have any recommendations for improving this, I would love to hear about it. Thanks!


Copy this code and paste it in your HTML
  1. package{
  2. //media
  3. import flash.media.Sound;
  4. import flash.media.SoundChannel;
  5. //net
  6. import flash.net.URLRequest;
  7. //events
  8. import flash.events.Event;
  9. import flash.events.IOErrorEvent;
  10. import flash.events.ProgressEvent;
  11.  
  12. public class SoundManager{
  13.  
  14. private var _url:String = "music/";
  15. private var _Sound:Sound;
  16. private var _URLRequest:URLRequest
  17. private var _SoundChannel:SoundChannel;
  18. private var _SongList:XMLList;
  19. private var _SongListLength:Number;
  20. private var _CurrentSongPosition:Number = 0;
  21. private var _isPlaying:Boolean = false;
  22.  
  23. public function SoundManager():void{
  24. trace("Sound Manager Instantiated");
  25. }//end of constructor
  26.  
  27. public function setSongList(pSongList:XMLList):void{
  28. _SongList = pSongList;
  29. _SongListLength = _SongList.length();
  30. }//end of setSongList
  31.  
  32. public function StartMusic(pTrack:Number=NaN):void{
  33. trace("\nStart Music");
  34. //is the track Not a Number
  35. if(isNaN(pTrack))
  36. pTrack = 0;
  37.  
  38. //check to see if sound is playing
  39. if(_isPlaying){
  40. _SoundChannel.stop(); //stop the sound
  41. _isPlaying = false; //set isPlaying to false, because it is no longer playing
  42. }
  43.  
  44. _URLRequest = new URLRequest(_url+_SongList[pTrack].attribute('filename'));
  45. _Sound = new Sound();
  46. _Sound.addEventListener(Event.COMPLETE, soundCompleteHandler);
  47. _Sound.addEventListener(Event.ID3, id3Handler);
  48. _Sound.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  49. _Sound.addEventListener(ProgressEvent.PROGRESS, progressHandler);
  50. _Sound.load(_URLRequest);
  51. _SoundChannel = _Sound.play();
  52. _isPlaying = true;
  53.  
  54. }//end of StartMusic
  55.  
  56. public function stopSong():void{
  57. if(_isPlaying){//is a song playing
  58. _SoundChannel.stop();
  59. _isPlaying = false;
  60. }
  61. }//end of stopSong
  62. public function playSong():void{
  63. if(!_isPlaying){//is a song NOT playing
  64. _SoundChannel = _Sound.play();
  65. _isPlaying = true;
  66. }
  67. }//end of playSong
  68. public function playNextSong():void{
  69. _CurrentSongPosition++; //go to the next position
  70. StartMusic(checkSongPosition(_CurrentSongPosition));
  71. }//end of playNextSong
  72. public function playPreviousSong():void{
  73. _CurrentSongPosition--; //go to a previous position
  74. StartMusic(checkSongPosition(_CurrentSongPosition));
  75. }//end of playPreviousSong
  76.  
  77. /*
  78. Check the position of the song that will be played next
  79.  
  80. Important:
  81. Both previous and next buttons depend on this function
  82.  
  83. Returns the position of the next song to be played
  84. */
  85. private function checkSongPosition(pCurrentSongPosition:Number):Number{
  86. if( Number(pCurrentSongPosition+1) > _SongListLength){
  87. pCurrentSongPosition = 0;
  88. }
  89. else if( pCurrentSongPosition < 0){
  90. pCurrentSongPosition = Number(_SongListLength - 1);
  91. }
  92. else{
  93.  
  94. }
  95.  
  96. return pCurrentSongPosition;
  97. }
  98. //Event Handlers
  99. private function soundCompleteHandler(e:Event):void{
  100. //trace("\nSong Completed Loading: " + e);
  101. }
  102. private function id3Handler(e:Event):void{
  103. //trace("id3Handler: " + e);
  104. }
  105. private function ioErrorHandler(e:IOErrorEvent):void{
  106. //trace("ioErrorHandler: " + e);
  107. }
  108. private function progressHandler(e:ProgressEvent):void{
  109. //trace("progressHandler: " + e);
  110. }
  111.  
  112. }
  113. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.