Android 歌词解析(完美解析)

最近要写个音乐播放器,需要解析Lrc文件。搜索了下网上的代码大都是有问题的,很多连歌词规则都没搞懂就写了个代码传到网上,有点害人的感觉。

实在受不了解析出来乱七八糟的歌词,只能自己动手做解析,修改了别人解析部分的代码。

下面是实现部分

 

解析类

  1. package com.ysh.suplay;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileNotFoundException;  
  7. import java.io.IOException;  
  8. import java.io.InputStreamReader;  
  9. import java.util.ArrayList;  
  10. import java.util.List;  
  11.   
  12. /** 
  13.  * 解析歌词类 
  14.  */  
  15. public class LrcProcess {  
  16.   
  17.     private List<LrcContent> LrcList;  
  18.   
  19.     private LrcContent mLrcContent;  
  20.   
  21.     public LrcProcess() {  
  22.   
  23.         mLrcContent = new LrcContent();  
  24.         LrcList = new ArrayList<LrcContent>();  
  25.     }  
  26.   
  27.     /** 
  28.      * 解析歌词 
  29.      */  
  30.     public String readLRC(String song_path) {  
  31.         // public void Read(String file){  
  32.   
  33.         StringBuilder stringBuilder = new StringBuilder();  
  34.   
  35.         File f = new File(song_path.substring(0, song_path.lastIndexOf("."))  
  36.                 + ".lrc");  
  37.   
  38.         try {  
  39.             FileInputStream fis = new FileInputStream(f);  
  40.             InputStreamReader isr = new InputStreamReader(fis, "GB2312");  
  41.             BufferedReader br = new BufferedReader(isr);  
  42.             String s = "";  
  43.             while ((s = br.readLine()) != null) {  
  44.                 s += " ";//处理只有时间没有内容的行  
  45.                 if ((s.indexOf("[ar:") != -1) || (s.indexOf("[ti:") != -1)  
  46.                         || (s.indexOf("[by:") != -1)  
  47.                         || (s.indexOf("[al:") != -1) || s.equals("")) {  
  48.                     continue;  
  49.                 }  
  50.                   
  51.                 s = s.replace("[""");  
  52.                   
  53.                 //关键代码,歌词用的时候需要对时间进行排序  
  54.                 String splitLrc_data[] = s.split("]");  
  55.                 for (int i = 0; i < splitLrc_data.length - 1; i++) {  
  56.                     //System.out.println(splitLrc_data[i]);  
  57.                     mLrcContent.setLrc_time(TimeStr(splitLrc_data[i]));  
  58.                     mLrcContent.setLrc(splitLrc_data[splitLrc_data.length - 1]);  
  59.                     LrcList.add(mLrcContent);  
  60.                     mLrcContent = new LrcContent();  
  61.                 }  
  62.                 //关键代码  
  63.   
  64.             }  
  65.             br.close();  
  66.             isr.close();  
  67.             fis.close();  
  68.         } catch (FileNotFoundException e) {  
  69.             // TODO Auto-generated catch block  
  70.             e.printStackTrace();  
  71.   
  72.             stringBuilder.append("没有找到歌词文件!");  
  73.         } catch (IOException e) {  
  74.             // TODO Auto-generated catch block  
  75.             e.printStackTrace();  
  76.             stringBuilder.append("无法读取歌词文件!");  
  77.         }  
  78.         return stringBuilder.toString();  
  79.     }  
  80.   
  81.     /** 
  82.      * 处理时间 
  83.      * 时间转换为毫秒millisecond 
  84.      */  
  85.     public int TimeStr(String timeStr) {  
  86.   
  87.         timeStr = timeStr.replace(":"".");  
  88.         timeStr = timeStr.replace(".""@");  
  89.         String timeData[] = timeStr.split("@");  
  90.         int currentTime = 0;  
  91.         // 分离出分、秒并转换为整型  
  92.         try {  
  93.             int minute = Integer.parseInt(timeData[0]);  
  94.             int second = Integer.parseInt(timeData[1]);  
  95.             int millisecond = Integer.parseInt(timeData[2]);  
  96.             currentTime = (minute * 60 + second) * 1000 + millisecond * 10;  
  97.         } catch (Exception ex) {  
  98.             ex.printStackTrace();  
  99.         }  
  100.         return currentTime;  
  101.     }  
  102.   
  103.     public List<LrcContent> getLrcContent() {  
  104.   
  105.         return LrcList;  
  106.     }  
  107.   
  108.     /** 
  109.      * 歌词类  
  110.      * 需要排序的话,要用Integer替代int 
  111.      */  
  112.     public class LrcContent {  
  113.         private String Lrc;  
  114.         private Integer Lrc_time;  
  115.   
  116.         public String getLrc() {  
  117.             return Lrc;  
  118.         }  
  119.   
  120.         public void setLrc(String lrc) {  
  121.             Lrc = lrc;  
  122.         }  
  123.   
  124.         public Integer getLrc_time() {  
  125.             return Lrc_time;  
  126.         }  
  127.   
  128.         public void setLrc_time(int lrc_time) {  
  129.             Lrc_time = lrc_time;  
  130.         }  
  131.     }  
  132.   
  133. }  

 

对时间进行排序

  1. Collections.sort(lrcList, new Comparator<LrcContent>() {  
  2.   
  3.     @Override  
  4.     public int compare(LrcContent o1, LrcContent o2) {  
  5.         TODO Auto-generated method stub  
  6.         return o1.getLrc_time().compareTo(o2.getLrc_time());  
  7.     }  
  8.           
  9. });  
  10. for (LrcContent lrcContent : lrcList) {  
  11.     System.out.println(toTime(lrcContent.getLrc_time()));  
  12.     System.out.println(lrcContent.getLrc());  
原文地址:https://www.cnblogs.com/fx2008/p/3131579.html