Flex播放器(实现播放、缓冲进度条和音频曲线显示)(flipside播放器)难以置信

随心笔谈2年前发布 编辑
166 0
🌐 经济型:买域名、轻量云服务器、用途:游戏 网站等 《腾讯云》特点:特价机便宜 适合初学者用 点我优惠购买
🚀 拓展型:买域名、轻量云服务器、用途:游戏 网站等 《阿里云》特点:中档服务器便宜 域名备案事多 点我优惠购买
🛡️ 稳定型:买域名、轻量云服务器、用途:游戏 网站等 《西部数码》 特点:比上两家略贵但是稳定性超好事也少 点我优惠购买

     horizontalScrollPolicy=”off” verticalScrollPolicy=”off” verticalAlign=”middle” horizontalAlign=”center”

     initialize=”init(event)” layout=”vertical” fontSize=”14″ paddingLeft=”0″ paddingTop=”0″ paddingRight=”0″ paddingBottom=”0″ >

            import mx.formatters.DateFormatter;

            import mx.effects.SoundEffect;

            import mx.events.SliderEvent;

            import mx.core.SoundAsset;

            import mx.controls.Alert;

            import mx.managers.CursorManager;

            import flash.media.*;

            import flash.utils.Timer;

            [Embed(source=”images/cursor.gif”)]

            private var cursorHand : Class;//图标

            private var xml:XML;

            private var xmlPath:String=”/flex/bin-debug/song.xml”;

            private var currIndex : Number=0;

            private var song :Sound;

            private var channel :SoundChannel;

            private var position : Number=0;

            // 保存 512 个声音波形的快照

            private var bytes:ByteArray=new ByteArray();

            // SoundBar 的个数

            private var barNum:uint=64;

            // 保存所有 SoundBar 的引用

            private var soundBars:Array=new Array();

            //定时器

            private var timer : Timer;

            //Application的initialize初试化事件

            private function init(event:Event):void

            {

                var loader:URLLoader=new URLLoader();

                loader.load(new URLRequest(xmlPath));

                loader.addEventListener(Event.COMPLETE,Xml_Complete);

                timer=new Timer(100);

                timer.addEventListener(TimerEvent.TIMER,timerTick);

                var barWidth:Number=boxSoundBar.width*1.00/barNum;

                // 初始化Canvas为音频条,放入舞台并加入数组

                for (var i:uint=0; i < barNum; i++)  {

                   var soundBar:Canvas=new Canvas();

                   soundBar.width=barWidth;

                   soundBar.height=boxSoundBar.height;

                   soundBar.x=i * barWidth;

                      soundBar.y=  0;

                   var g:Graphics=soundBar.graphics;

                   g.lineStyle(1,0x6688AA,1);

                   g.beginGradientFill(GradientType.RADIAL,[0x33cc00,0x456628],[1,1],[0,255],null,SpreadMethod.REFLECT,InterpolationMethod.RGB,0);

                   g.drawRect(0,0,soundBar.width,soundBar.height);

                   g.endFill();

                   boxSoundBar.addChild(soundBar);

                   soundBars.push(soundBar);

                }

                // 隐藏一些内建的鼠标右键菜单项

                this.contextMenu.hideBuiltInItems();

                var contextMenuItem : ContextMenuItem=new ContextMenuItem(“Powered By: Jonllen”);

                contextMenuItem.enabled=false;

                contextMenu.customItems.push(contextMenuItem);

                this.contextMenu.customItems.push(contextMenuItem);

                //更改鼠标图标

                CursorManager.setCursor(cursorHand);

            }

            //读取XML文件完成事件

            private function Xml_Complete(event:Event):void

            {

                xml=new XML(event.target.data);

                if(xml.item.length()>=1)

                {

                    listSong.dataProvider=xml.item.name;

                    listSong.selectedIndex=0;

                    //手动触发List的Change事件

                    listSong.dispatchEvent(new mx.events.ListEvent(Event.CHANGE, true, false));

                }

            }

            //List选择歌曲改变事件

            private function Xml_Change(event:Event):void

            {

                currIndex=event.target.selectedIndex;

                timer.stop();

                //停止声音文件的加载

                if( song!=null )

                {

                    //移除之前加载PROGRESS事件对songProgress进度条的控制

                    song.removeEventListener(ProgressEvent.PROGRESS,songProgress_Change);

                    if( song.isBuffering )

                        song.close();

                }

                song=new Sound();

                var url : String=xml.item[currIndex].url;

                var source:URLRequest=new URLRequest(url);

                song.load(source);

                song.addEventListener(ProgressEvent.PROGRESS, songProgress_Change);

                song.addEventListener(IOErrorEvent.IO_ERROR, songProgress_Error);

                position=0;

                songStart();

            }

            //歌曲播放完成

            private function songProgress_Complete(e:Event):void {

                if(currIndex==xml.item.length()-1) {

                    currIndex=0;

                }else {

                    currIndex++;

                }

                listSong.selectedIndex=currIndex;

                listSong.dispatchEvent(new mx.events.ListEvent(Event.CHANGE, true, false));

            }

            //加载歌曲失败

            private function songProgress_Error(e:IOErrorEvent):void {

                Alert.show(“文件不存在!”,”系统提示”);

            }

            //开始播放歌曲

            private function songStart():void {

                if ( channel !=null ){

                    channel.stop();

                }

                lblName.text=xml.item[currIndex].name;

                channel=song.play(position,int.MAX_VALUE);

                var length :Number=song.length*song.bytesTotal/song.bytesLoaded;

                var date : Date=new Date();

                date.time=length;               

                var dt : DateFormatter=new DateFormatter();

                dt.formatString=”NN:SS”;

                var totalTime : String=dt.format(date);

                date.time=channel.position;

                lblTime.text=dt.format(date) + ” | ” + totalTime;

                lblStatus.text=”播放”;

                var soundcontrol : SoundTransform=channel.soundTransform;

                soundcontrol.volume=volumeSlider.value;

                channel.soundTransform=soundcontrol;

                timer.start();

                boxSoundBar.visible=true;

            }

            //停止歌曲播放

            private function songStop():void {

                timer.stop();

                position=0;

                boxSoundBar.visible=false;

                lblTime.text=”00:00 |”+lblTime.text.split(“|”)[1];

                lblStatus.text=”停止”;

                songSlider.value=songSlider.minimum;

                songProgress.setProgress(songProgress.minimum,songProgress.maximum);

                if ( channel !=null )

                {

                    channel.stop();

                }

            }

            //暂停歌曲播放

            private function songPause():void {

                if ( channel !=null ){

                    timer.stop();

                    position=channel.position;

                    channel.stop();

                    lblStatus.text=”暂停”;

                }

            }

            //加载歌曲进度条显示

            private function songProgress_Change(e:ProgressEvent):void {

                var percent:int=Math.round(e.bytesLoaded * 100 / e.bytesTotal);

                songProgress.setProgress(e.bytesLoaded,e.bytesTotal);

            }

            //定时器方法

            private function timerTick( e:TimerEvent):void {

                if( channel!=null) {

                    var length :Number=song.length*song.bytesTotal/song.bytesLoaded;

                    var date : Date=new Date();

                    date.time=length;               

                    var dt : DateFormatter=new DateFormatter();

                    dt.formatString=”NN:SS”;

                    var totalTime : String=dt.format(date);

                    date.time=channel.position;

                    lblTime.text=dt.format(date) + ” | ” + totalTime;

                    songSlider.value=100*channel.position/length;

                    if( songSlider.value>=songSlider.maximum){

                        timer.stop();

                        songProgress_Complete(null);

                        return;

                    }

                    SoundMixer.computeSpectrum(bytes, false, 0);

                    for (var i:uint=0; i < barNum; i++)  {

                       soundBars[i].scaleY=bytes.readFloat();

                    }

                }

            }

            //歌曲进度调整事件

            internal function songSlider_Change(e:SliderEvent):void{

                timer.stop();

                if ( channel !=null ){

                    var length :Number=song.length*song.bytesTotal/song.bytesLoaded;

                    position=e.value*length/100;

                    songStart();

                }

            }

            //声音大小调整事件

            internal function changeVolume(evt:SliderEvent):void{

                if ( channel !=null ){

                    var soundcontrol : SoundTransform=channel.soundTransform;

                    soundcontrol.volume=evt.value;

                    channel.soundTransform=soundcontrol;

                }

            }

            //设置歌曲播放时间和总时间

            private function setTimeStatus():void {

                var length :Number=song.length*song.bytesTotal/song.bytesLoaded;

                var date : Date=new Date();

                date.time=length;               

                var dt : DateFormatter=new DateFormatter();

                dt.formatString=”NN:SS”;

                var totalTime : String=dt.format(date);

                date.time=channel.position;

                lblTime.text=dt.format(date) + ” | ” + totalTime;

            }

        ]]>

                 width=”192″ height=”50″ x=”5″ y=”39″ visible=”false”>

                width=”290″ height=”3″ mode=”manual” textAlign=”left”

                labelPlacement=”center” fontSize=”3″ x=”10″ y=”97″

                minimum=”0″ maximum=”100″ barColor=”yellow”

                trackColors=”[white, haloSilver]”/>

                 showTrackHighlight=”true” x=”5″ y=”85″ thumbDrag=”songSlider_Change(event)”

                 width=”300″ height=”22″ minimum=”0″ maximum=”100″

                 liveDragging=”false” snapInterval=”1″  toolTip=”拖动调整播放进度”>

                 showTrackHighlight=”true” value=”0.5″ x=”222″ y=”133″

                  width=”81″ minimum=”0″ maximum=”10″ liveDragging=”true”

                   snapInterval=”0.1″ toolTip=”音量调节” />

                 change=”Xml_Change(event)” width=”130″ toolTip=”点击选择歌曲”>

© 版权声明

相关文章