音乐播放器实现歌词同步

项目中音乐播放器实现歌词同步思路:
1.读取LRC歌词文件信息
2.将歌词时间段和对应时间段歌词保存至两个数组中,
3.按时间顺序对两个数组进行排序
核心代码如下:
public class SongWord{

 public String[] time;

 public String[] songWord;
 
 public long[] songTime;

 private UIController m_uicontroller;

 private static SongWord SONGPLAYER = null;
 
 private Displayable dis;
 
 public Displayable getDis() {
  return dis;
 }

 public void setDis(Displayable dis) {
  this.dis = dis;
 }

 public static SongWord getInstance(UIController uicontroller) {
  if (SONGPLAYER == null) {
   SONGPLAYER = new SongWord(uicontroller);
  }
  return SONGPLAYER;
 }

 private SongWord(UIController uicontroller) {
  this.m_uicontroller = uicontroller;
 }

 public void readFiles(String url) {

  String musicUrl = url;
  String temp = musicUrl.substring(0, musicUrl.length() - 3);
  String fileUrl = temp + "lrc";

  Vector vc = new Vector();
  try {
   FileConnection fc = (FileConnection) Connector.open("file://"
     + fileUrl);
   InputStream in = fc.openInputStream();

   StringBuffer buffer = new StringBuffer();

   int ch;
   while ((ch = in.read()) != -1) {
    if (ch != '\n') {// && ch != '\r'
     buffer.append((char) ch);
    } else {
     String line = new String(buffer.toString().getBytes(),"gbk");

     if (line.charAt(0) == '[' && line.charAt(1) == '0') {
      vc.addElement(line);
     }
     buffer = new StringBuffer();
    }
   }
   
  } catch (Exception e) {
   m_uicontroller.setCurrent(
     new MessageAlert("警告", "读文件出错!", dis,m_uicontroller));e.printStackTrace();
  }

  outputVector(vc);
 }

 public void outputVector(Vector vc) {

  int length = 0;
  for (int i = 0; i < vc.size(); i++) {
   String line = (String) vc.elementAt(i);
   int count = getCount(line);
   length = length + count;
  }

  songWord = new String[length];
  songTime = new long[length];

  int k = 0;
  for (int i = 0; i < vc.size(); i++) {
   String line = (String) vc.elementAt(i);
   int count = getCount(line);
   if (count == 1) {
    songTime[k] = getTime(line.substring(1, 9));
    songWord[k] = line.substring(10);
   } else if (count == 2) {
    songTime[k] = getTime(line.substring(1, 9));
    songWord[k] = line.substring(20);
    k = k + 1;
    songTime[k] = getTime(line.substring(11, 19));
    songWord[k] = line.substring(20);
   } else if (count == 3) {
    songTime[k] = getTime(line.substring(1, 9));
    songWord[k] = line.substring(30);
    k = k + 1;
    songTime[k] = getTime(line.substring(11, 19));
    songWord[k] = line.substring(30);
    k = k + 1;
    songTime[k] = getTime(line.substring(21, 29));
    songWord[k] = line.substring(30);
   } else if (count == 4) {
    songTime[k] = getTime(line.substring(1, 9));
    songWord[k] = line.substring(40);
    k = k + 1;
    songTime[k] = getTime(line.substring(11, 19));
    songWord[k] = line.substring(40);
    k = k + 1;
    songTime[k] = getTime(line.substring(21, 29));
    songWord[k] = line.substring(40);
    k = k + 1;
    songTime[k] = getTime(line.substring(31, 39));
    songWord[k] = line.substring(40);
   }
   k++;
  }

  //putInfo(songTime, songWord);

  // 排序
  sort(songTime, songWord);
  
  //putInfo(songTime, songWord);

 }

 public void putInfo(long[] songTime,String[] songWorld) {
  for (int i = 0; i < songTime.length; i++) {
   System.out.println("songTime[" + i + "]" + songTime[i]);
   System.out.println("songWord[" + i + "]" + songWord[i]);
  }
 }

 // 排序
 public void sort(long[] values, String[] songWord) {
  long temp;
  String tempWord;
  for (int i = 0; i < values.length; ++i) {
   for (int j = 0; j < values.length - i - 1; ++j) {
    if (values[j] > values[j + 1]) {
     temp = values[j];
     tempWord = songWord[j];
     values[j] = values[j + 1];
     songWord[j] = songWord[j + 1];
     values[j + 1] = temp;
     songWord[j + 1] = tempWord;
    }
   }
  }
 }

 // 将时间转化为微秒作为数组返回
 public long getTime(String line) {
  String m = line.substring(0, 2);
  String s = line.substring(3, 5);

  long secTime = Integer.parseInt(m) * 60 + Integer.parseInt(s);
  long nTime = secTime * 1000000;
  return nTime;
 }

 public int getCount(String line) {
  int count = 0;
  int length = line.length();
  for (int i = 0; i < length; i++) {
   if (line.charAt(i) == '[') {
    count++;
   }
  }
  return count;
 }
}

 

你可能感兴趣的:(技术学习(j2me))