WEB开发网
开发学院图形图像Flash Flash AS制作LRC歌词同步的详细教程 阅读

Flash AS制作LRC歌词同步的详细教程

 2008-06-05 11:22:38 来源:WEB开发网   
核心提示: CODE:var lrc_txt:TextField=new TextField();var LRCarray:Array=new Array();var sc:SoundChannel;public function LRCPlayer() {lrc_txt.width=500;lrc_

CODE:

 var lrc_txt:TextField=new TextField();
 var LRCarray:Array=new Array();
 var sc:SoundChannel;
 public function LRCPlayer() {
 lrc_txt.width=500;
 lrc_txt.selectable=false;
 addChild(lrc_txt);
 //歌词在文本 lrc_txt 中显示
 var loader:URLLoader=new URLLoader();
 loader.load(new URLRequest("LRC/青花瓷.lrc"));
 loader.addEventListener(Event.COMPLETE,LoadFinish);
 var sound:Sound=new Sound();
 sound.load(new URLRequest("Music/青花瓷.mp3"));
 sc=sound.play();
 //播放声音,并生成 sc 变量,SoundChannel 类的实例
 stage.addEventListener(Event.ENTER_FRAME,SoundPlaying);
 //实时刷新歌词
 }
 function SoundPlaying(evt:Event):void {
 for (var i=1; i  if (sc.position  lrc_txt.text=LRCarray[i-1].lyric;
  break;
  //找到歌词,跳出循环体
  }
  lrc_txt.text=LRCarray[LRCarray.length-1].lyric;
  //找不到歌词,说明已超出了最后一句的时间,因此显示最后一句歌词
 }
 }

五、全部代码(文档类 LRCPlayer.as):

CODE:

package {
import flash.display.Sprite;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.Event;
import flash.text.TextField;
import flash.system.System;
public class LRCPlayer extends Sprite {
 var lrc_txt:TextField=new TextField();
 var LRCarray:Array=new Array();
 var sc:SoundChannel;
 public function LRCPlayer() {
 System.useCodePage=true;
 lrc_txt.width=500;
 lrc_txt.selectable=false;
 addChild(lrc_txt);
 var loader:URLLoader=new URLLoader();
 loader.load(new URLRequest("LRC/青花瓷.lrc"));
 loader.addEventListener(Event.COMPLETE,LoadFinish);
 var sound:Sound=new Sound();
 sound.load(new URLRequest("Music/青花瓷.mp3"));
 sc=sound.play();
 stage.addEventListener(Event.ENTER_FRAME,SoundPlaying);
 }
 function SoundPlaying(evt:Event):void {
 for (var i=1; i  if (sc.position  lrc_txt.text=LRCarray[i-1].lyric;
  break;
  }
  lrc_txt.text=LRCarray[LRCarray.length-1].lyric;
 }
 }
 function LoadFinish(evt:Event):void {
 var list:String=evt.target.data;
 var listarray:Array=list.split("
");
 var reg:RegExp=/[[0-5][0-9]:[0-5][0-9].[0-9][0-9]]/g;
 for (var i=0; i  var info:String=listarray[i];
  var len:int=info.match(reg).length;
  var timeAry:Array=info.match(reg);
  var lyric:String=info.substr(len*10);
  for (var k:int=0; k  var obj:Object=new Object();
  var ctime:String=timeAry[k];
  var ntime:Number=Number(ctime.substr(1,2))*60+Number(ctime.substr(4,5));
  obj.timer=ntime*1000;
  obj.lyric=lyric;
  LRCarray.push(obj);
  }
 }
 LRCarray.sort(compare);
 }
 private function compare(paraA:Object,paraB:Object):int {
 if (paraA.timer>paraB.timer) {
  return 1;
 }
 if (paraA.timer  return -1;
 }
 return 0;
 }
}
}

上一页  1 2 3 4 5 6 7  下一页

Tags:Flash AS 制作

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接