Flash AS3用于读取LRC同步歌词的类

http://www.itjxue.com  2015-07-17 23:39  来源:未知  点击次数: 

今天心情不错,用FLASH写了一个用于读取LRC同步歌词的类,只要25行代码,完全用是用FLASH的字符串操作来分析标签的。

下面就是这个类:

package LRC{
         public class readlrc {
         public function readlrcastime(mp3playtime:Number,gc:String):String {
                var qsd:int;//起始点
                var fhz:String;
                 for (var j:int=0; j < gc.length; j++) {
                              if (gc.charCodeAt(j) == 58) {
                                       if (int(gc.slice(j - 2,j)) * 60 + int(gc.slice(j + 1,j + 3)) == mp3playtime) {
                                                 var i:int=0;
                                                do {
                                                        i++;
                                                       if (gc.charCodeAt(j + i) == 93) {
                                                        qsd=j + i;
                                                           }
                                                        if (gc.charCodeAt(j + i) == 13) {
                                                                    fhz=gc.slice(qsd + 1,j + i);
                                                         }
                                               } while (gc.charCodeAt(j + i) != 13&&j+i<gc.length);
                                        }
                               }
                        }
                         return fhz;
                 }
           }
}

将此文件保存为readlrc.as。

注意:一定要把此AS文件放入一个名为LRC的文件夹中,才可以使用。

下面就用一个小示例来说明如何用这个类,新建一个AS文件,写入以下代码:

package {
             import flash.display.MovieClip;
              import flash.events.Event;
            import flash.net.URLLoader;
              import flash.net.URLRequest;
              import flash.media.Sound;
             import flash.media.SoundChannel;
             import flash.system.System;
               import flash.text.TextField;
              import LRC.readlrc;
               public class mp3 extends MovieClip {
                         private var lrcreq:URLRequest=new URLRequest("日不落.lrc");
                          private var mp3url:URLRequest=new URLRequest("日不落.mp3");
                           private var mymp3:Sound=new Sound();
                           private var channel:SoundChannel=new SoundChannel();
                          private var lrcurl:URLLoader=new URLLoader();
                           private var tbgc:String;
                          private var mylrc:readlrc=new readlrc();
                         System.useCodePage=true;
                         public function mp3() {
                                  mymp3.load(mp3url);
                                  channel=mymp3.play();
                                  lrcurl.load(lrcreq);
                                    lrcurl.addEventListener(Event.COMPLETE,loaded);
                            }
                           private function enterframe(event:Event) {
                                     try {
                                             gc_txt.text=mylrc.readlrcastime(int(channel.position/1000),tbgc);
                                      } catch (error:Error) {
                                             //
                                       }
                             }
                              private function loaded(event:Event) {
                                            tbgc=String(event.target.data);
                                            addEventListener(Event.ENTER_FRAME,enterframe);
                              }
                  }
}

保存此文件为mp3.as。

写完后,我们还需要一个FLA文件来编译这些代码,新建一个FLA文件,名字随意,在场景中拖出一个动态文本域,实例名称填写gc_txt,在FLA文件的文档类中填写mp3,将此FLA文件与mp3.as文件,日不落.mp3文件,日不落.lrc文件和名为LRC的文件夹放入同一个目录下,测试影片,就可以看到效果了。

(责任编辑:IT教学网)

更多

推荐Flash actionscript文章