解析LRC歌词文件readlrc

 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 }
Main
 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 }
LRC
 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 }
Utils

 

转载于:https://www.cnblogs.com/gaojinhua/p/4710970.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值