ActionScript 3 声音管理器类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionScript 3 声音管理器类相关的知识,希望对你有一定的参考价值。

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);		
		}
		
	}
}

以上是关于ActionScript 3 声音管理器类的主要内容,如果未能解决你的问题,请参考以下文章

ActionScript 3 使用文件参考API的AS3文件参考管理器类

ActionScript 3 xml加载器类

Windows 的声音管理器类

声音管理器类

ActionScript 3.0 声音不工作

使用 Actionscript 3.0 淡入声音