12
返回列表 发新帖
楼主: 浪人

[【图释心语】] 有缘再相聚【AS3.0多行歌词习作】

[复制链接]

签到天数: 7 天

[LV.3]偶尔看看II

鲜花(0) 鸡蛋(0)
 楼主| 发表于 2018-12-10 22:15 | 显示全部楼层
主场景调用代码
import LrcReader;

var bgm:Sound = new Sound();
var sc:SoundChannel = new SoundChannel();
bgm.load( new URLRequest("https://img.yxlz8.com/wp-content/uploads/asgarosforum/109/01.mp3"));
SoundMixer.bufferTime = 10;
sc = bgm.play();
sc.addEventListener( Event.SOUND_COMPLETE , replay );

var timer:Timer = new Timer( 10 );
timer.addEventListener( "timer" , loop );
timer.start();

//调用LrcReader类加载并分析出歌词数据
var lrcdata:Array = [];
var lclrcReader = new LrcReader("https://ln2018.oss-cn-hangzhou.aliyuncs.com/gc/gxm/001.gif");
lcl.addEventListener( "complete" , lrcHandler );
lcl.addEventListener( "error" , lrcHandler );

function lrcHandler( e ){
if( e.type == "complete" ){
lrcdata = lcl.lrcToArray ;
}else{
trace("歌词分析失败!");
}
}

//显示歌词
var i:int = 0;

txt.autoSize = "left";
txtbg.autoSize = "left";
txtbg.mask = bar;

var ct:int = 0;
var tt:int = 0;

function loop( e ){
for( var l = i ; l < lrcdata.length ; l++ ){
if( int( sc.position / 10 ) >= lrcdata[l][0] ){
txt.text = lrcdata[l][1];
txtbg.text = txt.text;
ct = lrcdata[l][0] * 10;
if( l < lrcdata.length - 1 ){
tt = lrcdata[l + 1][0] * 10;
}else{
tt = bgm.length;
}
if( l + 1 < lrcdata.length ){
txtb.text = lrcdata[l+1][1];
}else{
txtb.text = "";
}
if( l + 2 < lrcdata.length ){
txtbb.text = lrcdata[l+2][1];
}else{
txtbb.text = "";
}
if( l - 1 >= 0 ){
txta.text = lrcdata[l-1][1];
}else{
txta.text = "";
}
if( l - 2 >= 0 ){
txtaa.text = lrcdata[l-2][1];
}else{
txtaa.text = "";
}
i++;
break;
}
}

bar.width = ( sc.position - ct ) / ( tt - ct ) * txtbg.width;

}

//
function replay( e ){
sc.stop();
sc = bgm.play();
sc.addEventListener( Event.SOUND_COMPLETE , replay );
i = 0;
}
////////频谱////////


//var revolt:Revolt = new Revolt("bgn",550, 300);
//addChild(revolt);


签到天数: 7 天

[LV.3]偶尔看看II

鲜花(0) 鸡蛋(0)
 楼主| 发表于 2018-12-10 22:18 | 显示全部楼层
LrcReader.as类
新建类LrcReader.as
代码如下
package {

import flash.events.EventDispatcher;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLLoaderDataFormat;
import flash.utils.ByteArray;
import flash.xml.*;


public class LrcReader extends EventDispatcher {

private var byte:ByteArray;
private var lrcStr:String;
private var timeArr:Array;
private var lrcArr:Array;
private var reg:RegExp;
private var lrccode:String;
private var gs:String;
private var xmld:XML;

//实例化(文件路径,编码 )
public function LrcReader( url:String = null , code:String = "黑体" ) {
byte = new ByteArray();
lrcStr="";
timeArr=[];
lrcArr=[];
reg=/[\n\r]/;
lrccode=code;
if( url != null ){

var loader:URLLoader = new URLLoader();
var urlrq:URLRequest = new URLRequest( url );

if( url.slice( url.length - 3 , url.length ) != "xml" ){
loader.dataFormat = URLLoaderDataFormat.BINARY;
gs = "txt";
}else{
gs = "xml";
}

loader.load( urlrq );
loader.addEventListener(Event.COMPLETE , loadComplete );
}
}

//歌词加载完成
private function loadComplete( e:Event ):void {
if( gs == "txt" ){
byte = e.target.data as ByteArray;
lrcStr = byte.readMultiByte( byte.bytesAvailable , lrccode );
strToData( lrcStr );
}else{
xmld = new XML( e.target.data );
xmlToData( xmld );
}

}

//分析歌词
public function strToData( strdata:String ){
timeArr=redTime(strdata,"[","]");
lrcArr=redLrc(strdata,"]");
breakFinish();
}

//分析xml歌词
public function xmlToData( xm:XML ){
for( var xl:int = 0; xl < xm.Lyrics.Lrc.length() ; xl ++ ){
timeArr.push( int( Number( xm.Lyrics.Lrc[ xl ].@Tag ) / 10 ));
if( xm.Lyrics.Lrc[ xl ] == "" ){
lrcArr.push("-" );
}else{
lrcArr.push( xm.Lyrics.Lrc[ xl ] );
}
}

breakFinish();
}

//分解完毕
private function breakFinish(){
if (timeArr.length>0&&lrcArr.length>0&&timeArr.length==lrcArr.length) {
this.dispatchEvent( new Event ("complete"));
} else {
this.dispatchEvent( new Event ("error"));
trace( timeArr.length );
trace( lrcArr.length );
}
}

//读取并分析歌词文件里的时间
private function redTime( obj:String , fst:String , end:String ):Array {

var arr:Array=[];
var tim:Array=[];

var ih:Boolean=false;
var st:int=0;
var et:int=0;
var sr:String = "";

for (var i = 0; i < obj.length; i ++) {

if (obj.charAt(i)==fst) {
st=i;
ih=true;
}

if (obj.charAt(i)==end&&ih) {
et=i;
if (ih) {
ih=false;
if (isNaN(Number(obj.charAt(st+1)))==false) {
arr.push( obj.slice( st + 1 , et ) );
}else{
arr.push( "00:00.00" );
}
}
}

}

for (var a in arr) {
tim.push( strToNumber( arr[a] ));
}

return tim;

}

//将文本格式的时间转换成数字格式
private function strToNumber( strs:String ):Number {

var num:Array=[];
var nums:Number=0;

for (var s:int = 0 ; s < strs.length ; s++ ) {
if ( isNaN(Number(strs.charAt(s))) == false ) {
num.push( Number( strs.charAt( s ) ));
}
}

//歌词文件的时间模式为[01:59.99]
nums=num[5];
nums+=num[4]*10;
nums+=num[3]*100;
nums+=num[2]*1000;
nums+=num[1]*1000*6;
nums+=num[0]*1000*60;

return nums;

}

//读取并分析歌词文件里的歌词部分
private function redLrc( obj:String , fst:String ):Array {

var lra:Array=[];
var ih:Boolean=false;
var st:int=0;
var et:int=0;
var sr:String = "";

for (var i = 0; i < obj.length; i ++) {
if (obj.charAt(i)==fst) {
st=i;
ih = true;
}
if (ih) {
if (reg.test(obj.charAt(i))==true) {
sr = obj.slice( st + 1 , i );
if( sr.length == 0 ){
sr = " ";
}
lra.push(sr);
ih=false;
}
}
}

return lra;

}

//获取已分析的歌词数据
public function get lrcToArray():Array {
var lrcdata:Array=[];
if (timeArr.length==lrcArr.length) {
for (var j in timeArr) {
lrcdata.push([timeArr[j],lrcArr[j]]);
}
}

return lrcdata;
}

//获取文本形式的歌词数据
public function get lrcToString():String {
return lrcStr;
}

}
}

签到天数: 1244 天

[LV.10]以坛为家III

鲜花(41) 鸡蛋(0)
发表于 2018-12-10 22:37 | 显示全部楼层
朋友,感谢你制作如此精美的音画与大家分享,与你网络相遇,是一种幸运,因为有你,中画论坛多了一份光彩;因为有你,中画论坛多了一个朋友;真诚地祝福:你在中画快乐、开心!

该用户从未签到

鲜花(0) 鸡蛋(0)
发表于 2018-12-12 11:02 | 显示全部楼层
问好,谢谢分享精彩
您需要登录后才可以回帖 登录 | 中文注册

本版积分规则

网站地图|小黑屋|Archiver|中画网 ( 蜀ICP备11021737号-2 )|网站地图

GMT+8, 2024-11-25 04:20 , Processed in 0.047273 second(s), 13 queries , MemCached On.

上网要文明 发言要理性

Powered by Discuz! © 2008-2023 YinHuaBBS.CN

快速回复 返回顶部 返回列表