2004/11/23 | mp3Player——AS版
类别(Flash习作) | 评论(0) | 阅读(263) | 发表于 05:18
这个mp3Player完全由AS生成,精致小巧(不到2KB),其实还可以做得更小些

_________________________________________________________

代码分处两帧,第一帧加载数据,第二帧构造按钮,进度条,建立播放器
_________________________________________________________
第一帧:

stop();
Stage.scaleMode = "noScale";//禁止缩放
Stage.showMenu = false;
System.useCodepage=true;
music_list=[];
//xml数据载入
myXML = new XML();
myXML.load("music_list.xml");
myXML.ignoreWhite = true;
myXML.onLoad = function(success) {
if(success){//数据加载成功,将数据转换为数组形式
for (var i=0;i<400;i++){
     music_list[i]={};
     music_list[i].name=myXML.childNodes[1].childNodes[i].attributes.name;
     music_list[i].addres=myXML.childNodes[1].childNodes[i].attributes.addres;
}
     play(); //跳转到下一帧
}
}
_________________________________________________________
第二帧:

stop();
cl_1 = 0x003366;//暗色
cl_2 = 0x6699ff;//中亮色
cl_3 = 0xffffff;//高亮色
baseH = 10;
// 函数"MCtoBT"将MC定义为按钮,"mcid"就是mc的id名
function MCtoBT(mcid) {
    mcid.useHandCursor=false;//不使用小手状光标
    mcid.mycol = new Color(mcid);
    mcid.onRollOver = function() {
        this.mycol.setRGB(cl_2);//鼠标移过,颜色变亮(中亮色)
    };
    mcid.onRollOut = function() {
        if (oldP != this._name or !this.jilu) {
            this.mycol.setRGB(cl_1);//鼠标移出,颜色变暗(暗色)
        }
    };
    mcid.onPress = function() {
        this.mycol.setRGB(cl_3);//鼠标按下,颜色变亮(高亮色)
    };
    mcid.onRelease = function() {
        did = false;//这里表示:暂时还未响应按钮动作
        this.mycol.setRGB(cl_2);//鼠标放开,颜色变暗(中亮色)
        newP = this._name;
        if (oldP != newP) {
            this._parent[oldP].mycol.setRGB(cl_1);
            oldP = newP;
        }
    };
}
//
//
// 画三角形
MovieClip.prototype.sanjiao = function(x, w, h) {
    this.moveTo(x, h);
    this.lineStyle(0, 0x003366, 0);
    this.beginFill(0x003366, 100);
    this.lineTo(x+w, 0);
    this.lineTo(x, -h);
    this.lineTo(x, h);
    this.endFill();
};
// 画矩形,x位置,宽,高
MovieClip.prototype.juxing = function(x, w, h) {
    this.moveTo(x, h);
    this.lineStyle(0, 0x003366, 0);
    this.beginFill(0x003366, 100);
    this.lineTo(x+w, h);
    this.lineTo(x+w, -h);
    this.lineTo(x, -h);
    this.lineTo(x, h);
    this.endFill();
};
//
//_____________按钮制作__________________
//
// 上翻
a = this.createEmptyMovieClip("UU", baseH++);
a._x = 0;
a._y = 0;
a.jilu = false;
a.sanjiao(0, -5, 5);//绘制三角
a.juxing(-5, -2.5, 5);//绘制矩形
MCtoBT(a);
//
// 播放
a = this.createEmptyMovieClip("PP", baseH++);
a._x = 20;
a.jilu = true;
a.sanjiao(0, 7, 5);//绘制三角
MCtoBT(a);
// 暂停
a = this.createEmptyMovieClip("KK", baseH++);
a._x = 40;
a.jilu = true;
a.juxing(0, 2.5, 5);//绘制矩形
a.juxing(4, 2.5, 5);//绘制矩形
MCtoBT(a);
// 停止
a = this.createEmptyMovieClip("SS", baseH++);
a._x = 60;
a.jilu = false;
a.juxing(0, 7, 3.5);//绘制矩形
MCtoBT(a);
// 下翻
a = this.createEmptyMovieClip("DD", baseH++);
a._x = 80;
a.jilu = false;
a.sanjiao(0, 5, 5);//绘制三角
a.juxing(5, 2.5, 5);//绘制矩形
MCtoBT(a);
//
// loading进度条底色块
a = this.createEmptyMovieClip("MCdi", baseH++);
a._x = -10;
a._y = 10;
a.juxing(0, 100, 3);//绘制一个矩形
// loading进度条
a = this.createEmptyMovieClip("loadingMC", baseH++);
a._x = MCdi._x+2;
a._y = MCdi._y;
a.juxing(0, MCdi._width-4, 1);
a.mycol = new Color(a);
//鼠标点击时实现播放进度的改变
a.onPress = function() {
    start_time = (_xmouse-this._x)/this._width*myMUSIC.duration/1000;
    huakuaiMC._x = _xmouse;
    if (loaded) {
        myMUSIC.stop();
        myMUSIC.start(start_time);
        playing = true;
        PP.mycol.setRGB(cl_2);
        SS.mycol.setRGB(cl_1);
        KK.mycol.setRGB(cl_1);
        oldP = "PP";
        newP = "PP";
    }
};
// 滑块
a = this.createEmptyMovieClip("huakuaiMC", baseH++);
a._visible = 0;
a._x = loadingMC;
a._y = MCdi._y;
a.juxing(-2, 4, 1);
a.mycol = new Color(a);
a.mycol.setRGB(0xccffff);
//
// ____________音乐播放器_____________
//
// 音乐来源信息处理
music_number = 0;
myMUSIC = new Sound();
// loading函数
function myloading() {
    b = myMUSIC.getBytesLoaded();
    loadingMC._xscale = (b+1)/a*100;    //设置进度条的宽度
    huakuaiMC._visible = 0;    //隐藏滑块
    if (b == a) {    //如果下载完毕
        huakuaiMC._visible = 1;
        loadingMC.mycol.setRGB(cl_2);
        loaded = true;
        isloading = false;
    }
}
//
// 加载音乐函数
function load_music(number) {
    myMUSIC.loadSound(music_list[number].addres, false);
    isloading = true;
    loaded = false;
    a = myMUSIC.getBytesTotal();
    start_time = 0;//播放音乐的起始时刻
        //设置为自动播放
        newP = "PP";
        oldP = "PP";
        PP.mycol.setRGB(cl_2);
}
//
//
//
load_music(music_number);
did = false;
//设置为自动播放
playing = true;
newP = "PP";
oldP = "PP";
PP.mycol.setRGB(cl_2);
// 下面是主程序
this.onEnterFrame = function() {
    if (a == undefined) {    //如果没有得到音乐文件的总字节数
        a = myMUSIC.getBytesTotal();
    } else if (isloading) {    //如果处于正在加载状态
        myloading();
    } else if (loaded and playing) {    //如果已经加载完毕,且正在播放
        //设置滑块的位置:
        huakuaiMC._x = (myMUSIC.position/myMUSIC.duration)*loadingMC._width+loadingMC._x;
    }
    //————————————————————————————————
    if (newP == "PP" and !playing) {//如果"Play"按钮处于激活状态,而音乐没有播放
        did = true;
        if (loaded) {
            myMUSIC.stop();
            myMUSIC.start(start_time);
            playing = true;
        } else if (!isloading) {
            load_music(music_number);
        }
    }
    //————————————————————————————————
    if (!did) {    //有按钮被按下,但还没响应按钮动作
        did = true;
        playing = false;
        if (newP == "KK") {    //如果“暂停”按钮被激活
            start_time = myMUSIC.position/1000; //纪录下当前的播放进度(时刻)
            myMUSIC.stop();
        } else if (newP == "SS") { //如果“停止”按钮被激活
            start_time = 0;
            myMUSIC.stop();
            //将滑块置于进度条的最左端:
            huakuaiMC._x = loadingMC._x;
        } else if (newP == "UU") { //如果“后退”按钮被激活
            if (music_number>0) {
                music_number--;
            } else {
                music_number=music_list.length-1;
            }
            load_music(music_number);
        } else if (newP == "DD") { //如果“前进”按钮被激活
            if (music_number<music_list.length-1) {
                music_number++;
            } else {
                music_number=0;
            }
            load_music(music_number);
        }
    }
};
___________________________________________________________
0

评论Comments