[转]发一个声音管理类 没啥技术含量 就是图个方便~
http://bbs.9ria.com/viewthread.php?tid=74163&extra=page%3D1%26amp;orderby%3Ddateline%26amp;filter%3D2592000
就不传附件了 免得消耗童鞋们的银子 直接上代码了~(代码没啥技术含量 就不加注释了)
/*声音管理
*@by 古卧猫王 2011.2.18
*/
package{ import flash.events.Event; import flash.events.EventDispatcher; import flash.media.Sound; import flash.media.SoundChannel; import flash.media.SoundMixer; import flash.media.SoundTransform; import flash.net.URLLoader; import flash.net.URLRequest; import flash.utils.Dictionary; public class SoundManager extends EventDispatcher { private var soundDic:Dictionary; private var soundChannelDic:Dictionary; private var urlLoader:URLLoader; public function SoundManager() { super(); init(); } private function init():void{ soundDic = new Dictionary(); soundChannelDic = new Dictionary(); urlLoader = new URLLoader(); } public function addSound(id:String,url:String):void{ if(soundDic[id]){ return; } var sound:Sound = new Sound(); sound.addEventListener(Event.COMPLETE,addComplete); sound.load(new URLRequest(url)); function addComplete(e:Event):void{ sound.removeEventListener(Event.COMPLETE,addComplete); soundDic[id] = sound; } } public function addAndPlaySound(id:String,url:String,loops:int = 0):void{ if(soundDic[id]){ return; } var sound:Sound = new Sound(); sound.addEventListener(Event.COMPLETE,addAndPlayComplete); sound.load(new URLRequest(url)); function addAndPlayComplete(e:Event):void{ sound.removeEventListener(Event.COMPLETE,addAndPlayComplete); soundDic[id] = sound; soundChannelDic[id] = sound.play(0,loops); } } public function play(id:String,loops:int = 0):void{ if(soundDic[id]){ if(soundChannelDic[id]){ (soundChannelDic[id] as SoundChannel).stop(); soundChannelDic[id] = (soundDic[id] as Sound).play(0,loops); }else{ soundChannelDic[id] = (soundDic[id] as Sound).play(0,loops); } } } public function stop(id:String):void{ if(soundChannelDic[id]){ (soundChannelDic[id] as SoundChannel).stop(); } } public function getSound(id:String):Sound{ if(soundDic[id]){ return (soundDic[id] as Sound); } return null; } public function getSoundChannel(id:String):SoundChannel{ if(soundChannelDic[id]){ return (soundChannelDic[id] as SoundChannel); } return null; } public function removeSound(id:String):void{ if(soundDic[id]){ if(soundChannelDic[id]){ (soundChannelDic[id] as SoundChannel).stop(); soundChannelDic[id] = null; delete soundChannelDic[id]; } soundDic[id] = null; delete soundDic[id]; } } public function setVolume(value:Number):void{ SoundMixer.soundTransform = new SoundTransform(value); } public function stopAllSound():void{ SoundMixer.stopAll(); } }}