`
learnmore
  • 浏览: 590087 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

flex之播放器练习

阅读更多
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()">
    <mx:Script>
    <![CDATA[
        import mx.rpc.events.AbstractEvent;
        import mx.controls.Alert;
        import mx.events.SliderEvent;
        import flash.media.*;
        private var myTimer:Timer=new Timer(5,0);
        private var mySong:Sound;
        private var ss:SoundChannel;
        private var location:Number;
        //设定是否处于暂停状态
        private var tag:Boolean=false;
        private var playTag:Boolean=false;
        //初始化数据
        private function init():void
        {
        mlist.dataProvider=musicList.item;
        }
    private function playSound():void
    {
    if(playTag)ss.stop();
    var playItem:String="http://team.11138.net/web/nancy/wzqskfj.mp3";
    message.text="正在播放:"+mlist.selectedItem.toString();
    //如果不是暂停
    if(!tag)
    {
    var request:URLRequest=new URLRequest(playItem);
    mySong=new Sound();
    mySong.load(request);
    ss=mySong.play();
    //将播放标记设置为true,表示正在播放
    playTag=true;
    }else
    {
       ss=mySong.play(location);
       playTag=true;
    }
    //在触发器运行时,一直在广播TimerEvent.TiMER,该事件定义处理方法,使滑块移动
    myTimer.addEventListener(TimerEvent.TIMER,sliderShow);
    //启动触发器
    ss.addEventListener(Event.SOUND_COMPLETE,thisComplete);
    myTimer.start();
    }
    //处理滑块时间问题
    private function sliderShow(event:TimerEvent):void
    {
    //当前播放的位置
    var position:Number=ss.position/mySong.length;
    //定义滑块的数值,当前播放的字节数/总字节数
    playProcess.setProgress(position,playProcess.maximum);
    playProcess.label=numberFormatter.format(position*100).toString()+"%";
    if(position>0.99)playProcess.label="100%";
   
    }
    //根据是否循环选项确定在所有播放曲目完成后是否循环播放
    private function thisComplete(event:Event):void
    {
    var index:int =mlist.selectedIndex+1;
    if(go.selected)
    {
    mlist.selectedIndex=index;
    var arr:Array=musicList.item;
    //播放
    playSound();
    //如果到达最后一首
    if(index==arr.length)
    {
    //如果选择了重复播放
    if(re.selected)
    {
    mlist.selectedIndex=0;
    }
    }
    }
    }
    //停止播放音乐
    private function stopSound():void
    {
    ss.stop();
    }
    //音量控制
    private function changeVol():void
    {
    var transform:SoundTransform=ss.soundTransform;
    transform.volume=myVol.value;
    ss.soundTransform=transform;
    }
    ]]>
    </mx:Script>
    <!--音乐列表数据-->
    <mx:Model id="musicList" source="music.xml"></mx:Model>
    <!--格式化数据-->
    <mx:NumberFormatter id="numberFormatter"
            precision="2"
            rounding="up" />
<mx:Panel x="102" y="20" width="348" height="368" layout="absolute" title="mp3播放器">
<mx:ProgressBar x="10" y="33" id="playProcess" label="[0,100%]" minimum="0" maximum="1" labelPlacement="right" indeterminate="false" enabled="true" mode="manual" themeColor="#F20D7A"/>
<mx:Label x="10" y="10" text="Label" width="200" id="message"/>
<mx:Button x="10" y="69" label="play" click="playSound()"/>
<mx:Button x="83" y="69" label="stop" click="stopSound()"/>
<mx:Button x="156" y="69" label="Button"/>
<mx:Button x="229" y="69" label="Button"/>
<mx:List x="10" y="99" width="211" height="219" id="mlist"></mx:List>
<mx:CheckBox x="229" y="248" label="连续播放" id="go"/>
<mx:CheckBox x="229" y="278" label="循环播放" id="re"/>
<mx:VSlider x="245" y="99" height="141" maximum="1" minimum="0" id="myVol" mouseUp="changeVol()" labels="音量"/>
</mx:Panel>

</mx:Application>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics