2004/12/21 | Flash版歌词秀
类别(Flash习作) | 评论(7) | 阅读(1793) | 发表于 02:27
说明:这个歌词秀既可演示字幕,又可编辑歌词
当左上角显示为演示状态时,点击“播放”即可演示字幕
当显示为编辑状态时,点击“记时”既可为歌词添加时间标签
时间标签中包含每个句子的起始时间和结束时间,均用60进制表示,
最末位的单位是60分之一秒

演示地址
--------------------------------------------
System.useCodepage = true;
//加载外部文本文件(文件路径,目标地址[,变量名称[,加载完毕后执行的函数]])
function loadTXT(url:String, temp:Object, func:Function) {
    var depth = this.getNextHighestDepth();
    var mc = this.createEmptyMovieClip("MC"+depth, depth);
    var tt = setInterval(test, 50);
    loadVariables(url, mc);
    function test() {
        if (mc.txtend != undefined) {
            temp.data = mc.jhdata;
            if (func != null) {
                func();
            }
            removeMovieClip(mc);
            clearInterval(tt);
        }
    }
}
//将10进制数N转换成d进制数(以0-9,A-Z,a-z字符表示),d为2-62
function jh_10toD(N:Number, d:Number) {
    N = Math.floor(N);
    d = d<2 ? 2 : d>62 ? 62 : d;
    var s, e = "";
    while (N>0) {
        s = N%d;
        if (s>35) {
            //36至62的数用a-z字符表示(s-36+97)
            s = String.fromCharCode(s+61);
        } else if (s>9) {
            //10至35的数用A-Z字符表示(s-10+65)
            s = String.fromCharCode(s+55);
        }
        e = s+e;
        N = Math.floor(N/d);
    }
    e = e != "" ? e : "0";
    return e;
}
//将d进制数str转换为10进制
function jh_Dto10(str:String, d:Number) {
    var code = 0, num = 0;
    for (var i = 0; i<str.length; i++) {
        code = str.charCodeAt(i);
        if (code>96) {
            code -= 61;
        } else if (code>64) {
            code -= 55;
        } else {
            code -= 48;
        }
        num += code*Math.pow(d, str.length-1-i);
    }
    return num;
}
//建立文本按钮(按钮字符,函数名,坐标,mc)
function get_button(BTtext:String, func:String, x:Number, y:Number, mc:MovieClip) {
    mc = mc != null ? mc : this;
    var depth = mc.getNextHighestDepth();
    mc.createTextField("BT"+depth, depth, x, y, 15*BTtext.length, 20);
    with (eval("BT"+depth)) {
        selectable = false;
        html = true;
        htmlText = "<A HREF=\"asfunction:"+func+"\"><font color='#cc3300'>"+BTtext+"</A>";
    }
}
//歌词处理:提取出每行的时间(换算为毫秒数)和歌词,存入数组(演示字幕用)
//如果没有时间信息,按同样格式转换,stime和etime置为""
function toarray(geci) {
    geci = geci.split("\r\n");
    for (var i in geci) {
        var temp = geci[i];
        var index1 = temp.indexOf(",");
        var index2 = temp.indexOf("]");
        geci[i] = {};
        if (index2 == -1) {
            if (temp == "" || temp == " ") {
                geci.splice(i, 1);
            } else {
                geci[i].stime = "";
                geci[i].etime = "";
                geci[i].txt = temp;
            }
        } else {
            geci[i].stime = 100/6*jh_Dto10(temp.slice(1, index1), 60);
            geci[i].etime = 100/6*jh_Dto10(temp.slice(index1+1, index2), 60);
            geci[i].txt = temp.slice(index2+1);
        }
    }
    return geci;
}
//记录时间(编辑歌词用),geci为Object数组,每个元素包括(stime,etime,txt)
function get_time() {
    if (loading.text == "编辑状态") {
        var time = myMU.position;
        //将1000分之一秒换算为60分之一秒,并转换为60进制数
        time = jh_10toD(time/100*6, 60);
        var i = Math.floor(counter/2);
        if (i<geci0.length) {
            if (counter%2 == 0) {
                geci0[i].stime = "["+time+",";
            } else {
                geci0[i].etime = time+"]";
            }
            var newhtml = "<P align='center'>";
            var sline = Math.max(i-2, 0);
            var eline = Math.max(Math.min(i+4, geci0.length), 6);
            var tempstr;
            for (var j = sline; j<eline; j++) {
                tempstr = geci0[j].stime+geci0[j].etime+geci0[j].txt;
                if (j == i) {
                    newhtml += "<FONT color='#cc6600'><b>"+tempstr+"</b></FONT><BR>";
                } else {
                    newhtml += tempstr+"<BR>";
                }
            }
            in_out.htmlText = newhtml.slice(0, newhtml.length-4)+"</P>";
            counter++;
        }
    }
}
//字幕演示
function mvtext(music:Sound, geci:Array) {
    var tt2 = setInterval(test, 100);
    var counter2 = 0;
    function test() {
        if (loading.text == "演示状态") {
            if (music.position-geci[counter2].stime>0) {
                while (music.position-geci[counter2].stime>0) {
                    counter2++;
                }
                counter2--;
                var newhtml = "<P align='center'>";
                var sline = Math.max(counter2-2, 0);
                var eline = Math.max(Math.min(counter2+4, geci.length), 6);
                for (var i = sline; i<eline; i++) {
                    if (i == counter2) {
                        newhtml += "<FONT color='#cc6600'><b>"+geci[i].txt+"</b></FONT><BR>";
                    } else {
                        newhtml += geci[i].txt+"<BR>";
                    }
                }
                in_out.htmlText = newhtml.slice(0, newhtml.length-4)+"</P>";
                counter2++;
                if (counter2>=geci.length) {
                    trace("kkkk");
                    clearInterval(tt2);
                }
            }
        }
    }
}
//
//建立状态显示框
this.createTextField("loading", this.getNextHighestDepth(), 13, 10, 100, 20);
loading.textColor = 0x339900;
loading.selectable = false;
//建立歌词显示框
this.createTextField("in_out", this.getNextHighestDepth(), 10, 10, 280, 100);
in_out.border = true;
in_out.multiline = true;
in_out.html = true;
in_out.htmlText = "";
//全局变量:
var geci0 = {};
var geci;
var counter = 0;
var stime = 0;
var myMU = new Sound(this);
//获取自身文件所在地址,加载同目录下同名txt文件,以获取mp3和txt的信息
var filepath = this._url.substr(0, this._url.length-3)+"txt";
var myurl = {};
loadTXT(filepath, myurl, loadMp3AndTxt);
delete filepath;
//获得mp3和txt的url信息后执行:
function loadMp3AndTxt() {
    myurl = myurl.data.split("\r\n");
    loadTXT(myurl[0], geci0, begin);
    myMU.loadSound(myurl[1], false);
    var myintervalID = setInterval(loadingtxt, 100);
    function loadingtxt() {
        var kk = Math.round(100*myMU.getBytesLoaded()/myMU.getBytesTotal());
        if (kk == 100) {
            go();
            clearInterval(myintervalID);
        }else{
            loading.text = "加载中"+kk+"%";
        }
        
    }
    delete myurl;
}
//获得歌词文本后执行:
function begin() {
    geci = toarray(geci0.data);
    geci0 = new Array();
    for(var i=0;i<geci.length;i++){
        geci0[i]={};
        geci0[i].txt=geci[i].txt;
        geci0[i].stime="";
        geci0[i].etime="";
    }
    var newhtml = "<P align='center'>";
    for (var i = 0; i<6; i++) {
        newhtml += geci[i].txt+"<BR>";
    }
    in_out.htmlText =newhtml.slice(0,newhtml.length-4)+ "</P>";
}
myMU.onLoad = go;
//音乐加载完毕后执行:
function go() {
    loading.text = "演示状态";
    get_button("[演示]", "_show", 80, 110);
    get_button("[编辑]", "_edit", 120, 110);
    get_button("[播放]", "_play", 160, 110);
    get_button("[暂停]", "_stop", 200, 110);
    get_button("[记时] ", "get_time", 240, 110);
}
function _edit() {
    loading.text = "编辑状态";
}
function _show() {
    loading.text = "演示状态";
}
function _stop() {
    stime = myMU.position/1000;
    myMU.stop();
}
function _play() {
    myMU.stop();
    myMU.start(stime);
    mvtext(myMU, geci);
}
myMU.onSoundComplete = _play;
this.onMouseUp = function() {
    if (eval(Selection.getFocus()) == in_out && loading.text == "编辑状态") {
        var temp = "<P align='center'>";
        for (var i = 0; i<geci0.length; i++) {
            temp += geci0[i].stime+geci0[i].etime+geci0[i].txt+"<BR>";
        }
        in_out.htmlText = temp+"</P>";
        Selection.setSelection(0, in_out.length);
    }
};
new _jh7086(100, null, 15, 112);
0

评论Comments