1 package com.jikexueyuan.readlrc.main; 2 3 4 import com.jikexueyuan.readlrc.utils.Utils; 5 6 import java.io.File; 7 8 9 /** 10 * Created by Tall on 2015/8/7. 11 */ 12 public class Main { 13 public static void main(String[] args) { 14 15 File lrcFile = new File("resource/庄心妍-时间会走.lrc"); 16 17 Utils utils = new Utils(); 18 19 if (lrcFile.exists()) { 20 utils.parseLRC(lrcFile); 21 } else { 22 System.out.println("LRC文件不存在"); 23 } 24 } 25 }
1 package com.jikexueyuan.readlrc.lrc; 2 3 /** 4 * Created by Gaojinhua on 2015/8/8. 5 */ 6 public class LRC { 7 private int timeKey; 8 private String currentLrc; 9 10 public LRC(int timeKey, String currentLrc) { 11 this.currentLrc = currentLrc; 12 this.timeKey = timeKey; 13 } 14 15 public String getLrc() { 16 return currentLrc; 17 } 18 19 public void setLrc(String lrc) { 20 this.currentLrc = lrc; 21 } 22 23 public int getTimeKey() { 24 return timeKey; 25 } 26 27 public void setTime(int timeKey) { 28 this.timeKey = timeKey; 29 } 30 }
1 package com.jikexueyuan.readlrc.utils; 2 3 import com.jikexueyuan.readlrc.lrc.LRC; 4 5 import java.io.BufferedReader; 6 import java.io.File; 7 import java.io.FileInputStream; 8 import java.io.InputStreamReader; 9 import java.util.*; 10 11 /** 12 * Created by Gaojinhua on 2015/8/8. 13 */ 14 public class Utils { 15 public void parseLRC(File lrcFile) { 16 String line; 17 18 int timeKey; 19 String currentLrc; 20 21 final Map<Integer, LRC> lrcMap = new LinkedHashMap<Integer, LRC>(); 22 23 try { 24 FileInputStream fileInputStream = new FileInputStream(lrcFile); 25 InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8"); 26 BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 27 28 while ((line = bufferedReader.readLine()) != null) { 29 timeKey = 60 * Integer.parseInt(line.substring(line.indexOf(":") - 2, line.indexOf(":"))) 30 + Integer.parseInt(line.substring(line.indexOf(":") + 1, line.indexOf(":") + 3)); 31 currentLrc = line.substring(line.lastIndexOf("]") + 1) + " "; 32 33 LRC lrc = new LRC(timeKey, currentLrc); 34 lrcMap.put(lrc.getTimeKey(), lrc); 35 } 36 37 final Timer timer = new Timer(); 38 final TimerTask timerTask = new TimerTask() { 39 @Override 40 public void run() { 41 int tempTime = 0; 42 for (LRC lrc : lrcMap.values()) { 43 try { 44 Thread.sleep(1000 * (lrc.getTimeKey() - tempTime)); 45 tempTime = lrc.getTimeKey(); 46 } catch (InterruptedException e) { 47 e.printStackTrace(); 48 } 49 50 System.out.printf("\r" + lrc.getLrc()); 51 } 52 timer.cancel(); 53 //this.cancel(); 54 } 55 }; 56 timer.schedule(timerTask, new Date()); 57 58 fileInputStream.close(); 59 inputStreamReader.close(); 60 bufferedReader.close(); 61 } catch (Exception e) { 62 e.printStackTrace(); 63 } 64 } 65 }