Revision: 23747
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at February 12, 2010 02:39 by alvincrespo
Initial Code
package{ //media import flash.media.Sound; import flash.media.SoundChannel; //net import flash.net.URLRequest; //events import flash.events.Event; import flash.events.IOErrorEvent; import flash.events.ProgressEvent; public class SoundManager{ private var _url:String = "music/"; private var _Sound:Sound; private var _URLRequest:URLRequest private var _SoundChannel:SoundChannel; private var _SongList:XMLList; private var _SongListLength:Number; private var _CurrentSongPosition:Number = 0; private var _isPlaying:Boolean = false; public function SoundManager():void{ trace("Sound Manager Instantiated"); }//end of constructor public function setSongList(pSongList:XMLList):void{ _SongList = pSongList; _SongListLength = _SongList.length(); }//end of setSongList public function StartMusic(pTrack:Number=NaN):void{ trace("\nStart Music"); //is the track Not a Number if(isNaN(pTrack)) pTrack = 0; //check to see if sound is playing if(_isPlaying){ _SoundChannel.stop(); //stop the sound _isPlaying = false; //set isPlaying to false, because it is no longer playing } _URLRequest = new URLRequest(_url+_SongList[pTrack].attribute('filename')); _Sound = new Sound(); _Sound.addEventListener(Event.COMPLETE, soundCompleteHandler); _Sound.addEventListener(Event.ID3, id3Handler); _Sound.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); _Sound.addEventListener(ProgressEvent.PROGRESS, progressHandler); _Sound.load(_URLRequest); _SoundChannel = _Sound.play(); _isPlaying = true; }//end of StartMusic public function stopSong():void{ if(_isPlaying){//is a song playing _SoundChannel.stop(); _isPlaying = false; } }//end of stopSong public function playSong():void{ if(!_isPlaying){//is a song NOT playing _SoundChannel = _Sound.play(); _isPlaying = true; } }//end of playSong public function playNextSong():void{ _CurrentSongPosition++; //go to the next position StartMusic(checkSongPosition(_CurrentSongPosition)); }//end of playNextSong public function playPreviousSong():void{ _CurrentSongPosition--; //go to a previous position StartMusic(checkSongPosition(_CurrentSongPosition)); }//end of playPreviousSong /* Check the position of the song that will be played next Important: Both previous and next buttons depend on this function Returns the position of the next song to be played */ private function checkSongPosition(pCurrentSongPosition:Number):Number{ if( Number(pCurrentSongPosition+1) > _SongListLength){ pCurrentSongPosition = 0; } else if( pCurrentSongPosition < 0){ pCurrentSongPosition = Number(_SongListLength - 1); } else{ } return pCurrentSongPosition; } //Event Handlers private function soundCompleteHandler(e:Event):void{ //trace("\nSong Completed Loading: " + e); } private function id3Handler(e:Event):void{ //trace("id3Handler: " + e); } private function ioErrorHandler(e:IOErrorEvent):void{ //trace("ioErrorHandler: " + e); } private function progressHandler(e:ProgressEvent):void{ //trace("progressHandler: " + e); } } }
Initial URL
Initial Description
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!
Initial Title
Sound Manager Class
Initial Tags
actionscript, 3
Initial Language
ActionScript 3