现在的位置: 首页 > 综合 > 正文

[as 3.0 学习笔记] 丑陋的mp3音乐播放器

2013年08月22日 ⁄ 综合 ⁄ 共 2930字 ⁄ 字号 评论关闭

为了检验自己学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来做游戏觉得开了眼界。加油~


附上丑陋的效果图:


抱歉!评论已关闭.