Flex播放器(实现播放、缓冲进度条和音频曲线显示(3)

// 隐藏一些内建的鼠标右键菜单项
                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;
            }
           
        ]]>
    </mx:Script>
    <mx:HBox verticalGap="0" verticalAlign="middle" horizontalAlign="center">
        <mx:Canvas borderColor="#CCCCCC" borderStyle="solid">
            <mx:Label x="5" fontSize="18" y="10" text=""/>
            <mx:HBox horizontalGap="0" verticalAlign="middle"
                 x="5" y="39" visible="false"></mx:HBox>
            <mx:ProgressBar label=""
                mode="manual" textAlign="left"
                labelPlacement="center" fontSize="3" x="10" y="97"
                minimum="0" maximum="100" barColor="yellow"
                trackColors="[white, haloSilver]"/>
            <mx:HSlider styleName="song" value="0"
                 showTrackHighlight="true" x="5" y="85" thumbDrag="songSlider_Change(event)"
                 minimum="0" maximum="100"
                 liveDragging="false" snapInterval="1"  toolTip="拖动调整播放进度">
            </mx:HSlider>
            <mx:Label x="243" y="41" text=""/>
            <mx:Label x="205" y="66" text="00:00 | 5:23"/>
            <mx:Button x="10" y="124" label="Play"/>
            <mx:Button x="74" y="124" label="Pause"/>
            <mx:Button x="152" y="124" label="Stop"/>
            <mx:HSlider styleName="volume" change="changeVolume(event)"
                 showTrackHighlight="true" value="0.5" x="222" y="133"
                  minimum="0" maximum="10" liveDragging="true"
                   snapInterval="0.1" toolTip="音量调节" />
            <mx:Label x="308" fontSize="18" y="10" text="歌曲列表"/>
            <mx:List alpha="0.25" x="308" y="43"
                 change="Xml_Change(event)" toolTip="点击选择歌曲"></mx:List>
        </mx:Canvas>
    </mx:HBox>
</mx:Application>

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzwpfw.html