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();
            }
        }
    }
  • 相关阅读:
    Ajax的基本使用
    Jquery--动画
    JQuery基础(选择器、事件、DOM操作)
    LinQ 创建连接、简单增删改查
    分页查询/组合查询
    分页查询、组合查询
    JavaScript
    WebForm Response和Request以及Cookie
    Response、Request、QueryString,修改,Cookies
    Repeater
  • 原文地址:https://www.cnblogs.com/602147629/p/2027604.html
Copyright © 2011-2022 走看看