解析LRC歌词文件readlrc

时间:2022-09-25 21:56:08
 package com.jikexueyuan.readlrc.main;

 import com.jikexueyuan.readlrc.utils.Utils;

 import java.io.File;

 /**
* Created by Tall on 2015/8/7.
*/
public class Main {
public static void main(String[] args) { File lrcFile = new File("resource/庄心妍-时间会走.lrc"); Utils utils = new Utils(); if (lrcFile.exists()) {
utils.parseLRC(lrcFile);
} else {
System.out.println("LRC文件不存在");
}
}
}

Main

 package com.jikexueyuan.readlrc.lrc;

 /**
* Created by Gaojinhua on 2015/8/8.
*/
public class LRC {
private int timeKey;
private String currentLrc; public LRC(int timeKey, String currentLrc) {
this.currentLrc = currentLrc;
this.timeKey = timeKey;
} public String getLrc() {
return currentLrc;
} public void setLrc(String lrc) {
this.currentLrc = lrc;
} public int getTimeKey() {
return timeKey;
} public void setTime(int timeKey) {
this.timeKey = timeKey;
}
}

LRC

 package com.jikexueyuan.readlrc.utils;

 import com.jikexueyuan.readlrc.lrc.LRC;

 import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*; /**
* Created by Gaojinhua on 2015/8/8.
*/
public class Utils {
public void parseLRC(File lrcFile) {
String line; int timeKey;
String currentLrc; final Map<Integer, LRC> lrcMap = new LinkedHashMap<Integer, LRC>(); try {
FileInputStream fileInputStream = new FileInputStream(lrcFile);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader); while ((line = bufferedReader.readLine()) != null) {
timeKey = 60 * Integer.parseInt(line.substring(line.indexOf(":") - 2, line.indexOf(":")))
+ Integer.parseInt(line.substring(line.indexOf(":") + 1, line.indexOf(":") + 3));
currentLrc = line.substring(line.lastIndexOf("]") + 1) + " "; LRC lrc = new LRC(timeKey, currentLrc);
lrcMap.put(lrc.getTimeKey(), lrc);
} final Timer timer = new Timer();
final TimerTask timerTask = new TimerTask() {
@Override
public void run() {
int tempTime = 0;
for (LRC lrc : lrcMap.values()) {
try {
Thread.sleep(1000 * (lrc.getTimeKey() - tempTime));
tempTime = lrc.getTimeKey();
} catch (InterruptedException e) {
e.printStackTrace();
} System.out.printf("\r" + lrc.getLrc());
}
timer.cancel();
//this.cancel();
}
};
timer.schedule(timerTask, new Date()); fileInputStream.close();
inputStreamReader.close();
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Utils