资源说明:package
{
import flash.display.Sprite;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
import flash.utils.ByteArray;
import flash.system.System;
import flash.net.*;
import flash.ui.*;
import flash.system.*;
public class Main extends Sprite
{
private var spectrum:Sprite;
private var sound:Sound;
private var schannel:SoundChannel;
private var lrcLoader:URLLoader;
private var info_txt:TextField;
private var id3_txt:TextField;
private var byteArray:ByteArray;
private var lrcArray:Array;
public function Main()
{
init();
}
////////////////////////////////////////////////////////////////////
////// 初始化 //////
//////////////////////////////////////////////////////////////////
private function init():void
{
System.useCodePage = true;
Security.allowDomain("*");
sound = new Sound ;
sound.load(new URLRequest("醉酒歌.mp3"));
schannel = sound.play();
//////////////////////////////////////////////////
lrcLoader = new URLLoader ;
lrcLoader.load(new URLRequest("《醉酒歌》.lrc"));
/////////////////////////////////////////////////;
info_txt = new TextField ;
info_txt.height = 20;
info_txt.y = 100;
info_txt.selectable = false;
info_txt.background = true;
info_txt.backgroundColor = 0xFF9900;
info_txt.defaultTextFormat = getFormat();
this.addChild(info_txt);
id3_txt = new TextField ;
id3_txt.width = 300;
id3_txt.selectable = false;
id3_txt.defaultTextFormat = getFormat();
this.addChild(id3_txt);
/////////////////////////////////////////////////;
spectrum = new Sprite ;
spectrum.x = 10;
spectrum.y = 250;
this.addChild(spectrum);
////////////////////////////////////////////////;
byteArray = new ByteArray ;
lrcArray = new Array ;
addEvents();
}
////////////////////////////////////////////////////////////////////
////// 设置文本格式 //////
//////////////////////////////////////////////////////////////////
private function getFormat():TextFormat
{
var textFmt:TextFormat = new TextFormat ;
textFmt.align = TextFieldAutoSize.LEFT;
textFmt.font = "Arial";
textFmt.color = 0x000000;
textFmt.size = 14;
return textFmt;
}
////////////////////////////////////////////////////////////////////
////// 添加事件 //////
//////////////////////////////////////////////////////////////////
private function addEvents():void
{
sound.addEventListener(Event.SOUND_COMPLETE,soundCompleteHandler);
sound.addEventListener(Event.ID3,id3InfoHandler);
lrcLoader.addEventListener(Event.COMPLETE,loadCompleteHandler);
stage.addEventListener(Event.ENTER_FRAME,soundPlayingHandler);
this.addEventListener(Event.ENTER_FRAME,creatSpectrum);
}
private function soundCompleteHandler(event:Event):void
{
stage.removeEventListener(Event.ENTER_FRAME,soundPlayingHandler);
this.removeEventListener(Event.ENTER_FRAME,creatSpectrum);
}
private function id3InfoHandler(event:Event):void
{
var tar:Sound = event.target as Sound;
id3_txt.text = "歌名:" + tar.id3.songName + "\n" + "歌手:" + tar.id3.artist + "\n" + "专辑:" + tar.id3.album;
}
////////////////////////////////////////////////////////////////////
////// 读取歌词信息 //////
//////////////////////////////////////////////////////////////////
private function loadCompleteHandler(event:Event):void
{
var lrclist:String = event.target.data;
var lrcArr:Array = lrclist.split("\n");
var reg:RegExp = /\[[0-5][0-9]:[0-5][0-9].[0-9][0-9]\]/g;
for (var i:int = 0; i < lrcArr.length; i++)
{
var lrcStr:String = lrcArr[i];
var len:int = lrcStr.match(reg).length;
var timeArr:Array = lrcStr.match(reg);
var lyrics:String = lrcStr.substr((len * 10));
for (var t:int = 0; t < timeArr.length; t++)
{
var timeS:String = timeArr[t];
var timeN:Number = (((Number(timeS.substr(1,2)) * 60) + Number(timeS.substr(4,5))) * 1000);
var object:Object = new Object ;
object.timer = timeN;
object.lrc = lyrics;
lrcArray.push(object);
}
}
lrcArray.sort(compare);
}
////////////////////////////////////////////////////////////////////;
////// 歌词歌曲同步 //////
///////////////////////////////////////////////////////////////////
private function soundPlayingHandler(event:Event):void
{
for (var i:int = 1; i < lrcArray.length; i++)
{
if (schannel.position < lrcArray[i].timer)
{
info_txt.text = "歌词:" + lrcArray[i - 1].lrc;
break;
}
info_txt.text = "end:" + lrcArray[lrcArray.length - 1].lrc;
}
info_txt.width = info_txt.textWidth + 5;
}
////////////////////////////////////////////////////////////////////
////// 创建频谱 //////
///////////////////////////////////////////////////////////////////
private function creatSpectrum(event:Event):void
{
SoundMixer.computeSpectrum(byteArray,true);
spectrum.graphics.clear();
spectrum.graphics.lineStyle(0,0x666666);
spectrum.graphics.beginFill(0x666666);
spectrum.graphics.moveTo(0,0);
for (var i:int = 0; i < 256; i += 5)
{
var n:Number = byteArray.readFloat() * 100;
spectrum.graphics.drawRect(i,0,3, - n);
}
}
////////////////////////////////////////////////////////////////////;
////// 比较函数 //////
///////////////////////////////////////////////////////////////////
private function compare(pareA:Object,pareB:Object):int
{
if (pareA.timer > pareB.timer)
{
return 1;
}
if (pareA.timer < pareB.timer)
{
return -1;
}
return 0;
}
}
}
本源码包内暂不包含可直接显示的源代码文件,请下载源码包。