flex 简单的mp3 播放器

 参考:http://www.javaeye.com/topic/686293

还未实现连续播放。。。

效果:

 

代码部分:

Code:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"   
  3.                xmlns:s="library://ns.adobe.com/flex/spark"   
  4.                creationComplete="init();"  
  5.                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" width="100%" height="100%">  
  6.     <s:layout>  
  7.         <s:HorizontalLayout verticalAlign="middle" horizontalAlign="center"/>  
  8.     </s:layout>  
  9.     <fx:Declarations>  
  10.         <!-- 将非可视元素(例如服务、值对象)放在此处 -->  
  11.     </fx:Declarations>  
  12.       
  13.     <fx:Script>  
  14.         <![CDATA[ 
  15.             import flash.utils.Timer; 
  16.              
  17.             import mx.collections.ArrayCollection; 
  18.             import mx.controls.Alert; 
  19.              
  20.             // 
  21.             private var acSound:ArrayCollection = new ArrayCollection(["butterfly.mp3", "life.mp3","rockstar.mp3"]); 
  22.              
  23.             //歌曲索引 
  24.             private var index:int = 0; 
  25.              
  26.             //使用 Sound 类创建 Sound 对象、将外部 MP3 文件加载到该对象并播放该文件、 
  27.             //关闭声音流,以及访问有关声音的数据,如有关流中字节数和 ID3 元数据的信息。 
  28.             private var sound:Sound; 
  29.              
  30.             //SoundChannel 类控制应用程序中的声音。 
  31.             private var channel:SoundChannel 
  32.              
  33.             //可以创建 Timer 对象,10ms运行一次,0 无限次执行。  
  34.             private var timer:Timer = new Timer(10, 0); 
  35.              
  36.             //是否为静音 
  37.             private var isQuite:Boolean = false; 
  38.              
  39.             //音量值 
  40.             private var volumnValue:Number; 
  41.              
  42.             //暂停位置 
  43.             private static var pausePos:int; 
  44.              
  45.             //播放进度 
  46.             private var tempValue:Number; 
  47.              
  48.              
  49.             //初始化播放器 
  50.             private function init():void { 
  51.                 loadSound(); 
  52.             } 
  53.              
  54.             //下载mp3 
  55.             private function loadSound():void { 
  56.                 if(channel != null) { 
  57.                     channel.stop(); 
  58.                 } 
  59.                 if(index == 0) { 
  60.                     previouseBtn.enabled = false; 
  61.                 } 
  62.                 else if(index >= acSound.length-1) { 
  63.                     nextBtn.enabled = false; 
  64.                 } 
  65.                 else if(index == acSound.length) { 
  66.                     index = 0; 
  67.                 } 
  68.                 else { 
  69.                     previouseBtn.enabled = true; 
  70.                     nextBtn.enabled = true; 
  71.                 } 
  72.                 //停止计时器。 
  73.                 timer.stop(); 
  74.                 sound = new Sound(); 
  75.                 //创建 URLRequest 对象,URLRequest 类可捕获单个 HTTP 请求中的所有信息 
  76.                 var url:URLRequest = new URLRequest(acSound[index]); 
  77.                  
  78.                 //load(stream:URLRequest, context:SoundLoaderContext = null):void 
  79.                 //启动从指定 URL 加载外部 MP3 文件的过程。 
  80.                 // stream:URLRequest — 指向外部 MP3 文件的 URL。     
  81.                 //context:SoundLoaderContext (default = null) — 一个可选的 SoundLoader 上下文对象, 
  82.                 //可以定义缓冲时间(MP3 数据保留到 Sound 对象的缓冲区中所需的最小毫秒数) 
  83.                 //并且可以指定加载声音前应用程序是否应该检查跨域策略文件。   
  84.                 sound.load(url); 
  85.                 timer.addEventListener(TimerEvent.TIMER, timerHandle); 
  86.                  
  87.                 //开始计时 
  88.                 timer.start(); 
  89.                  
  90.                 pausePos = 0; 
  91.                 channel = sound.play(); 
  92.                 playOrPauseBtn.label = "暂停"; 
  93.                 music.text = "--" + acSound[index] + "--"; 
  94.             } 
  95.              
  96.             //设置timer监听器,更新进度条值 
  97.             private function timerHandle(event:TimerEvent):void { 
  98.                 tempValue = (channel.position/sound.length)*100; 
  99.                 play_bar.setProgress(tempValue, 100); 
  100.             } 
  101.              
  102.             //播放、暂停事件处理 
  103.             private function playPauseHandle():void { 
  104.                 var str:String = playOrPauseBtn.label; 
  105.                 if(str == "暂停") { 
  106.                     channel.stop(); 
  107.                     pausePos = channel.position; 
  108.                     playOrPauseBtn.label = "播放"; 
  109.                 } 
  110.                 else {   
  111.                     if(pausePos != 0) { 
  112.                         channel = sound.play(pausePos); 
  113.                         playOrPauseBtn.label = "暂停"; 
  114.                     } 
  115.                     else { 
  116.                         loadSound();     
  117.                     } 
  118.                 }            
  119.             } 
  120.              
  121.             //停止事件处理 
  122.             private function stopHandle():void { 
  123.                 if(channel != null) { 
  124.                     timer.stop(); 
  125.                     play_bar.setProgress(100, 100); 
  126.                     channel.stop(); 
  127.                     pausePos = 0; 
  128.                     playOrPauseBtn.label = "播放"; 
  129.                 } 
  130.             } 
  131.              
  132.             //播放上一首 
  133.             private function playNext():void { 
  134.                 index--; 
  135.                 loadSound(); 
  136.             } 
  137.             //播放下一首 
  138.             private function playPrevious():void { 
  139.                 index++; 
  140.                 loadSound(); 
  141.             } 
  142.             //设置音量 
  143.             private function volumnHandle():void { 
  144.                 //public function SoundTransform(vol:Number = 1, panning:Number = 0) 
  145.                 // vol:Number (default = 1) — 音量范围从 0(静音)至 1(最大音量)。   
  146.                 //panning:Number (default = 0) — 声音从左到右的平移,范围从 -1(左侧最大平移) 
  147.                 //至 1(右侧最大平移)。值 0 表示没有平移(居中)。   
  148.  
  149.                 var soundTf:SoundTransform = new SoundTransform(volumn_hs.value, 0); 
  150.                 channel.soundTransform = soundTf; 
  151.             } 
  152.             //设置静音 
  153.             private function quiteBtnHandle():void { 
  154.                 var soundTf:SoundTransform; 
  155.                 if(!isQuite) { 
  156.                     soundTf = new SoundTransform(0, 0); 
  157.                     channel.soundTransform = soundTf; 
  158.                     volumnValue = volumn_hs.value; 
  159.                     volumn_hs.value = 0; 
  160.                     isQuite = true;  
  161.                 } 
  162.                 else { 
  163.                     soundTf = new SoundTransform(volumnValue, 0); 
  164.                     channel.soundTransform = soundTf; 
  165.                     isQuite = false; 
  166.                     volumn_hs.value = volumnValue; 
  167.                 }            
  168.             } 
  169.             //设置播放进度条 
  170.             private function play_barHandle(event:MouseEvent):void { 
  171.                 var str:String = playOrPauseBtn.label; 
  172.                 if(str == "暂停") { 
  173.                     channel.stop(); 
  174.                     channel = sound.play((event.localX/play_bar.width) * sound.length); 
  175.                 } 
  176.             }        
  177.         ]]>  
  178.     </fx:Script>  
  179.       
  180.     <mx:VBox width="50%" height="50%" paddingLeft="5" paddingRight="5" paddingTop="5"   
  181.              paddingBottom="5" borderStyle="solid">  
  182.         <mx:Label id="music"  width="122" fontSize="15"/>  
  183.         <!-- 用来摆的!! -->  
  184.         <mx:VideoDisplay width="100%" height="80%" />  
  185.         <!-- 播放的进度条 -->  
  186.         <mx:ProgressBar id="play_bar" width="100%" minimum="0" maximum="100"   
  187.                         labelPlacement="center" indeterminate="false" visible="true"  
  188.                         click="play_barHandle(event);" label="" mode="manual" direction="right" height="16"/>  
  189.         <mx:HBox width="100%" height="20%" verticalAlign="middle" fontSize="15" borderStyle="solid">  
  190.             <mx:LinkButton id="playOrPauseBtn" label="播放" click="playPauseHandle();"/>  
  191.             <mx:LinkButton id="stopBtn" label="停止" click="stopHandle();"/>  
  192.             <mx:LinkButton id="previouseBtn" label="上一首" click="playNext();"/>  
  193.             <mx:LinkButton id="nextBtn" label="下一首" click="playPrevious();"/>  
  194.             <mx:Spacer width="100%" />   
  195.             <mx:LinkButton id="quiteBtn" label="静音" click="quiteBtnHandle();"/>  
  196.             <!-- 控制音量 -->  
  197.             <mx:HSlider id="volumn_hs" change="volumnHandle();" value="4"/>  
  198.               
  199.         </mx:HBox>  
  200.     </mx:VBox>    
  201. </s:Application>  

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值