zoukankan      html  css  js  c++  java
  • Sound 音乐工具类【转载】

    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;
        
        /*
        *使用方法:一般是直接在文档类初始化一个静态对象,比如定义为Main.soundManager
        *添加一个声音:Main.soundManager.addSound(声音id,声音url地址);//通过id访问管理器中的声音
        *添加并播放一个声音:Main.soundManager.addAndPlaySound(声音id,声音url地址,循环次数);//如果需要一直播放背景音,建议把循环次数设置为int.MAX_VALUE
        *播放指定声音:Main.soundManager.play(声音id,循环次数);//默认为播放一次
        *停止指定声音:Main.soundManager.stop(声音id);
        *获得指定声音的Sound对象:Main.soundManager.getSound(声音id);
        *获得指定声音的SoundChannel对象:Main.soundManager.getSoundChannel(声音id);
        *移除指定声音:Main.soundManager.removeSound(声音id);
        *设置音量大小:Main.soundManager.setVolume(声音大小);//0静音~1最大
        *停止播放所有声音:Main.soundManager.stopAllSound();
        */
    
    
        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])
                {
                    /**如果已经有这个音乐 那么就直接播放不添加**/
                    soundDic[id].play();
                    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;
                    var soundChannel:SoundChannel = sound.play(0,loops);
                    soundChannelDic[id] = soundChannel;
                }
            }
            public function play(id:String,loops:int=0):void
            {
                if (soundDic[id])
                {
                    if (soundChannelDic[id])                
                    {                    
                        soundChannelDic[id].stop();       
                        soundChannelDic[id] = soundDic[id].play(0, loops);    
                    }               
                    else       
                    {           
                        soundChannelDic[id] = soundDic[id].play(0, loops);   
                    } 
                }
            }
            public function stop(id:String):void
            {
                if (soundChannelDic[id])
                {
                    soundChannelDic[id].stop();
                }
            }
            public function getSound(id:String):Sound
            {
                if (soundDic[id])
                {
                    return soundDic[id];
                }
                return null;
            }
            public function getSoundChannel(id:String):SoundChannel
            {
                if (soundChannelDic[id])
                {
                    return soundChannelDic[id];
                }
                return null;
            }
            public function removeSound(id:String):void
            {
                if (soundDic[id])
                {
                    if (soundChannelDic[id])
                    {
                        soundChannelDic[id].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();
            }
        }
    }
  • 相关阅读:
    CefSharp 无法输入中文的问题
    CEF编译 执行gn args outRelease_GN_x86异常
    【Cef编译】 CefSharp编译失败,检测到“RuntimeLibrary”的不匹配项: 值“MT_StaticRelease”不匹配值“MD_DynamicRelease”
    ResourceDictionary主题资源替换(二) :编译期间,替换主题资源
    C# 打开文件/跳转链接
    C# 动态加载资源
    一套简约漂亮的响应式博客园主题皮肤分享给你们(一)
    Grok Debugger安装配置
    深入理解Java AIO(一)—— Java AIO的简单使用
    深入理解NIO(四)—— epoll的实现原理
  • 原文地址:https://www.cnblogs.com/602147629/p/2027604.html
Copyright © 2011-2022 走看看