歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中解析lrc歌詞

Android中解析lrc歌詞

日期:2017/3/1 10:17:35   编辑:Linux編程

Android中解析lrc歌詞

1.定義歌詞實體類

  1. import java.util.HashMap;
  2. import java.util.Map;
  3. /*
  4. * 用於封裝歌詞的類
  5. * @author
  6. *
  7. * */
  8. public class LrcInfo {
  9. private String title;//music title
  10. private String artist;//artist name
  11. private String album;//album name
  12. private String bySomeBody;//the lrc maker
  13. private String offset;//the time delay or bring forward
  14. private Map<Long,String> infos;//保存歌詞信息和時間點一一對應的Map
  15. public String getTitle() {
  16. return title;
  17. }
  18. public void setTitle(String title) {
  19. this.title = title;
  20. }
  21. public String getArtist() {
  22. return artist;
  23. }
  24. public void setArtist(String artist) {
  25. this.artist = artist;
  26. }
  27. public String getAlbum() {
  28. return album;
  29. }
  30. public void setAlbum(String album) {
  31. this.album = album;
  32. }
  33. public String getBySomeBody() {
  34. return bySomeBody;
  35. }
  36. public void setBySomeBody(String bySomeBody) {
  37. this.bySomeBody = bySomeBody;
  38. }
  39. public String getOffset() {
  40. return offset;
  41. }
  42. public void setOffset(String offset) {
  43. this.offset = offset;
  44. }
  45. public Map<Long, String> getInfos() {
  46. return infos;
  47. }
  48. public void setInfos(Map<Long, String> infos) {
  49. this.infos = infos;
  50. }
  51. }
2.定義解析類
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.util.Iterator;
  9. import java.util.Map;
  10. import java.util.Map.Entry;
  11. import java.util.TreeMap;
  12. import java.util.regex.Matcher;
  13. import java.util.regex.Pattern;
  14. import android.util.Log;
  15. /*
  16. * 此類用來解析LRC文件
  17. *
  18. */
  19. public class LrcParser {
  20. private LrcInfo lrcinfo= new LrcInfo();
  21. //存放臨時時間
  22. private long currentTime = 0 ;
  23. //存放臨時歌詞
  24. private String currentContent=null;
  25. //用於保存時間點和歌詞之間的對應關系
  26. //private Map<Long,String> maps =new HashMap<Long,String>();
  27. private Map<Long,String> maps=new TreeMap<Long,String>();
  28. /*
  29. * 根據文件路徑,讀取文件,返回一個輸入流
  30. * @param path 文件路徑
  31. * @return InputStream 文件輸入流
  32. * @throws FileNotFoundException
  33. * */
  34. private InputStream readLrcFile(String path) throws FileNotFoundException{
  35. File f= new File(path);
  36. InputStream ins = new FileInputStream(f);
  37. return ins;
  38. }
  39. public LrcInfo parser(String path)throws Exception{
  40. InputStream in = readLrcFile(path);
  41. lrcinfo = parser(in);
  42. return lrcinfo;
  43. }
  44. /**
  45. * @param inputStream 輸入流
  46. * @return
  47. *
  48. * */
  49. public LrcInfo parser(InputStream inputStream) throws IOException{
  50. //包裝對象
  51. InputStreamReader inr = new InputStreamReader(inputStream);
  52. BufferedReader reader =new BufferedReader(inr);
  53. //一行一行的讀,每讀一行解析一行
  54. String line =null;
  55. while((line=reader.readLine())!=null){
  56. parserLine(line);
  57. }
  58. //全部解析完後,設置info
  59. lrcinfo.setInfos(maps);
  60. Iterator<Entry<Long, String>> iter = maps.entrySet().iterator();
  61. while (iter.hasNext()) {
  62. Entry<Long,String> entry = (Entry<Long,String>)iter.next();
  63. Long key = entry.getKey();
  64. String val = entry.getValue();
  65. Log.e("---", "key="+key+" val="+val);
  66. }
  67. return lrcinfo;
  68. }
  69. /**
  70. * 利用正則表達式解析每行具體語句
  71. * 並將解析完的信息保存到LrcInfo對象中
  72. * @param line
  73. */
  74. private void parserLine(String line) {
  75. //獲取歌曲名信息
  76. if(line.startsWith("[ti:")){
  77. String title =line.substring(4,line.length()-1);
  78. Log.i("","title-->"+title);
  79. lrcinfo.setTitle(title);
  80. }
  81. //取得歌手信息
  82. else if(line.startsWith("[ar:")){
  83. String artist = line.substring(4, line.length()-1);
  84. Log.i("","artist-->"+artist);
  85. lrcinfo.setArtist(artist);
  86. }
  87. //取得專輯信息
  88. else if(line.startsWith("[al:")){
  89. String album =line.substring(4, line.length()-1);
  90. Log.i("","album-->"+album);
  91. lrcinfo.setAlbum(album);
  92. }
  93. //取得歌詞制作者
  94. else if(line.startsWith("[by:")){
  95. String bysomebody=line.substring(4, line.length()-1);
  96. Log.i("","by-->"+bysomebody);
  97. lrcinfo.setBySomeBody(bysomebody);
  98. }
  99. //通過正則表達式取得每句歌詞信息
  100. else{
  101. //設置正則表達式
  102. String reg ="\\[(\\d{1,2}:\\d{1,2}\\.\\d{1,2})\\]|\\[(\\d{1,2}:\\d{1,2})\\]";
  103. Pattern pattern = Pattern.compile(reg);
  104. Matcher matcher=pattern.matcher(line);
  105. //如果存在匹配項則執行如下操作
  106. while(matcher.find()){
  107. //得到匹配的內容
  108. String msg=matcher.group();
  109. //得到這個匹配項開始的索引
  110. int start = matcher.start();
  111. //得到這個匹配項結束的索引
  112. int end = matcher.end();
  113. //得到這個匹配項中的數組
  114. int groupCount = matcher.groupCount();
  115. for(int index =0;index<groupCount;index++){
  116. String timeStr = matcher.group(index);
  117. Log.i("","time["+index+"]="+timeStr);
  118. if(index==0){
  119. //將第二組中的內容設置為當前的一個時間點
  120. currentTime=str2Long(timeStr.substring(1, timeStr.length()-1));
  121. }
  122. }
  123. //得到時間點後的內容
  124. String[] content = pattern.split(line);
  125. //for(int index =0; index<content.length; index++){
  126. Log.i("","content="+content[content.length-1]);
  127. //if(index==content.length-1){
  128. //將內容設置魏當前內容
  129. currentContent = content[content.length-1];
  130. //}
  131. //}
  132. //設置時間點和內容的映射
  133. maps.put(currentTime, currentContent);
  134. Log.i("","currentTime--->"+currentTime+" currentContent--->"+currentContent);
  135. //遍歷map
  136. }
  137. }
  138. }
  139. private long str2Long(String timeStr){
  140. //將時間格式為xx:xx.xx,返回的long要求以毫秒為單位
  141. Log.i("","timeStr="+timeStr);
  142. String[] s = timeStr.split("\\:");
  143. int min = Integer.parseInt(s[0]);
  144. int sec=0;
  145. int mill=0;
  146. if(s[1].contains(".")){
  147. String[] ss=s[1].split("\\.");
  148. sec =Integer.parseInt(ss[0]);
  149. mill=Integer.parseInt(ss[1]);
  150. Log.i("","s[0]="+s[0]+"s[1]"+s[1]+"ss[0]="+ss[0]+"ss[1]="+ss[1]);
  151. }else{
  152. sec=Integer.parseInt(s[1]);
  153. Log.i("","s[0]="+s[0]+"s[1]"+s[1]);
  154. }
  155. return min*60*1000+sec*1000+mill*10;
  156. }
  157. }
3.解析調用
  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.util.Log;
  4. public class Lyric extends Activity {
  5. /** Called when the activity is first created. */
  6. @Override
  7. public void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.main);
  10. LrcParser lp =new LrcParser();
  11. String path="/sdcard/Hotel California";
  12. try{
  13. Log.i("____Lyric___", "--begin--");
  14. lp.parser(path);
  15. }catch(Exception e){
  16. }
  17. }
  18. }
Copyright © Linux教程網 All Rights Reserved