用flash写 一个mp3播放器

类别:编程语言 点击:0 评论:0 推荐:

前二天在网上看了一个flash写的,
于是自己就改进了一下,修改了若干BUG,增加了一些功能。

主要的AS如下:
-------------------------------------------------------------------------------------
// Action script...
var myXML = new XML();
var mySound = new Sound();
var TrackCount = 0;
// [Action in Frame 1]
fscommand("allowscale", "false");
fscommand("showmenu", "false");
Stage.scaleMode = "noScale";
_quality = "height";
var fmt = new TextFormat();
fmt.font = "Tahoma";
fmt.size = 12;
//format
LCD.main.createTextField("mainText", 1, 0, 0, 78, 18);
LCD.main.mainText.selectable = false;
LCD.main.mainText.setTextFormat(fmt);
LCD.createTextField("hintText", 1, 4, 5, 78, 18);
LCD.hintText.setTextFormat(fmt);

var player = {};
player.selectedIndex = -1;
player.maxIndex = -1;
player.selectedItem = {};
player.subject = "";
player.mode = 0;
player.rplay = false;
player.intplay = true;
player.playtime = 3;
player.volumeValue = 70;
player.panValue = 0;
player.stoped = true;
player.setDefault = function() {
 with (LCD) {
  pause._visible = false;
  play._visible = false;
  stop._visible = true;
  main.mainText.text = "已经全部就绪";  
 }
 mySound.isLoaded = false;
 stopjump();
 progressbar.preloadBar._x = -147;
 progressbar.loadBar._x = -147;
 progressbar.semiBar._x = -147;
};
player.setDefault();
this.mp3list._visible = false;
this.copyright._visible = false;
this.LCD.max._visible = false;
this.volume.dot._x = 70-player.volumeValue/100*20;
//this.pan.dot._x = 70-player.panValue/100*20;
this.LCD.volumeLeft._width = player.volumeValue/100*20;
this.LCD.volumeLeft._x = 120-player.volumeValue/100*20;
this.LCD.volumeRight._width = player.volumeValue/100*20;
this.LCD.pan._x = 110+player.panValue/100*10;
this.LCD.stop._visible = true;
this.LCD.play._visible = false;
this.LCD.pause._visible = false;
this.LCD.repeatAll._visible = false;
this.LCD.repeatOne._visible = false;
var textMask = this.LCD.createEmptyMovieClip("textMask", 100);
textMask.beginFill(0, 100);
drawRectangle(textMask, 78, 18.5, 6, 6);
this.LCD.main.mainText.autoSize = true;
this.LCD.main.setMask(LCD.textMask);
setInterval(function () {
 LCD.main.mainText._x--;
 if (LCD.main.mainText._x<LCD.main.mainText._width*-1) {
  LCD.main.mainText._x = LCD.textMask._width;
 }
}, 80);
setInterval(function () {
 if (player.intplay) {
  player.select(0);
  player.intplay = false;
 }
}, 100);
player.select = function(index) {
 mp3list.mc.sList.shape._y = index*16+mp3list.mc.sList._y;
 player.selectedIndex = index;
 player.hasplay[index]++;
 player.selectedItem = eval("mp3list.mc.sList.l"+index);
 player.subject = player.labelAry[index];
 player.artist = player.artAry[index];
 LCD.main.mainText._x = 0;
 LCD.main.mainText.text = player.subject+"(Artist:"+player.artist+")";
 mySound.isLoaded = false;
 mySound.preloadDuration = 0;
 mySound.start(0);
 mySound.stop();
 stopjump();
 mySound.loadSound(player.dataAry[index], true);
 mySound.setVolume(player.volumeValue);
 mySound.start(0);
 playjump();
 mySound.allcomplete = false;
 Mshow.trackText.text = (index+1)+"/"+TrackCount;
 var goFrame = player.levelAry[index];
 Mshow.levelText.gotoAndStop(Number(goFrame)+1);
 LCD.stop._visible = false;
 LCD.pause._visible = false;
 LCD.play._visible = true;
 setInterval(function () {
  LCD.time.text = formatTime(mySound.position/1000 | 0);
 }, 500);
};
//System.useCodepage=true;
myXML.ignoreWhite = true;
myXML.load("mp3list.xml");
myXML.onLoad = function(succ) {
 if (succ) {
  LCD.main.mainText.text = "载入播放清单完成,可以欣赏音乐了.";
  var _l3 = this.firstChild.childNodes;
  var labelary = [];
  var dataary = [];
  var artary = [];
  var hasplay = [];
  var levelary = [];
  var _l1 = 0;
  TrackCount = _l3.length;
  while (_l1<TrackCount) {
   labelary[labelary.length] = _l3[_l1].attributes.n;
   dataary[dataary.length] = _l3[_l1].attributes.f;
   var _l2 = _l3[_l1].attributes.a;
   _l2 = _l2 != undefined ? (" - "+_l2) : ("");
   artary[artary.length] = _l2;
   var _l4 = _l3[_l1].attributes.l;
   _l4 = _l4 != undefined ? (_l4) : ("0");
   _l4 = _l4 > 5 ? ("5") : (_l4);
   levelary[levelary.length] =_l4;
   hasplay[hasplay.length] = 0;
   _l1++;
  }
  player.maxIndex = Math.min(labelary.length-1, dataary.length-1);
  player.labelAry = labelary;
  player.artAry = artary;
  player.dataAry = dataary;
  player.levelAry = levelary;
  player.hasplay = hasplay;
  getMp3List(mp3list, labelary, artary, 158, 98);
  Mshow.trackText.text = TrackCount ;
 } else {
  LCD.main.mainText.text = "载入播放清单出错了";
 }
};
function randomplay() {
 var j = 1;
 while ( j<=player.playtime ) {
  var i = 0;
  while (i<TrackCount) {
   var activeTrack = random(TrackCount-1);
   activeTrack = activeTrack+1;
   if (player.hasplay[activeTrack]<j) {
    player.select(activeTrack);
    exit;
   }
   i++;
  }
  j++;
 }
}
function stopjump() { 
  Mshow.effectbar.b1.gotoAndStop(1);
  Mshow.effectbar.b2.gotoAndStop(1);
  Mshow.effectbar.b3.gotoAndStop(1);
  Mshow.effectbar.b4.gotoAndStop(1);
  Mshow.effectbar.b5.gotoAndStop(1);
  Mshow.effectbar.b6.gotoAndStop(1);
  Mshow.effectbar.b7.gotoAndStop(1);
}
function pausejump() {
        Mshow.effectbar.b1.stop();
  Mshow.effectbar.b2.stop();
  Mshow.effectbar.b3.stop();
  Mshow.effectbar.b4.stop();
  Mshow.effectbar.b5.stop();
  Mshow.effectbar.b6.stop();
  Mshow.effectbar.b7.stop();
}
function playjump() {
 Mshow.effectbar.b1.play();
 Mshow.effectbar.b2.play();
 Mshow.effectbar.b3.play();
 Mshow.effectbar.b4.play();
 Mshow.effectbar.b5.play();
 Mshow.effectbar.b6.play();
 Mshow.effectbar.b7.play();
}
function setzero() {
 var i = 0;
 while (i<TrackCount) {
  player.hasplay[i] = 0;
  i++;
 }
}
function playselecte() {
 if (player.maxIndex <= player.selectedIndex) {
  if (player.rplay) {
   randomplay();
  } else if (player.mode == 0) {
   player.setDefault();
  } else if (player.mode == 1) {
   player.select(0);
  } else {
   player.select(player.selectedIndex);
  }
 } else {
  if (player.rplay) {
   randomplay();
  } else if (player.mode == 2) {
   player.select(player.selectedIndex);
  } else {
   player.select(player.selectedIndex+1);
  }
 }
}

mySound.isLoaded = false;
mySound.onLoad = function(succ) {
 if (succ) {
  mySound.isLoaded = true;
  player.timeNum = mySound.duration;
  if (player.timeNum | false) {
   LCD.timeNum.num.text = formatTime(player.timeNum/1000);
  } else {
   LCD.timeNum.num.text = "N/A.";
  }
 } else {
  stopjump();
  //jumpnext();  
 }
};
mySound.onSoundComplete = function() {
 stopjump();
 mySound.allcomplete = true;
 Mshow.trackText.text = "";
 Mshow.levelText.gotoAndStop(1);
 playselecte()
};
mySound.onID3 = function() {
 if (mySound.id3.TYER | false) {
  var _l3 = mySound.id3.TYER;
 } else if (mySound.id3.year | false) {
  _l3 = mySound.id3.year;
 } else {
  _l3 = " N/A.";
 }
 LCD.year.num.text = _l3;
 var _l2 = [];
 var _l1;
 _l1 = mySound.id3.TALB;
 if (_l1 != undefined) {
  _l2[_l2.length] = "Album:"+_l1;
 }
 _l1 = mySound.id3.TCOM;
 if (_l1 != undefined) {
  _l2[_l2.length] = "Composer:"+_l1;
 }
 _l1 = mySound.id3.TCOP;
 if (_l1 != undefined) {
  _l2[_l2.length] = "Copyright:"+_l1;
 }
 _l1 = mySound.id3.TRCK;
 if (_l1 != undefined) {
  _l2[_l2.length] = "Track:"+_l1;
 }
 _l1 = mySound.id3.TPUB;
 if (_l1 != undefined) {
  _l2[_l2.length] = "Publisher:"+_l1;
 }
 if (_l2.length>1) {
  _l1 = _l2.join(", ");
 } else if (_l2.length == 0) {
  _l1 = "";
 } else {
  _l1 = _l2[0];
 }
 LCD.main.mainText.text = player.subject+" (Artist: "+player.artist+(_l1.length>0 ? (", ") : (""))+_l1+" ) ";
};
function drawRectangle(myMc, width, height, Xoffset, Yoffset) {
 var _l1 = Xoffset;
 var _l11 = Yoffset;
 var _l2 = myMc;
 _l1 = _l1 != null ? (_l1) : (0);
 _l11 = _l11 != null ? (_l11) : (0);
 _l2.moveTo(_l1, _l11);
 _l2.lineTo(width+_l1, _l11);
 _l2.lineTo(width+_l1, height+_l11);
 _l2.lineTo(_l1, height+_l11);
 _l2.lineTo(_l1, _l11);
}
function getMp3List(parentmc, label, artary, width, height) {
 var mc = parentmc.createEmptyMovieClip("mc", 1);
 var shapeHeight = 16;
 var padding = 1;
 var borderWidth = 1;
 var scrollWidth = 11;
 var shapeWidth = width-borderWidth-padding*3-scrollWidth;
 var clientHeight = shapeHeight*label.length;
 var border = mc.createEmptyMovieClip("border", 100);
 border.lineStyle(1, 6710886);
 drawRectangle(border, width, height, padding*-1-borderWidth-0.8, -2.8);
 var background = mc.createEmptyMovieClip("background", -100);
 background.beginFill(16777215, 90);
 drawRectangle(background, width, height, padding*-1-borderWidth-0.2, -2.2);
 var sList = mc.createEmptyMovieClip("sList", 10);
 var _l2 = 0;
 var clicktim = 0;
 while (_l2<label.length) {
  var _l1 = sList.createEmptyMovieClip("l"+_l2, _l2);
  _l1.createTextField("label", 1, -3, -2.5, 200, shapeHeight);
  _l1.label.autoSize = false;
  _l1.label._width = width-padding*2-borderWidth*2-scrollWidth+20;
  _l1.label.selectable = false;
  _l1._y = shapeHeight*_l2;
  _l1.index = _l2;
  _l1.label.text = _l2+1+":"+label[_l2]+artary[_l2];
  _l1.label.setTextFormat(fmt);
  _l1.doubleclick = function() {
   var _l2 = this;
   var nowtim = getTimer();
   _l2.clicked = nowtim;
   _l2.clicked = getTimer();
   var subtim = nowtim-clicktim;
   clicktim = nowtim;
   return (subtim<=300 ? (true) : (false));
  };
  _l1.onPress = function() {
   var _l1 = (_ymouse-parentmc._y-mp3list.mc.sList._y)/shapeHeight | 0;
   if (this.doubleclick()) {
    player.select(_l1);
    shape._y = _l1*shapeHeight;
   }
   focusShape._y = _l1*shapeHeight;
  };
  _l2++;
 }
 var shape = sList.createEmptyMovieClip("shape", -1);
 shape.beginFill(65280, 30);
 drawRectangle(shape, shapeWidth, shapeHeight, -1, -1);
 var hintShape = sList.createEmptyMovieClip("hintShape", -2);
 hintShape.beginFill(65280, 10);
 drawRectangle(hintShape, shapeWidth, shapeHeight, -1, -1);
 var focusShape = sList.createEmptyMovieClip("focusShape", -3);
 focusShape.lineStyle(1, 65280, 50);
 drawRectangle(focusShape, shapeWidth, shapeHeight, -1, -1);
 mc.onMouseMove = function() {
  updateAfterEvent();
  hintShape._y = Math.max(0, Math.min(_ymouse-shapeHeight/2-parentmc._y, height-padding-borderWidth-shapeHeight)-(sList._y | 0));
 };
 var myMask = mc.createEmptyMovieClip("myMask", 99);
 myMask.beginFill(0, 100);
 drawRectangle(myMask, width-borderWidth-scrollWidth, height-borderWidth-padding*2, -2.5, -1.5);
 sList.setMask(myMask);
 var scrollbar = mc.createEmptyMovieClip("scrollbar", 97);
 scrollbar.beginFill(16382457);
 drawRectangle(scrollbar, scrollWidth, height-padding*2-borderWidth);
 var scrollbarBorder = scrollbar.createEmptyMovieClip("scrollbarBorder", 1);
 scrollbarBorder.lineStyle(1, 14935011);
 drawRectangle(scrollbarBorder, scrollWidth-1, height-padding*2-borderWidth, 0, -1.5);
 var scrollbutton = scrollbar.createEmptyMovieClip("scrollbutton", 2);
 scrollbutton.beginFill(10066329);
 drawRectangle(scrollbutton, scrollbar._width-5, 16, 1.5);
 scrollbar._x = width-scrollWidth-padding*2-borderWidth-0.5;
 scrollbar.onPress = function() {
  this.button1 = true;
 };
 scrollbar.onMouseUp = function() {
  this.button1 = false;
 };
 scrollbar.maxY = scrollbar._height-scrollbutton._height-5;
 scrollbar.onMouseMove = function() {
  if (this.button1) {
   scrollbutton._y = Math.max(0, Math.min(_ymouse-parentmc._y-scrollbutton._height/2-2, this.maxY));
   sList._y = Math.max(scrollbutton._y*clientHeight/(scrollbar._height-scrollbutton._height)*-1 | 0, myMask._height-clientHeight);
  }
 };
}
function fixSemiTime(time) {
 var _l1 = time;
 _l1>=10 ? (_l1) : ("0"+_l1);
 return (_l1>=10 ? (_l1) : ("0"+_l1));
}
function formatTime(time) {
 var s = "";
 return (fixSemiTime(time/60 | 0)+":"+fixSemiTime(time%60 | 0));
}
function hint(obj, hintStr) {
 var _l1 = obj;
 _l1.onRollOver = function() {
  LCD.hintText._visible = true;
  LCD.hintText.text = hintStr;
  LCD.main.mainText._visible = false;
 };
 _l1.onRollOut = function() {
  LCD.hintText._visible = false;
  LCD.hintText.text = "";
  LCD.main.mainText._visible = true;
 };
 _l1.onReleaseOutside = function() {
  LCD.hintText._visible = false;
  LCD.hintText.text = "";
  LCD.main.mainText._visible = true;
 };
}
var cbackground = copyright.createEmptyMovieClip("hintShape", 1);
cbackground.beginFill(16777215, 90);
drawRectangle(cbackground, 159, 99, -3.5, -3.5);
var cborder = cbackground.createEmptyMovieClip("focusShape", 1);
cborder.lineStyle(1, 0, 50);
drawRectangle(cborder, 158, 98, -2.8, -3);
cbackground.onPress = function() {
 return (false);
};
cbackground.useHandCursor = false;
var rightText = copyright.createTextField("rightText", 2, 0, -2, 153, 95);
copyright.rightText.html = true;
copyright.rightText.selectable = false;
copyright.rightText.htmlText = "<b><font color=\"#ff0000\">MP3Player</font></b><br/>----------------------------------------<br/>Version: 2.0 (2.0.1016)<br/>Author: <u><a href=\"http://landlordh.qqvip.com/\" target=\"_blank\"><font color=\"#0066cc\">landlord</font></a></u><br/>Publish Date: 10-16-2004<br/>----------------------------------------<br/>Special thanks to                <u>L3X</u>";
copyright.rightText.setTextFormat(fmt);

var mdown = false;
var xwidth = volume._width-volume.dot._width;
this.volume.onPress = function() {
 mdown = true;
};
this.volume.onMouseUp = function() {
 mdown = false;
};
this.volume.onMouseMove = function() {
 if (mdown) {
  updateAfterEvent();
  var _l2 = _xmouse-volume._x;
  var _l1 = 8;
  volume.dot._x = Math.max(_l1, Math.min(_l2, xwidth)-1)-3;
  player.volumeValue = ((volume.dot._x-1)/(volume._width-_l1-volume.dot._width-1)*100 | 0)-5;
  mySound.setVolume(player.volumeValue);
  LCD.hintText.text = "音量: "+player.volumeValue+"%";
  if (player.volumeValue == 0) {
   LCD.speaker._visible = false;
  } else if (player.volumeValue == 100) {
   LCD.max._visible = true;
  } else {
   LCD.speaker._visible = true;
   LCD.max._visible = false;
  }
  LCD.volumeLeft._width = player.volumeValue/100*20;
  LCD.volumeLeft._x = 120.2-player.volumeValue/100*20;
  LCD.volumeRight._width = player.volumeValue/100*20;
 }
};
this.pan.maxX = 70;
this.pan.offsetX = pan._x;
this.pan.dot._width = 18;
this.pan.dotWidth = 14;
this.pan.onPress = function() {
 this.mdown = true;
};
this.pan.onMouseUp = function() {
 this.mdown = false;
};
this.pan.onMouseMove = function() {
 if (this.mdown) {
  updateAfterEvent();
  pan.dot._x = Math.max(0, Math.min(_xmouse-pan.offsetX+pan.dotWidth/2, pan.maxX))+pan.dotWidth/2;
  player.panValue = (pan.dot._x-pan.dotWidth/2)/(pan.maxX-pan.dotWidth/3+4.500000)*200-100 | 0;
  if (player.panValue>-15 && player.panValue<15) {
   player.panValue = 0;
   pan.dot._x = pan.maxX/2+pan.dotWidth/1.700000;
  }
  LCD.hintText.text = "Balance: "+player.panValue;
  mySound.setPan(player.panValue);
  LCD.pan._x = 110+player.panValue/100*10;
 }
};
setInterval(function () {
 if (mySound.allcomplete){
  progressbar.preloadBar._x = -147;
  progressbar.loadBar._x = -147;
  
 }
 else if (!mySound.isLoaded) {
  progressbar.preloadBar._x = (mySound.getBytesLoaded()/mySound.getBytesTotal()*147 | 0)-147;
  mySound.preloadDuration = mySound.getBytesTotal()*mySound.duration/mySound.getBytesLoaded() | 0;
  LCD.timeNum.num.text = formatTime(mySound.preloadDuration/1000 | 0);
  progressbar.loadBar._x = (mySound.position/mySound.preloadDuration*147 | 0)-147;
 } else {
  progressbar.preloadBar._x = 0;
  progressbar.loadBar._x = (mySound.position/mySound.duration*147 | 0)-147;
 }
}, 500);
progressbar.onMouseDown = function() {
 this.button = true;
 if (_ymouse>progressbar._y && _ymouse<progressbar._height+progressbar._y && _xmouse>progressbar._x && _xmouse<progressbar._x+147 && !mp3list._visible) {
  progressbar.semiBar._visible = true;
  progressbar.semiBar._x = Math.min(Math.max(0, Math.min(_xmouse-progressbar._x, 147))-147, progressbar.preloadBar._x);
 }
};
progressbar.onMouseUp = function() {
 this.button = false;
 if (_ymouse>progressbar._y && _ymouse<progressbar._height+progressbar._y && _xmouse>progressbar._x && _xmouse<progressbar._x+147 && !mp3list._visible) {
  mySound.start((progressbar.semiBar._x+147)/147*mySound.duration/1000 | 0);
 }
 progressbar.semiBar._visible = false;
};
progressbar.onMouseMove = function() {
 updateAfterEvent();
 if (_ymouse>progressbar._y && _ymouse<progressbar._height+progressbar._y && this.button && !mp3list._visible) {
  progressbar.semiBar._x = Math.min(Math.max(0, Math.min(_xmouse-progressbar._x, 147))-147, progressbar.preloadBar._x);
 }
};
btPlay.onRelease = function() {
 playjump();
 if (mySound.position != 0 && !mySound.reseted && player.selectedIndex != -1) {
  mySound.start(mySound.position/1000);
 } else if (player.selectedIndex == -1) {
  player.select(0);
 } else {
  mySound.start(0);
 }
 LCD.stop._visible = false;
 LCD.pause._visible = false;
 LCD.play._visible = true;
};
btPause.onRelease = function() {
 mySound.stop();
 mySound.reseted = false;
 pausejump();
 LCD.stop._visible = false;
 LCD.pause._visible = true;
 LCD.play._visible = false;
};
btStop.onRelease = function() {
 setzero();
 stopjump();
 LCD.stop._visible = true;
    LCD.pause._visible = false;
    LCD.play._visible = false;
 mySound.stop();
 mySound.reseted = true;
 //stopjump();
 //LCD.stop._visible = true;
 //LCD.pause._visible = false;
 //LCD.play._visible = false;
};
btPreview.onRelease = function() {
 if (player.rplay) {
  randomplay();
 } else if (player.mode == 1 && player.selectedIndex == 0) {
  player.select(player.maxIndex);
 } else if (player.mode == 2) {
  player.select(player.selectedIndex);
 } else if ( player.selectedIndex <= 0 ) {
  LCD.hintText.text = "已经是第一首";
 } else {
  player.select(player.selectedIndex-1);
 }
};
btNext.onRelease = function() {
 if (player.rplay) {
  randomplay();
 } else if (player.mode == 1 && player.maxIndex == player.selectedIndex) {
  player.select(0);
 } else if (player.mode == 2) {
  player.select(player.selectedIndex);
 } else if (player.maxIndex <= player.selectedIndex) {
  LCD.hintText.text = "已经是最末首";
 } else {
  player.select(player.selectedIndex+1);
 }
};
btList.onRelease = function() {
 mp3list._visible = true;
};
btMode.onRelease = function() {
 if (player.mode == 0) {
  player.mode = 1;
  LCD.repeatOne._visible = false;
  LCD.repeatAll._visible = true;
  LCD.hintText.text = "重复所有";
 } else if (player.mode == 1) {
  player.mode = 2;
  LCD.repeatOne._visible = true;
  LCD.repeatAll._visible = false;
  LCD.hintText.text = "重复这首";
 } else {
  player.mode = 0;
  LCD.repeatOne._visible = false;
  LCD.repeatAll._visible = false;
  LCD.hintText.text = "不重复";
 }
};
btrplay.onRelease = function() {
 if (player.rplay) {
  player.rplay = false;
  LCD.hintText.text = "随机→顺序";
 } else {
  player.rplay = true;
  LCD.hintText.text = "顺序→随机";
 }
};
btRight.onRelease = function() {
 copyright._visible = true;
};
mp3list.btClose.onRelease = function() {
 mp3list._visible = false;
};
copyright.btClose.onRelease = function() {
 copyright._visible = false;
};
hint(btPreview, "上一首");
hint(btNext, "下一首");
hint(btPlay, "播放");
hint(btStop, "停止");
hint(btPause, "暂停");
hint(btList, "清单");
hint(btRight, "版权说明");
hint(progressbar, "播放进度");
hint(volume, "音量控制");
hint(pan, "音量平衡");
hint(btMode, "播放模式");
hint(btrplay, "随机播放");

---------------------------------------------------------------------------------------



本文地址:http://com.8s8s.com/it/it26542.htm