这次的内容是歌词处理模块LyricAdapter类。这个类的主要功能有
1.歌词文件的解析
2.对外提供歌词访问服务(歌词数取得,歌词内容,时间的取得等)
3.根据播放位置检索对应的歌词。
4.在歌词文件取得后和当前歌词变化以后通过登录的LyricListener进行通知。
先看看LyricAdapter类在整个软件中的位置。
从图中可以看出,LyricAdapter类和SafeTimer类一样,归LyricPlayerService管理,并位置提供服务。
接下来在说明LyricAdapter的功能之前,先让我们看看我们的处理对象,歌词文件的内容。打开一个歌词文件(*.lrc)可以看到以下内容。
- [ti:δ֪]
- [ar:]
- [al:Family Album, U.S.A.]
- [by:SPJ]
- [00:00.97]EPISODE 12 You're Tops
- [00:06.20]ACT II
- [00:10.52]Sam, would you come in, please?
- [00:16.62]You sound like something's bothering you, Susan.
- [00:19.15]The sketches for the cover of the new doll book?
- [00:21.74]That's not it.
- [00:23.35]Please sit down.
- [00:24.56]Sure.
- [00:30.31]I need your advice on a personsal matter,
- [00:32.53]but it's not about me.
- [00:35.12]You need my advice on a personal matter,
- [00:36.89]and it's not about you. OK.
- [00:40.01]It's about my grandfather.
- [00:42.62]What's the problem?
- [00:45.17]It won't sound like a big deal,
除了前面的几个特殊的ti,ar,al,by等关键字以外的每一句歌词都是有包含在中括号中的时间和后面的歌词组成的。歌词处理模块的功能就是解析歌词文件并按照歌曲播放的时间选择合适的歌词表示就可以了。
下面来看一看今天的主角和配角吧。
LyricAdapter类主要提供以下功能
- 解析歌词文件并管理得到的信息。
- 提供访问歌词的接口(歌词语句数,取得特定歌词信息等)
- 根据提供的时间选择合适的歌词并将结果通知给LyricPlayerServie类。
SafetyTimer类主要提供以下功能
- 负责定时启动从MediaPlayer取得播放的当前时间并传达给LyricAdapter
LyricPlayerService类主要提供以下功能
- 负责控制LyricAdapter,SafetyTimer的创建
- 建立LyricPlayerService,LyricAdapter,SafetyTimer之间的联系。
- 控制LyricAdapter,SafetyTimer的动作
- 处理传出的LyricAdapter通知
以下是时序图
在这个时序图中,为了说明LyricAdapter的功能,我们省略了许多细节。下面是时序图的说明。
1.创建对象并建立联系
1-1 LyricPlayerService创建MediaPlayer对象
1-2 LyricPlayerService创建LyricAdapter对象
1-3 LyricPlayerService创建SafetyTimer.OnTimerListener的匿名内嵌派生类
1-4 LyricPlayerService创建SafetyTimer并指定前一步创建的SafetyTimer.OnTimerListener
1-5 LyricPlayerService将自己指定成LyricAdapter的listener.
2.解析歌词
2-1 LyricPlayerService调用LyricAdapter.LoadLyric方法,参数为字幕文件的文件路径+文件名。
2 -2 LyricAdapter在LoadLyric的最后会调用LyricPlayerService.onLyricLoaded方法进行通知。
3.启动Timer并处理播放时间通知
3-1 在启动播放器的同时启动SafetyTimer
3-2 当定时时间到了已有,SafetyTimer会调用在1-3中创建的OnTimeListener对象的OnTimer方法。
3-3 在OnTimer方法中,OnTimeListener首先从MediaPlayer取得现在的播放位置,然后调用LyricAdapter的notifyTime方法将位置传递个LyricAdapter。
3-4 LyricAdapter根据播放位置取得当前的歌词并通知LyricPlayerService进行相应的处理。
需要补充一点,在这里我们定义了一个LyricListener接口实现了,既可以将消息通知给LyricPlayerService有避免了LyricAdapter对LyricPlayerService的依赖关系。这一点和
Andorid歌词秀设计思路(1)SafetyTimer中用到的是一样的方法。
以下是LyricAdapter的代码,请参考。很简单的。
- package LyricPlayer.xwg;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import android.util.Log;
- public class LyricAdapter{
- private ArrayList<LyricLine> mLyricLines= null;
- private LyricListener mListener = null; //歌词载入,变化Listener
- private int mCurrentLyric = 0; //当前的歌词
- private int mLyricOffset = -300; //为了解决播放滞后的问题设置的调整时间,单位是毫秒
- private static final String TAG = new String("LyricAdapter");
- //用于向外通知歌词载入,变化的Listener
- public interface LyricListener{
- public void onLyricChanged(int lyric_index);
- public void onLyricLoaded();
- }
- //歌词信息
- private class LyricLine{
- long mLyricTime; //in milliseconds
- String mLyricText;
- LyricLine(long time, String lyric){
- mLyricTime = time;
- mLyricText = lyric;
- }
- }
- //将歌词的时间字符串转化成毫秒数的Utility类,如果参数是00:01:23.45
- private static class TimeParser{
- //@return value in milliseconds.
- static long parse(String strTime){
- String beforeDot = new String("00:00:00");
- String afterDot = new String("0");
- //将字符串按小数点拆分成整秒部分和小数部分。
- int dotIndex = strTime.indexOf(".");
- if(dotIndex < 0){
- beforeDot = strTime;
- }else if(dotIndex == 0){
- afterDot = strTime.substring(1);
- }else{
- beforeDot = strTime.substring(0, dotIndex);//00:01:23
- afterDot = strTime.substring(dotIndex + 1); //45
- }
- long intSeconds = 0;
- int counter = 0;
- while(beforeDot.length() > 0){
- int colonPos = beforeDot.indexOf(":");
- try{
- if(colonPos > 0){//找到冒号了。
- intSeconds *= 60;
- intSeconds += new Integer(beforeDot.substring(0, colonPos));
- beforeDot = beforeDot.substring(colonPos + 1);
- }else if(colonPos < 0){//没找到,剩下都当一个数处理了。
- intSeconds *= 60;
- intSeconds += new Integer(beforeDot);
- beforeDot = "";
- }else{//第一个就是冒号,不可能!
- return -1;
- }
- }catch(NumberFormatException e){
- return -1;
- }
- ++counter;
- if(counter > 3){//不会超过小时,分,秒吧。
- return -1;
- }
- }
- //intSeconds=83
- String totalTime = String.format("%d.%s", intSeconds, afterDot);//totaoTimer = "83.45"
- Double doubleSeconds = new Double(totalTime); //转成小数83.25
- return (long)(doubleSeconds * 1000);//转成毫秒8345
- }
- }
- //歌词读入
- public void LoadLyric(String path){
- mLyricLines= new ArrayList<LyricLine>();
- mLyricLines.clear();
- mCurrentLyric = -1;
- try {
- FileReader fr = new FileReader(path);
- BufferedReader br = new BufferedReader (fr);
- String line;
- while ((line = br.readLine())!=null){//读出一行
- int timeEndIndex = line.lastIndexOf("]");//找到歌词时间的最后位置
- if(timeEndIndex >= 3){//最起码[1]程度应该有吧。
- String lyricText = new String();
- //先取出歌词
- if(timeEndIndex < (line.length() - 1)){
- lyricText = line.substring(timeEndIndex + 1, line.length());
- }
- //处理重复的歌词,如下面的例子
- //[时间1][时间2][时间3][时间4]歌词
- int timeSegmentEnd = timeEndIndex;
- while(timeSegmentEnd > 0){
- timeEndIndex = line.lastIndexOf("]", timeSegmentEnd);
- if(timeEndIndex < 1) break; //没找到"]",算了
- int timeStartIndex = line.lastIndexOf("[", timeEndIndex - 1);
- if(timeStartIndex < 0) break; //没找到"[",算了
- //"["和"]"都找到了,取出时间字符串
- long lyricTime = TimeParser.parse(line.substring(timeStartIndex + 1, timeEndIndex));
- if(lyricTime >= 0){
- lyricTime += mLyricOffset;//调整时间
- if(lyricTime < 0){
- lyricTime = 0;//也别太小了。
- }
- //行了,保存一句。
- mLyricLines.add(new LyricLine(lyricTime, lyricText));
- }
- timeSegmentEnd = timeStartIndex;
- }
- }
- }
- //按时间排序
- Collections.sort(mLyricLines, new Comparator<LyricLine>(){
- //内嵌,匿名的compare类
- public int compare(LyricLine object1, LyricLine object2){
- if(object1.mLyricTime > object2.mLyricTime){
- return 1;
- } else if(object1.mLyricTime < object2.mLyricTime){
- return -1;
- }else{
- return 0;
- }
- }
- });
- fr.close();
- } catch (FileNotFoundException e) {
- mLyricLines = null;
- e.printStackTrace();
- } catch (IOException e) {
- mLyricLines = null;
- e.printStackTrace();
- }
- if(mListener != null){
- //如果有人想知道,告诉一声,歌词已经读进来了。
- mListener.onLyricLoaded();
- }
- }
- public int getLyricCount(){
- if(mLyricLines != null){
- return mLyricLines.size();
- }else{
- return 0;
- }
- }
- public String getLyric(int index){
- if(mLyricLines != null){
- if(index >= 0 && index < mLyricLines.size()){
- return mLyricLines.get(index).mLyricText;
- }else{
- return null;
- }
- }else{
- return null;
- }
- }
- public long getLyricTime(int index){
- if(mLyricLines != null){
- if(index >= 0 && index < mLyricLines.size()){
- return mLyricLines.get(index).mLyricTime;
- }else{
- return -1;
- }
- }else{
- return -1;
- }
- }
- public int getCurrentLyric(){
- return mCurrentLyric;
- }
- public void setListener(LyricListener listener){
- mListener = listener;
- }
- //由利用者调用,用来通知现在的播放时间,一边找到合适的歌词。
- public void notifyTime(long millisecond){
- if (mLyricLines != null){
- int newLyric = seekLyric(millisecond);
- Log.i(TAG, "newLyric = " + newLyric);
- if(newLyric != -1 && newLyric != mCurrentLyric){//如果找到的歌词和现在的不是一句。
- if(mListener != null){
- //告诉一声,歌词已经编程另外一句啦!
- mListener.onLyricChanged(newLyric);
- }
- mCurrentLyric = newLyric;
- }
- }
- }
- private int seekLyric(long millisecond){
- int findStart = 0;
- if(mCurrentLyric >= 0){
- //如果已经指定当前字幕,则现在位置开始
- findStart = mCurrentLyric;
- }
- long lyricTime = mLyricLines.get(findStart).mLyricTime;
- if(millisecond > lyricTime){ //如果想要查找的时间在现在字幕的时间之后
- //如果开始位置经是最后一句了,直接返回最后一句。
- if(findStart == (mLyricLines.size() - 1)) return findStart;
- int new_index = findStart + 1;
- //找到第一句开始时间大于输入时间的歌词
- while(new_index < mLyricLines.size() && mLyricLines.get(new_index).mLyricTime <= millisecond){
- ++new_index;
- }
- //这句歌词的前一句就是我们要找的了。
- return new_index - 1;
- }else if(millisecond < lyricTime){ //如果想要查找的时间在现在字幕的时间之前
- //如果开始位置经是第一句了,直接返回第一句。
- if(findStart == 0) return 0;
- int new_index = findStart - 1;
- //找到开始时间小于输入时间的歌词
- while(new_index > 0 && mLyricLines.get(new_index).mLyricTime > millisecond){
- --new_index;
- }
- //就是它了。
- return new_index;
- }else{
- //不用找了
- return findStart;
- }
- }
- }