Sounds工具类 库中有六种音乐

package demo.sound
{
	import flash.media.*;
	import flash.events.Event;
	import flash.utils.*;
	
	public class SoundMgr
	{
		//背景音乐
		public static var bgSound:Sound=new BgSound();
		public static var bgSoundChannel:SoundChannel = new SoundChannel;
		private static var bgSoundTransform:SoundTransform = new SoundTransform;
		//mouse is walking
		private static var mouseWalk:Sound=new MouseWalk();
		private static var mouseWalkSoundChannel:SoundChannel = new SoundChannel;
		private static var mouseWalkSoundTransform:SoundTransform = new SoundTransform;
		//cat wakes up
		private static var catWakeUp:Sound=new CatWakeUp();
		private static var catWakeUpSoundChannel:SoundChannel = new SoundChannel;
		private static var catWakeUpSoundTransform:SoundTransform = new SoundTransform;
		//food is picked up
		private static var pickUp:Sound=new PickUp();
		private static var pickUpSoundChannel:SoundChannel = new SoundChannel;
		private static var pickUpSoundTransform:SoundTransform = new SoundTransform;
		//a chili pepper is eaten
		private static var chiliEate:Sound=new ChiliEate();
		private static var chiliEateSoundChannel:SoundChannel = new SoundChannel;
		private static var chiliEateSoundTransform:SoundTransform = new SoundTransform;
		//cat is looking around
		private static var lookAround:Sound=new LookAround();
		private static var lookAroundSoundChannel:SoundChannel = new SoundChannel;
		private static var lookAroundSoundTransform:SoundTransform = new SoundTransform;

		public function SoundMgr():void
		{
			/*
			判断声音是否播放完
			if(sound.length == soundChannel.position)
			{ 
   				trace("song complete")
			}
			*/
		}
		
		//--------------------------------------------
		//背景音乐
		public static function playBgSound():void
		{
			bgSoundChannel = bgSound.play();
			bgSoundTransform.volume=10;
			bgSoundChannel.soundTransform=bgSoundTransform;
		}
		//背景音乐停止
		public static function stopBgSound():void
		{
			bgSoundChannel.stop();
		}
		//背景音乐播放完成
		public static function bgSoundComplete():void
		{
			if(bgSound.length == bgSoundChannel.position)
			{
				SoundMgr.playBgSound();
			}
		}
		
		//--------------------------------------------
		//mouse is walking
		public static function playMouseWalkSound():void
		{
			mouseWalkSoundChannel = mouseWalk.play(500);
			mouseWalkSoundTransform.volume=1;
			mouseWalkSoundChannel.soundTransform=mouseWalkSoundTransform;
			//输出音乐长度
			//trace(mouseWalk.length);
			//走路音乐播放完成
			//mouseWalk.addEventListener(Event.SOUND_COMPLETE,mouseWalkSoundComplete)
		}
		//停止走路音乐
		public static function stopMouseWalkSound():void
		{
			mouseWalkSoundChannel.stop();
		}
		//走路音乐播放完成
		public static function mouseWalkSoundComplete():void
		{
			if(mouseWalk.length == mouseWalkSoundChannel.position)
			{
				SoundMgr.playMouseWalkSound();
			}
		}
		//走路音乐播放完成
		/*private static function mouseWalkSoundComplete(e:Event):void
		{
			trace("走路音乐播放完毕!!!");
		}*/
		
		//--------------------------------------------
		//cat wakes up
		public static function playCatWakeUpSound():void
		{
			catWakeUpSoundChannel = catWakeUp.play();
			catWakeUpSoundTransform.volume=1;
			catWakeUpSoundChannel.soundTransform=catWakeUpSoundTransform;
		}
		//food is picked up
		public static function playPickUpSound():void
		{
			//从500毫秒开始播放
			pickUpSoundChannel = pickUp.play(500);
			pickUpSoundTransform.volume=1;
			pickUpSoundChannel.soundTransform=pickUpSoundTransform;
			//1500毫秒之后停止播放food is picked up
			setTimeout(stopPickUpSound,1000);
		}
		//一秒钟之后停止播放food is picked up
		public static function stopPickUpSound():void
		{
			pickUpSoundChannel.stop();
		}
		
		//--------------------------------------------
		//a chili pepper is eaten
		public static function playChiliEateSound():void
		{
			chiliEateSoundChannel = chiliEate.play();
			chiliEateSoundTransform.volume=0.2;
			chiliEateSoundChannel.soundTransform=chiliEateSoundTransform;
		}
		//停止a chili pepper is eaten
		public static function stopChiliEateSound():void
		{
			chiliEateSoundChannel.stop();
		}
		//a chili pepper is eaten音乐播放完成
		public static function chiliEateSoundComplete():void
		{
			if(chiliEate.length == chiliEateSoundChannel.position)
			{
				SoundMgr.playChiliEateSound();
			}
		}
		
		//--------------------------------------------
		//cat is looking around
		public static function playLookAroundSound():void
		{
			lookAroundSoundChannel = lookAround.play(1000);
			lookAroundSoundTransform.volume=0.2;
			lookAroundSoundChannel.soundTransform=lookAroundSoundTransform;
		}
		//停止cat is looking around
		public static function stopLookAroundSound():void
		{
			lookAroundSoundChannel.stop();
		}
		//cat is looking around音乐播放完成
		public static function lookAroundSoundComplete():void
		{
			if(lookAround.length == lookAroundSoundChannel.position)
			{
				SoundMgr.playLookAroundSound();
			}
		}
		
		public static function stopAllSound():void
		{
			SoundMixer.stopAll();
			/*stopBgSound();
			stopMouseWalkSound();
			stopPickUpSound();
			stopChiliEateSound();
			stopChiliEateSound();
			stopLookAroundSound();*/
		}
	}
}
原文地址:https://www.cnblogs.com/602147629/p/1956179.html