[AS3]as3写了一个mp3播放器很简单附源代码
[AS3]as3写了一个mp3播放器很简单附源代码
代码可能有些乱,和我一样的新手耐心点看哦!(在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;//CuPlayer.com xml歌曲个数
- var pausePosition:int;//播放到的地方
- var currentIndex:int=0;//当前播放第几首歌,默认为第一首
- var loadtxt:TextField=new TextField();//播放进度值监听文本
- var soundchannel:SoundChannel=new SoundChannel();
- var frame:Sprite=getLoad(290,2,0x2255aa);//播放进度中间的那根条
- var loadrect:Sprite=getLoad(2,8,0x009900);//播放进度小方块
- var currentPlay:TextField=new TextField();//当前播放歌曲
- //创建数组存储歌曲列表信息
- var songListArray:Array=new Array();
- //创建按钮数组存储按钮
- var buttonArray:Array=new Array();
- //创建按钮文本数组
- var txtArray:Array=new Array();
- //创建数组存储声音对象
- var soundArray:Array=new Array();
- //创建数组存储声道对象
- var channelArray:Array=new Array();
- //构造函数
- public function PlayMain() {
- init();
- }
- //CuPlayer.com初始化,加载XML数据
- private function init():void {
- //当前播放歌曲文本
- addChild(currentPlay);
- currentPlay.x=60;
- currentPlay.y=60;
- currentPlay.width=200;
- currentPlay.text="没有歌曲正在播放";
- //创建歌曲列表显示文本
- 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);
- }
- //CuPlayer.com创建按钮
- 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);
- }
- //CuPlayer.com创建文本
- for(var m:int=0;m<5;m++){
- var txt:TextField=new TextField();
- txtArray.push(txt);
- }
- //CuPlayer.com给文本赋值
- 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;
- //填充数组
- 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数据加载完成函数
- 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);
- }
- }
- //CuPlayer.com注册侦听
- 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);
- }
- //CuPlayer.com暂停
- private function onPause(evt:MouseEvent):void{
- pausePosition=channelArray[currentIndex].position;
- channelArray[currentIndex].stop();
- trace("索引为"+currentIndex+"的歌曲暂停播放");
- }
- //CuPlayer.com停止
- private function onStop(evt:MouseEvent):void{
- channelArray[currentIndex].stop();
- pausePosition=0;
- trace("索引为"+currentIndex+"的歌曲停止播放");
- }
- //CuPlayer.com上一曲
- private function onPre(evt:MouseEvent):void{
- channelArray[currentIndex].stop();//停止现在歌曲的播放
- songListArray[currentIndex].textColor=0xff0000;
- currentIndex--;
- if(currentIndex>=0){
- channelArray[currentIndex]=soundArray[currentIndex].play();//播放下一曲
- trace("您操作了上一曲,现在播放索引为"+currentIndex+"的歌曲");
- }else{
- currentIndex=3;
- channelArray[currentIndex]=soundArray[currentIndex].play();
- trace("您操作了上一曲,现在播放索引为"+currentIndex+"的歌曲");
- }
- currentPlay.text="正在播放:"+songListArray[currentIndex].text;
- songListArray[currentIndex].textColor=0x00ff00;
- }
- //CuPlayer.com下一曲
- 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("您操作了下一曲,现在播放索引为"+currentIndex+"的歌曲");
- }else{
- channelArray[currentIndex]=soundArray[currentIndex].play();
- trace("CuPlayer.com您操作了下一曲,现在播放索引为"+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="CuPlayer.com播放进度:"+Math.round(currentPlayPercent*100)+"%";
- loadrect.x=50+currentPlayPercent*frame.width;
- }
- //CuPlayer.com绘制按钮
- 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;
- }
- //CuPlayer.com绘制矩形条
- 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;
- }
- }
- }
XML文件
- <?xml version="1.0" ?>
- <song>
- <item url="sound/不开心与没烦恼.mp3" />
- <item url="sound/爱的翅膀.mp3" />
- <item url="sound/贝多芬的悲伤.mp3" />
- <item url="sound/心愿.mp3" />
- </song>
热门文章推荐
- 纯HLS(m3u8)跨平台技术(HLSPlayer,m3u8Player跨平台多终端)
- DiscuzX3.2酷播视频插件(dz论坛自定义视频插件带广告
- [微信视频]实现网站中的视频在微信平台上正常播放(超多组图)
- [rtsp]海康威视监控摄像头实现web端无插件监控实拍效果
- 很酷,酷播wordpress视频插件(支持PC/安卓/苹果跨平台播放)
- [组图]微信视频技术:支持微信视频直播和视频点播
- [AS3]as3.0的rtmp流媒体播放器写法源代码示例
- 一步一步教你制作FLV网页视频播放器
请稍候...