[转]发一个声音管理类 没啥技术含量 就是图个方便~
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(); } }}
*使用方法:一般是直接在文档类初始化一个静态对象,比如定义为Main.soundManage
*添加一个声音:Main.soundManage.addSound(声音id,声音url地址);//通过id访问管理器中的声音
*添加并播放一个声音:Main.soundManage.addAndPlaySound(声音id,声音url地址,循环次数);//如果需要一直播放背景音,建议把循环次数设置为int.MAX_VALUE
*播放指定声音:Main.soundManage.play(声音id,循环次数);//默认为播放一次
*停止指定声音:Main.soundManage.stop(声音id);
*获得指定声音的Sound对象:Main.soundManage.getSound(声音id);
*获得指定声音的SoundChannel对象:Main.soundManage.getSoundChannel(声音id);
*移除指定声音:Main.soundManage.removeSound(声音id);
*设置音量大小:Main.soundManage.setVolume(声音大小);//0静音~1最大
*停止播放所有声音:Main.soundManage.stopAllSound();
一般游戏这个管理器就够用了 如果不够可以在上面继续改进~