|
剛學(xué)AS不久,花了一天做的播放器,不足之處,望大師們指點!代碼可能有些亂,和我一樣的新手耐心點看哦!(在sound文件夾里面放四首歌,名字改對,就可以運行了)!
- package {
- import flash.display.Loader;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.MouseEvent;
- import flash.media.Sound;
- import flash.media.SoundChannel;
- import flash.net.URLLoader;
- import flash.net.URLRequest;
- import flash.text.TextField;
- public class PlayMain extends Sprite {
-
- var num:uint;//xml歌曲個數(shù)
- var pausePosition:int;//播放到的地方
- var currentIndex:int=0;//當(dāng)前播放第幾首歌,默認(rèn)為第一首
- var loadtxt:TextField=new TextField();//播放進度值監(jiān)聽文本
- var soundchannel:SoundChannel=new SoundChannel();
- var frame:Sprite=getLoad(290,2,0x2255aa);//播放進度中間的那根條
- var loadrect:Sprite=getLoad(2,8,0x009900);//播放進度小方塊
- var currentPlay:TextField=new TextField();//當(dāng)前播放歌曲
- //創(chuàng)建數(shù)組存儲歌曲列表信息
- var songListArray:Array=new Array();
- //創(chuàng)建按鈕數(shù)組存儲按鈕
- var buttonArray:Array=new Array();
- //創(chuàng)建按鈕文本數(shù)組
- var txtArray:Array=new Array();
- //創(chuàng)建數(shù)組存儲聲音對象
- var soundArray:Array=new Array();
- //創(chuàng)建數(shù)組存儲聲道對象
- var channelArray:Array=new Array();
-
-
- //構(gòu)造函數(shù)
- public function PlayMain() {
- init();
- }
- //初始化,加載XML數(shù)據(jù)
- private function init():void {
-
- //當(dāng)前播放歌曲文本
- addChild(currentPlay);
- currentPlay.x=60;
- currentPlay.y=60;
- currentPlay.width=200;
- currentPlay.text="沒有歌曲正在播放";
-
- //創(chuàng)建歌曲列表顯示文本
- for(var a:int=0;a<4;a++){
- var songtext:TextField=new TextField();
- songtext.selectable=false;
- songtext.width=200;
- songtext.textColor=0xff0000;
- addChild(songtext);
- songtext.x=400;
- songtext.y=20+a*20;
- songListArray.push(songtext);
- }
- //創(chuàng)建按鈕
- for(var j:int=0;j<5;j++){
- var sp:Sprite=getButton();
-
- addChild(sp);
- sp.x=j*60+50;
- sp.y=200;
- buttonArray.push(sp);
- }
-
- //創(chuàng)建文本
- for(var m:int=0;m<5;m++){
- var txt:TextField=new TextField();
- txtArray.push(txt);
- }
- //給文本賦值
- txtArray[0].text="播放";
- txtArray[1].text="暫停";
- txtArray[2].text="停止";
- txtArray[3].text="上一曲";
- txtArray[4].text="下一曲";
- //顯示文本到按鈕
- for(var i:int=0;i<5;i++){
- txtArray[i].selectable=false;
- buttonArray[i].addChild(txtArray[i]);
- }
-
- //顯示播放進度文本到舞臺
- stage.addChild(loadtxt);
- loadtxt.x=230;
- loadtxt.y=100;
-
- //顯示進度條到舞臺
-
- stage.addChild(frame);
- frame.x=50;
- frame.y=130;
- stage.addChild(loadrect);
- loadrect.x=50;
- loadrect.y=127;
-
- //填充數(shù)組
- for(var b:int=0;b<4;b++){
- var sound:Sound=new Sound();
- var soundchannel:SoundChannel=new SoundChannel();
- soundArray.push(sound);
- channelArray.push(soundchannel);
- }
- var xmlLoader:URLLoader=new URLLoader();
- xmlLoader.addEventListener(Event.COMPLETE,xmlComplete);
- xmlLoader.load(new URLRequest("xml/data.xml"));
- }
-
-
- //xml數(shù)據(jù)加載完成函數(shù)
- private function xmlComplete(evt:Event):void {
- var _xml:XML=new XML(evt.target.data);
- trace(_xml);
- num=_xml.item.length();
-
- for(var i:uint=0;i<num;i++){
- var soundurl:String=_xml.item[i].@url;//音樂路徑
- trace(soundurl);
- songListArray[i].text=soundurl.substring(6);
- //加載音樂
- soundArray[i].load(new URLRequest(soundurl));
-
- soundArray[i].addEventListener(Event.COMPLETE,soundComplete);
- }
- }
-
- //注冊偵聽
- private function soundComplete(evt:Event):void{
- trace("加載完成,請欣賞!..");
- buttonArray[0].addEventListener(MouseEvent.CLICK,onPlay);
- buttonArray[1].addEventListener(MouseEvent.CLICK,onPause);
- buttonArray[2].addEventListener(MouseEvent.CLICK,onStop);
- buttonArray[3].addEventListener(MouseEvent.CLICK,onPre);
- buttonArray[4].addEventListener(MouseEvent.CLICK,onNext);
- addEventListener(Event.ENTER_FRAME,onLoad);
- }
-
- //播放
- private function onPlay(evt:MouseEvent):void{
- trace("正在播放索引為"+currentIndex+"的歌曲");
- currentPlay.text="正在播放: "+songListArray[currentIndex].text;
- songListArray[currentIndex].textColor=0x00ff00;
- channelArray[currentIndex]=soundArray[currentIndex].play(pausePosition);
-
- }
- //暫停
- private function onPause(evt:MouseEvent):void{
- pausePosition=channelArray[currentIndex].position;
- channelArray[currentIndex].stop();
- trace("索引為"+currentIndex+"的歌曲暫停播放");
- }
- //停止
- private function onStop(evt:MouseEvent):void{
- channelArray[currentIndex].stop();
- pausePosition=0;
- trace("索引為"+currentIndex+"的歌曲停止播放");
- }
- //上一曲
- private function onPre(evt:MouseEvent):void{
- channelArray[currentIndex].stop();//停止現(xiàn)在歌曲的播放
- songListArray[currentIndex].textColor=0xff0000;
- currentIndex--;
- if(currentIndex>=0){
- channelArray[currentIndex]=soundArray[currentIndex].play();//播放下一曲
- trace("您操作了上一曲,現(xiàn)在播放索引為"+currentIndex+"的歌曲");
- }else{
- currentIndex=3;
- channelArray[currentIndex]=soundArray[currentIndex].play();
- trace("您操作了上一曲,現(xiàn)在播放索引為"+currentIndex+"的歌曲");
- }
- currentPlay.text="正在播放:"+songListArray[currentIndex].text;
- songListArray[currentIndex].textColor=0x00ff00;
- }
- //下一曲
- private function onNext(evt:MouseEvent):void{
- channelArray[currentIndex].stop();
- songListArray[currentIndex].textColor=0xff0000;
- currentIndex++;
- if(currentIndex>3){
- currentIndex=0;
- channelArray[currentIndex]=soundArray[currentIndex].play();
- trace("您操作了下一曲,現(xiàn)在播放索引為"+currentIndex+"的歌曲");
- }else{
- channelArray[currentIndex]=soundArray[currentIndex].play();
- trace("您操作了下一曲,現(xiàn)在播放索引為"+currentIndex+"的歌曲");
- }
- currentPlay.text="正在播放:"+songListArray[currentIndex].text;
- songListArray[currentIndex].textColor=0x00ff00;
- }
-
- private function onLoad(evt:Event):void{
- var currentPlayPercent:Number=channelArray[currentIndex].position/soundArray[currentIndex].length;
- loadtxt.text="播放進度:"+Math.round(currentPlayPercent*100)+"%";
- loadrect.x=50+currentPlayPercent*frame.width;
- }
- //繪制按鈕
- private function getButton():Sprite
- {
- var button:Sprite=new Sprite();
- button.graphics.beginFill(0x990000);
- button.graphics.drawRect(0,0,50,20);
- button.graphics.endFill();
- return button;
- }
- //繪制矩形條
- private function getLoad(width:int,height:int,color:uint):Sprite{
- var rect:Sprite=new Sprite();
- rect.graphics.beginFill(color);
- rect.graphics.drawRect(0,0,width,height);
- rect.graphics.endFill();
- return rect;
- }
- }
- }
復(fù)制代碼 xml部分
- <?xml version="1.0" ?>
- <song>
- <item url="sound/不開心與沒煩惱.mp3" />
- <item url="sound/愛的翅膀.mp3" />
- <item url="sound/貝多芬的悲傷.mp3" />
- <item url="sound/心愿.mp3" />
- </song>
復(fù)制代碼 | |