爲了檢驗自己學as3的成效如何,自個寫了個mp3音樂播放器玩玩。功能很簡單:播放,暫停,選歌,連添加音樂文件路徑都直接在代碼裏面了,略懶呢~
下面是醜陋的代碼:
package
{
import fl.controls.Button;
import fl.controls.ComboBox;
import fl.controls.DataGrid;
import fl.controls.ScrollPolicy;
import fl.controls.UIScrollBar;
import fl.data.DataProvider;
import fl.video.SoundEvent;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filesystem.File;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.text.TextField;
public class MusicPlayer extends Sprite
{
private var playlist:Array;
private var bPlay:Button;
private var bStop:Button;
private var index = 1;
private var url = "";
private var soundControl:SoundChannel;
private var dg:DataGrid;
private var combox:ComboBox;
public function MusicPlayer()
{
playlist = getPlaylist();
showPlaylist(playlist);
}
//獲取目標文件夾裏的mp3文件,並以列表形式返回
public function getPlaylist():Array{
var arr = new Array();
var directory:File = File.documentsDirectory.resolvePath("file:///D:/music");
var directoryList:Array = directory.getDirectoryListing();
for(var i:uint = 0;i<directoryList.length;i++){
if(directoryList[i].extension == "mp3"){
// trace(directoryList[i].nativePath);
arr.push({label:directoryList[i].name,url:directoryList[i].url});
}
}
return arr;
}
//把獲取到的mp3文件列表展示成待播放列表
public function showPlaylist(playlist:Array):void{
// playlist.forEach(tracePlaylist);
combox = new ComboBox();
combox.dataProvider = new DataProvider(playlist);
combox.setSize(300,50);
combox.prompt = "點擊選擇";
combox.addEventListener(Event.CHANGE,choice);
addChild(combox);
bPlay = new Button();
bPlay.x = 455;
bPlay.y = 100;
bPlay.width = 40;
bPlay.height = 30;
bPlay.label = "play";
addChild(bPlay);
bPlay.addEventListener(MouseEvent.CLICK,playMusic);
bStop = new Button();
bStop.x = 455;
bStop.y = 150;
bStop.width = 40;
bStop.height = 30;
bStop.label = "stop";
addChild(bStop);
bStop.addEventListener(MouseEvent.CLICK,stopMusic);
}
//點擊play事件處理
public function playMusic(e:MouseEvent){
var path:URLRequest = new URLRequest(url);
var sound:Sound = new Sound();
sound.load(path);
soundControl = sound.play(startTime,0,null);
bPlay.removeEventListener(MouseEvent.CLICK,playMusic);
bPlay.addEventListener(MouseEvent.CLICK,replay);
}
//再次點擊play的事件處理
public function replay(e:MouseEvent){
soundControl.stop();
var path1:URLRequest = new URLRequest(url);
var sound1:Sound = new Sound();
sound1.load(path1);
soundControl = sound1.play(0,0,null);
sound1.addEventListener(Event.COMPLETE,playFinsh);
bPlay.addEventListener(MouseEvent.CLICK,replay);
}
//點擊stop事件處理
public function stopMusic(e:MouseEvent){
soundControl.stop();
startTime = soundControl.position;
bPlay.addEventListener(MouseEvent.CLICK,playMusic);
bPlay.removeEventListener(MouseEvent.CLICK,replay);
}
//選擇音樂的事件處理
public function choice(e:Event){
url = e.target.selectedItem.url;
}
//測試用的輸出
public function tracePlaylist(element:*,index:int,arr:Array):void{
trace (element.name);
}
}
}
代碼醜陋,功能簡單,計劃是要加上播放進度以及播放音量控制功能還有自定義的路徑修改框,但由於最近迷上了node.js,因爲看到了網易開源了他們基於node.js開發的遊戲框架pomolo,也試玩了他的online demo lord of pomelo,想快點入手,所以急急忙忙的記錄下這個半成品,準備着開始node.js的進一步學習,想想用js來做遊戲都覺得開了眼界。加油~
附上醜陋的效果圖: