要求
播放器演示已經在部落格banner部分給出了,下面還是上一截圖吧:
下面是程式的核心業務程式碼:
<?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" width="330" height="75" backgroundAlpha="1.0" backgroundColor="#9C4B4B" preloaderChromeColor="#060606" creationComplete="initApp()"> <fx:Declarations> <!-- 將非可視元素(例如服務、值物件)放在此處 --> </fx:Declarations> <fx:Script> <![CDATA[ import mx.collections.ArrayCollection; import mx.controls.Alert; /** * musicItem Object 音樂資訊物件 */ [Bindable] public var musicItem:Object; /** * 音樂是否正在播放 */ private var isplaying:Boolean = false; /** * 播放器音量 */ private var currentVolum:Number =0.5; /** * 正在播放的音樂的URL字串 * 從主程式中獲得 player.currentMusicUrlString=musicSource; */ public var currentMusicUrlString:String; /** * 正在播放的音樂的URLRequest */ private var currentMusicUrlRequest:URLRequest; /** * 正在播放的音樂的Sound */ private var currentMusicSound:Sound; /** * 正在播放的音樂的SoungChannel * SoundChannel 類控制應用程式中的聲音。每個聲音均分配給一個聲道,而且應用程式可以具有混合在一起的多個聲道。SoundChannel 類包含 stop() 方法、用於監控聲道幅度(音量)的屬性以及用於對聲道指定 SoundTransform 物件的屬性。 */ private var currentMusicChannel:SoundChannel; /** * 正在播放的音樂的 SoundTransform * SoundTransform 類包含音量和平移的屬性。 */ private var currentMusicTransform:SoundTransform; /** * 正在播放的音樂的總時間 */ private var currentMusicTotleTime:Number =0; /** * 正在播放的音樂的播放進度引數 */ private var currentMusicPosition:int =0; private var songs:ArrayCollection=new ArrayCollection([ {name:"初戀",singer:"張力尹",pic:"data/images/zly.jpg",uri:"data/chulian.mp3"}, {name:"純真的愛",singer:"張力尹",pic:"data/images/zly.jpg",uri:"data/chunzhendeai.mp3"}, {name:"相信愛",singer:"張力尹",pic:"data/images/zly.jpg",uri:"data/xiangxinai.mp3"}, {name:"星願(I WILL)",singer:"張力尹",pic:"data/images/zly.jpg",uri:"data/I will.mp3"}, {name:"TIMELESS",singer:"張力尹",pic:"data/images/zly.jpg",uri:"data/TIMELESS.mp3"}, {name:"BLUE",singer:"BigBang",pic:"data/images/ALIVE-BIGBANG.jpg",uri:"data/BigBang - BLUE.mp3"}, {name:"BLUE(指彈)",singer:"Gabriella Quevedo",pic:"data/images/ALIVE-BIGBANG.jpg",uri:"data/Gabriella Quevedo - blue.mp3"}, {name:"You're Beautiful",singer:"James Blunt",pic:"data/images/s1461123.jpg",uri:"data/You're Beautiful.mp3"}, {name:"Only Friends",singer:"Rita Calypso",pic:"data/images/s3786226.jpg",uri:"data/Only Friends.mp3"}, {name:"총맞은것처럼(像中槍一樣)",singer:"白智英",pic:"data/images/s3387692.jpg",uri:"data/Muz Cast - xiangzhongqiangyiyang.mp3"}, {name:"是否",singer:"G.E.M",pic:"data/images/150_albumpic_444525_0[1].jpg",uri:"data/G.E.M. - shifou.mp3"}, {name:"Young Girls",singer:"Bruno Mars",pic:"data/images/Bruno Mars.jpg",uri:"data/Bruno Mars - Young Girls.mp3"}, {name:"Nothing On You",singer:"B.O.B&Bruno Mars",pic:"data/images/Bruno Mars.jpg",uri:"data/B.o.B. - Nothing On You.mp3"}, {name:"Young Girls(指彈)",singer:"Gabriella Quevedo",pic:"data/images/Bruno Mars.jpg",uri:"data/Gabriella Quevedo-Young Girls.mp3"}, {name:"Im Yours(指彈)",singer:"Gabriella Quevedo",pic:"data/images/Bruno Mars.jpg",uri:"data/Im Yours -Gabriella Quevedo.mp3"}, {name:"金泰妍 - 가까이(靠近)",singer:"金泰妍 ",pic:"data/images/jintaiyan.jpg",uri:"data/closer.mp3"}, {name:"Grace",singer:"李秀英 ",pic:"data/images/s2868209.jpg",uri:"data/Grace.mp3"}, {name:"Twenty Nine",singer:"李秀英 ",pic:"data/images/s2722298.jpg",uri:"data/Twenty Nine.mp3"} ]); /** * 隨機獲取一首歌曲 */ private function getMusicItem():Object{ var index: Number = Math.round(Math.random()*(songs.length-1)); //產生一個隨機數 musicItem=songs[index]; return musicItem; } /** * 程式初始化 */ private function initApp():void{ //初始話一條資料 getMusicItem(); //UI控制元件事件監聽 playAndPause.addEventListener(MouseEvent.CLICK,musicPlay); //播放按鈕 next.addEventListener(MouseEvent.CLICK,autoPlayNext); playingProcess.addEventListener(Event.CHANGE,playingProcess_changeHandler); //進度條滾動事件 } private function musicPlay(event:MouseEvent):void{ if(!isplaying){ //播放 false //此狀態為 啟動播放器 然後點選播放按鈕 狀態(空狀態) if(currentMusicSound==null&¤tMusicChannel ==null){ currentMusicUrlString=musicItem.uri; currentMusicUrlRequest =new URLRequest(currentMusicUrlString); currentMusicSound = new Sound(); currentMusicSound.load(currentMusicUrlRequest); currentMusicSound.addEventListener(Event.COMPLETE,load_CompleteHandler); currentMusicChannel = currentMusicSound.play();//開始播放 timer_GetCurrentPositionHandler();//同步更新已經播放的時間的計時器 }else{//此狀態為 暫停後點選播放按鈕 狀態 currentMusicChannel = currentMusicSound.play(currentMusicPosition); } isplaying=true; }else{ //暫停 //此狀態為 播放過程中點選 暫停按鈕 狀態 currentMusicPosition = currentMusicChannel.position;//記錄暫停位置 currentMusicChannel.stop();//暫停 isplaying=false; } //currentMusicChannel.addEventListener(Event.SOUND_COMPLETE,autoPlayNext);//自動播放下一首 } /** * 自動播放下一首 和 next按鈕採用同樣的方式吧 * @param event * */ protected function autoPlayNext(event:Event):void{//過濾引數問題 getMusicItem(); if(currentMusicSound!=null&¤tMusicChannel!=null){ currentMusicChannel.stop();//暫停 } clearPar(); currentMusicUrlString=musicItem.uri; currentMusicUrlRequest =new URLRequest(currentMusicUrlString); currentMusicSound = new Sound(); currentMusicSound.load(currentMusicUrlRequest); currentMusicSound.addEventListener(Event.COMPLETE,load_CompleteHandler); playAndPause.selected=true; isplaying =true; currentMusicChannel = currentMusicSound.play();//開始播放 timer_GetCurrentPositionHandler();//同步更新已經播放的時間的計時器 currentMusicChannel.addEventListener(Event.SOUND_COMPLETE,autoPlayNext);//自動播放下一首 } /** * 正在播放的歌曲的總時長 */ private var len:int; /** * 檔案載入完成 能讀取到音樂的總時長 * @param event * */ protected function load_CompleteHandler(event:Event):void{ len = currentMusicSound.length; } /** * 同步更新已經播放的時間的計時器 * */ protected function timer_GetCurrentPositionHandler():void{ var clock:Timer = new Timer(100,int(len/1000/60*10));//每0.1秒更新一次 clock.start(); clock.addEventListener(TimerEvent.TIMER,showTime); } /** * 顯示已經播放的總時間 * @param event * */ protected function showTime(event:Event):void{ playingProcess.maximum = int(len/1000)*10;//最大值 playingProcess.value = int(currentMusicPosition/1000*10); //當前值 currentMusicPosition = currentMusicChannel.position; } /** * 播放進度條 可以拖動 * @param event * */ protected function playingProcess_changeHandler(event:Event):void{ if(currentMusicChannel!=null){ currentMusicPosition = playingProcess.value*1000/10;//當前音樂播放進度 currentMusicChannel.stop(); currentMusicChannel = currentMusicSound.play(currentMusicPosition); isplaying=true; playAndPause.selected=true; currentMusicChannel.addEventListener(Event.SOUND_COMPLETE,autoPlayNext);//自動播放下一首 }else{ playingProcess.value=0; } } /** * 清除引數 * currentMusicSound = null; * currentMusicChannel = null; * currentMusicPosition = 0; * */ private function clearPar():void{ currentMusicSound = null; currentMusicChannel = null; currentMusicPosition = 0; } ]]> </fx:Script> <s:Group verticalCenter="0" > <!--背景--> <s:Rect width="330" height="75"> <s:fill> <s:SolidColor color="#FFFFFF"/> </s:fill> </s:Rect> <s:BitmapImage source="{musicItem.pic}" smooth="true" width="75" height="75" /> <s:VGroup left="85" top="10" right="10" bottom="0" gap="0"> <s:HGroup height="45" width="235" verticalAlign="middle" > <s:VGroup height="45" gap="0" width="150"> <!--歌名--> <s:Label maxDisplayedLines="1" width="100%" height="25" fontFamily="微軟雅黑" fontSize="14" fontWeight="bold" text="{musicItem.name}" verticalAlign="middle"/> <!--歌手--> <s:Label maxDisplayedLines="1" width="100%" height="20" fontFamily="微軟雅黑" text="{musicItem.singer}" verticalAlign="middle"/> </s:VGroup> <s:HGroup width="100%" horizontalAlign="right" gap="30" paddingRight="5"> <!--next--> <s:Button id="next" skinClass="components._ButtonSkin1"/> <!--play--> <s:ToggleButton id="playAndPause" skinClass="components.pToggleButtonSkin1"/> </s:HGroup> </s:HGroup> <s:Group width="100%" height="20"> <!--進度條--> <s:ScrubBar id="playingProcess" horizontalCenter="0" verticalCenter="-2" skinClass="skinks.playercontrol.scrubbar.ScrubBar"/> </s:Group> </s:VGroup> </s:Group> </s:Application>
作者:Li-Cheng
本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線,否則保留追究法律責任的權利。