Android 歌词解析(完美解析)
最近要写个音乐播放器,需要解析Lrc文件。搜索了下网上的代码大都是有问题的,很多连歌词规则都没搞懂就写了个代码传到网上,有点害人的感觉。
实在受不了解析出来乱七八糟的歌词,只能自己动手做解析,修改了别人解析部分的代码。
下面是实现部分
解析类
- package com.ysh.suplay;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * 解析歌词类
- */
- public class LrcProcess {
- private List<LrcContent> LrcList;
- private LrcContent mLrcContent;
- public LrcProcess() {
- mLrcContent = new LrcContent();
- LrcList = new ArrayList<LrcContent>();
- }
- /**
- * 解析歌词
- */
- public String readLRC(String song_path) {
- // public void Read(String file){
- StringBuilder stringBuilder = new StringBuilder();
- File f = new File(song_path.substring(0, song_path.lastIndexOf("."))
- + ".lrc");
- try {
- FileInputStream fis = new FileInputStream(f);
- InputStreamReader isr = new InputStreamReader(fis, "GB2312");
- BufferedReader br = new BufferedReader(isr);
- String s = "";
- while ((s = br.readLine()) != null) {
- s += " ";//处理只有时间没有内容的行
- if ((s.indexOf("[ar:") != -1) || (s.indexOf("[ti:") != -1)
- || (s.indexOf("[by:") != -1)
- || (s.indexOf("[al:") != -1) || s.equals("")) {
- continue;
- }
- s = s.replace("[", "");
- //关键代码,歌词用的时候需要对时间进行排序
- String splitLrc_data[] = s.split("]");
- for (int i = 0; i < splitLrc_data.length - 1; i++) {
- //System.out.println(splitLrc_data[i]);
- mLrcContent.setLrc_time(TimeStr(splitLrc_data[i]));
- mLrcContent.setLrc(splitLrc_data[splitLrc_data.length - 1]);
- LrcList.add(mLrcContent);
- mLrcContent = new LrcContent();
- }
- //关键代码
- }
- br.close();
- isr.close();
- fis.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- stringBuilder.append("没有找到歌词文件!");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- stringBuilder.append("无法读取歌词文件!");
- }
- return stringBuilder.toString();
- }
- /**
- * 处理时间
- * 时间转换为毫秒millisecond
- */
- public int TimeStr(String timeStr) {
- timeStr = timeStr.replace(":", ".");
- timeStr = timeStr.replace(".", "@");
- String timeData[] = timeStr.split("@");
- int currentTime = 0;
- // 分离出分、秒并转换为整型
- try {
- int minute = Integer.parseInt(timeData[0]);
- int second = Integer.parseInt(timeData[1]);
- int millisecond = Integer.parseInt(timeData[2]);
- currentTime = (minute * 60 + second) * 1000 + millisecond * 10;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return currentTime;
- }
- public List<LrcContent> getLrcContent() {
- return LrcList;
- }
- /**
- * 歌词类
- * 需要排序的话,要用Integer替代int
- */
- public class LrcContent {
- private String Lrc;
- private Integer Lrc_time;
- public String getLrc() {
- return Lrc;
- }
- public void setLrc(String lrc) {
- Lrc = lrc;
- }
- public Integer getLrc_time() {
- return Lrc_time;
- }
- public void setLrc_time(int lrc_time) {
- Lrc_time = lrc_time;
- }
- }
- }
对时间进行排序
- Collections.sort(lrcList, new Comparator<LrcContent>() {
- @Override
- public int compare(LrcContent o1, LrcContent o2) {
- TODO Auto-generated method stub
- return o1.getLrc_time().compareTo(o2.getLrc_time());
- }
- });
- for (LrcContent lrcContent : lrcList) {
- System.out.println(toTime(lrcContent.getLrc_time()));
- System.out.println(lrcContent.getLrc());
- }
虽功未成,亦未敢藏私,众侠诸神通尽录于此,竟成一笈,名葵花宝典,以飨后世。
邮箱:steven9801@163.com
QQ: 48039387
邮箱:steven9801@163.com
QQ: 48039387