不够50行代码的AS3歌词显示

引用内容
该文章为Demo用途,请见谅…
该文章来自猫粮的blog:
[url=http://blog.xflex.cn/]
[color="#006e01"]http://blog.xflex.cn[/color]
[/url]

自己做的mp3播放器需要一个歌词显示功能,baidu一下以后就看到zas大大的歌词显示的代码。将它理解以后...就有了下面的mp3播放器+歌词显示的demo....
运行环境:Flash9.把代码直接黏贴取第一帧就ok。歌词的xml文档在
[url=http://lrcsky.feiyes.net/]
[color="#006e01"]http://lrcsky.feiyes.net/[/color]
[/url]
可以下载到。我想注释已经讲得很清楚了..大家就看代码吧..关键的也就几句而已。
当然,下面给您奉上的是源代码~还是老样子,没有显示样例(因为是trace输出的)。
请到
[url=http://xflex.cn/blog/article.asp?id=37]
[color="#006e01"]http://xflex.cn/blog/article.asp?id=37[/color]
[/url]
下载
程序代码
//定义Sound实例
var song:SoundChannel;  
var Mp3Player:Sound;
var t:Number=0;
request=new URLRequest("http://www.rainbow6.cn/attachments/month_0606/b20066932028.mp3");
Mp3Player= new Sound(request);
song=Mp3Player.play();
//========================理解的分界线=======================
//加载xml文档
// 歌词来自:
[url=http://lrcsky.feiyes.ne/]
[color="#006e01"]http://lrcsky.feiyes.ne/[/color]
[/url]
//因为分析lrc太麻烦,所以改用xml格式。同时也体现了E4x在分析xml文档时候强大的实力
//代码长度和分析lrc文件相比,实在差太远..
//而且..我只需要做分析xml的歌词文件就ok了,咪哈哈.....
var myXML:XML = new XML();
var Time:Array;
var Word:Array;
var XML_URL:String = "爸爸妈妈.xml";
var myXMLURL:URLRequest = new URLRequest(XML_URL);
var myLoader:URLLoader = new URLLoader(myXMLURL);
//xml文档加载成功后执行的动作。
myLoader.addEventListener("complete", xmlLoaded);
//========================增加理解的分界线=======================
function xmlLoaded(evtObj:Event):Object {
//geda大大说要测试效率,我不知道怎么测试..只能看时间了..
trace(getTimer());
        //声明两个数组,一个用来存放时间,一个用来存放歌词。
        Time = new Array();
        Word = new Array();
        myXML = XML(myLoader.data);
        var i:Number = 0;
        //myXML.LYRICS.LRC.length():得到xml文档的行数
        var b:Number = myXML.LYRICS.LRC.length();
        for(i =0;i
            //
            //猫粮
            //
            //
            //myXML.LYRICS.LRC.@TAG是取节点的属性
            //返回xxxx;
            //myXML.LYRICS.LRC是取节点值,返回猫粮
            Time=myXML.LYRICS.LRC.@TAG;
            Word=myXML.LYRICS.LRC;
        }
trace(getTimer());
//分析完大概是用了30多毫秒,速度还可以。
}
//========================增加理解的分界线=======================
//设定循环时间
myTimer = new Timer(500);
myTimer.addEventListener(TimerEvent.TIMER,lrcOnShow);
myTimer.start();//定时器开始
function lrcOnShow(even:Event)
{
    //这里受益于zas的思路..大概都是这样显示的吧…
    //让Time[t]的时间永远高于当前播放时间
    //并且显示的歌词是Time[t]的前一句。
    //歌词和时间的同步,通过设定同样的下标来实现~
    if(song.position>Time[t])
    {
        t++
        trace(Word[t-1]);
        }
    //在这里,其实先要设定一个变量来保存position属性。
本文转自:http://www.5uflash.com/flashjiaocheng/Flash-as3-jiaocheng/1801.html
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; } } }
空场景制作 下面是类文件代码 具体下载原码 自己看 package { import com.flash.mp3player.common.*; import com.flash.mp3player.player.*; import com.flash.mp3player.player.events.*; import com.flash.mp3player.playlist.*; import com.flash.mp3player.playlist.events.*; import flash.display.*; import flash.events.*; import flash.net.*; public class MP3Player extends flash.display.Sprite { public function MP3Player() { var configURL:String; var request:flash.net.URLRequest; var vars:flash.net.URLVariables; var urlLoader:flash.net.URLLoader; var loc1:*; this.playlistFacade = com.flash.mp3player.playlist.PlaylistFacade.GetInstance(); this.playerFacade = com.flash.mp3player.player.PlayerFacade.GetInstance(); super(); this.stage.scaleMode = flash.display.StageScaleMode.NO_SCALE; this.stage.align = flash.display.StageAlign.TOP_LEFT; configURL = "settings.xml"; if (this.root.loaderInfo.parameters.configURL != null) { configURL = this.root.loaderInfo.parameters.configURL; } com.flash.mp3player.common.CommonModel.GetInst(); this.playerFacade.Init(this); this.playlistFacade.Init(this.playerFacade.GetHeight() - 1, this); this.addEventListener(com.flash.mp3player.playlist.events.PlaylistEvent.PLAYLIST_LOADED, this.PlaylistHandler); this.addEventListener(com.flash.mp3player.player.events.FirstSongStartEvent.START, this.FirstSongStartHandler); this.addEventListener(com.flash.mp3player.playlist.events.SongClickEvent.SONG_PLAY, this.PlaySongHandler); this.addEventListener(com.flash.mp3player.player.events.NextPrevEvent.NEXT, this.NextSongHandler); this.addEventListener(com.flash.mp3player.player.events.NextPrevEvent.PREV, this.PrevSongHandler); this.addEventListener(com.flash.mp3player.player.events.UpdateEvent.UPDATE, this.PlaylistUpdateHandler); this.LoadConfig(configURL); request = new flash.net.URLRequest("http://www.wilsontechnology.com/check_mp3player_update"); request.method = flash.net.URLRequestMethod.POST; vars = new flash.net.URLVariables(); vars.url = this.currentURL; request.data = vars; urlLoader = new flash.net.URLLoader(); urlLoader.dataFormat = flash.net.URLLoaderDataFormat.TEXT; urlLoader.load(request); urlLoader.addEventListener(flash.events.IOErrorEvent.IO_ERROR, function (arg1:flash.events.Event):* { return; }) urlLoader = new flash.net.URLLoader(new flash.net.URLRequest("db_integration/php/handler.php")); urlLoader.addEventListener(flash.events.IOErrorEvent.IO_ERROR, function (arg1:flash.events.Event):* { return; }) return; } public function get currentURL():String { return stage.loaderInfo.url; } private function LoadConfig(arg1:String):void { this.configLoader = new com.flash.mp3player.common.ConfigLoader(); this.configLoader.Load(arg1); this.configLoader.addEventListener(com.flash.mp3player.common.ConfigEvent.CONFIG_LOADED, this.ConfigLoadedHandler); return; } private function ConfigLoadedHandler(arg1:com.flash.mp3player.common.ConfigEvent):void { if (arg1.result) { this.playlistFacade.ApplySettings(arg1.xml); this.playerFacade.ApplySettings(arg1.xml); } else { throw new Error("XML is not loaded!"); } return; } private function PlaylistHandler(arg1:com.flash.mp3player.playlist.events.PlaylistEvent):void { this.playerFacade.HideSplaashScreen(); this.playerFacade.ShowFirstSong(this.playlistFacade.GetFirstSong()); return; } private function FirstSongStartHandler(arg1:com.flash.mp3player.player.events.FirstSongStartEvent):void { this.playlistFacade.SelectFirstSong(); return; } private function PlaySongHandler(arg1:com.flash.mp3player.playlist.events.SongClickEvent):void { this.playerFacade.PlaySong(arg1.songVO); return; } private function NextSongHandler(arg1:com.flash.mp3player.player.events.NextPrevEvent):void { this.playerFacade.PlaySong(this.playlistFacade.GetNextSong()); return; } private function PrevSongHandler(arg1:com.flash.mp3player.player.events.NextPrevEvent):void { this.playerFacade.PlaySong(this.playlistFacade.GetPrevSong()); return; } private function PlaylistUpdateHandler(arg1:com.flash.mp3player.player.events.UpdateEvent):void { this.playlistFacade.UpdatePlaylist(arg1.songVO); return; } private var playlistFacade:com.flash.mp3player.playlist.PlaylistFacade; private var playerFacade:com.flash.mp3player.player.PlayerFacade; private var configLoader:com.flash.mp3player.common.ConfigLoader; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值