分享

新手学写的flex音乐播放器

 菁清siso 2014-09-25
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
  xmlns:s="library://ns.adobe.com/flex/spark" 
  xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
  >
<!-- creationComplete="init()-->
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
private var sound:Sound = null;//建立一个音乐对象
private var soundchannel:SoundChannel = null;//建立一个声音控制对象
private var pausePosition:int ; 
private var choose:Boolean =true;
private var flag1:Boolean =true;
private var soundlength:Number =0.1;
private var myTimer:Timer = new Timer(10, 0); 
private var thisplay:int ; 
//初始化函数
private function init():void
if(flag1)
{
sound = new Sound();//新建一个音乐对象
sound.load(new
URLRequest("http://183.213.10.72:801/videoplayer/14945107241200128.mp3?ich_u_r_i=7d14481658ab022dd5602b60d19f443a&ich_s_t_a_r_t=0&ich_e_n_d=0&ich_k_e_y=9600659c7fe4f942f9f861239d3d1ea5c130790b637d39f9bd0029d73a6c7112&ich_t_y_p_e=1096&ich_d_i_s_k_i_d=2&ich_s_e_q=2473976&ich_u_n_i_t=1"));//音乐加载地址
soundchannel = sound.play();//音乐开始播放赋值给播放权限
//添加事件侦听器, 功能: 循环执行 timerHandler 函数(但这里还没有开始循环执行)   
myTimer.addEventListener(TimerEvent.TIMER, timerHandler);   
myTimer.start(); // 这里才开始循环执行 timerHandler 函数   
this.addEventListener(Event.ENTER_FRAME,xunhuan);//循环获取音乐播放进度
flag1=false;
tingzhi.enabled=true;
bofang.enabled=false;
}
}
private function stop():void
{
if(choose)
{
pausePosition = soundchannel.position;
soundchannel.stop();
choose =false;
}
}
private function huifu():void
{
if(!choose)
{
soundchannel = sound.play(pausePosition);
choose=true;
}
}
//事件循环
private function xunhuan(e:Event):void
var playbfb:Number = sound.bytesLoaded / sound.bytesTotal;//音乐加载百分比
soundlength = sound.length /playbfb;//获取到音乐总长度
thisplay= Math.round((soundchannel.position/soundlength)*100);//音乐当前播放长
bar.setProgress(thisplay,100);//更新进度条的值
bar.label = "音乐播放"+thisplay +"%";
if(thisplay==100)
{
this.removeEventListener(Event.ENTER_FRAME,xunhuan);//我们要学会清理垃圾
}
}
private function jinduCtrl():void
{
soundchannel.stop();
soundchannel = sound.play(dragHs.value*sound.length/10);  
}
//停止音乐:   
private function stopSound():void{   
if(!flag1)
{
myTimer.stop();   
soundchannel.stop(); // 正式停止音乐   
dragHs.value = 0; // 使那"播放进度"条的值回到0,即回到最左端  
flag1=true;
tingzhi.enabled=false;
bofang.enabled=true;
}
}   
//定义被 myTimer 循环执行的函数 timerHandler   
private function timerHandler(event:TimerEvent):void{   
dragHs.value =(soundchannel.position/soundlength)*10;  
}  
]]>
</fx:Script>
<mx:ProgressBar x="70" y="78" maximum="100" minimum="0" id="bar" mode="manual"/>
<mx:HSlider id="dragHs" x="65" y="120" width="210" change="jinduCtrl()" mouseDown="myTimer.stop()" mouseUp="myTimer.start()" />
<s:Button x="71" y="191" label="暂停" click="stop()"/>
<s:Button x="200" y="147" label="继续"  click="huifu()"/>
<s:Button x="70" y="147"  label="播放" id="bofang"  click="init()"/>
<s:Button x="200" y="191" label="停止" id="tingzhi" enabled="false" click="stopSound()"/>
</s:Application>


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多