[转]发一个声音管理类 没啥技术含量 就是图个方便~

本文介绍了一个用于Flash的声音管理器类,该类可以帮助开发者更高效地加载、播放、停止声音资源,并提供了音量调节及全部声音停止的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[url]http://bbs.9ria.com/viewthread.php?tid=74163&extra=page%3D1%26amp;orderby%3Ddateline%26amp;filter%3D2592000[/url]

就不传附件了 免得消耗童鞋们的银子 直接上代码了~(代码没啥技术含量 就不加注释了)

/*声音管理
*@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();

一般游戏这个管理器就够用了 如果不够可以在上面继续改进~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值