Runt
2022-05-15 75360a9fa2fcead8b516467d982c5cdf82c63263
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.auto.lyric.data;
 
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.List;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * Created by Runt (qingingrunt2010@qq.com) on 2022/5/4.
 */
public class LyricServer {
 
 
    // key 时间戳 , obj  歌词
    private static TreeMap<Integer, LyricObject> lrc_map = new TreeMap<>();
 
    public static TreeMap<Integer, LyricObject> getLrc_map() {
        return lrc_map;
    }
 
 
    /**
     * 读取歌词文件
     * @param file 歌词的路径
     *
     */
    public static void read(String file) {
        TreeMap<Integer, LyricObject> lrc_read =new TreeMap<Integer, LyricObject>();
        String data = "";
        try {
            File saveFile=new File(file);
            // System.out.println("是否有歌词文件"+saveFile.isFile());
            if(!saveFile.isFile()){
                return;
            }
            //System.out.println("bllrc==="+blLrc);
            FileInputStream stream = new FileInputStream(saveFile);//  context.openFileInput(file);
 
            BufferedReader br = new BufferedReader(new InputStreamReader(stream,"GB2312"));
            while ((data = br.readLine()) != null) {
                readText(data,lrc_read);
            }
            stream.close();
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
        data ="";
        initLrc(lrc_read);
 
    }
 
    public static void read(List<String> texts){
        TreeMap<Integer, LyricObject> lrc_read =new TreeMap<Integer, LyricObject>();
        for(String str : texts){
            readText(str,lrc_read);
        }
        initLrc(lrc_read);
    }
 
    private static void readText(String text,TreeMap<Integer, LyricObject> lrc_read){
        Pattern pattern = Pattern.compile("\\d{2}");
        // System.out.println("++++++++++++>>"+data);
        /*text = text.replace("[","");//将前面的替换成后面的
        text = text.replace("]","@");
        String splitdata[] = text.split("@");//分隔
        String regex = "(?<=\\])";
        Pattern compile = Pattern.compile(regex);
        String[] splitdata = compile.split(text,2);//分隔*/
        int index = text.indexOf("]");
        String[] splitdata;
        if(text.length()-1 == index){
            splitdata = new String[]{text};
        }else{
            splitdata = new String[]{text.substring(0,index),text.substring(index+1)};
        }
        String tmpstr = splitdata[0].replace("[","").replace("]","");
        if(splitdata.length == 1){
            tmpstr = tmpstr.replace(":",".");
            tmpstr = tmpstr.replace(".","@");
            String timedata[] =tmpstr.split("@");
            Matcher matcher = pattern.matcher(timedata[0]);
            if(timedata.length==3 && matcher.matches()){
                int m = Integer.parseInt(timedata[0]);  //分
                int s = Integer.parseInt(timedata[1]);  //秒
                int ms = Integer.parseInt(timedata[2]); //毫秒
                int currTime = (m*60+s)*1000+ms*10;
                LyricObject item1= new LyricObject();
                item1.begintime = currTime;
                item1.lrc       = "";
                lrc_read.put(currTime,item1);
            }
        } else{
            String lrcContenet = splitdata[1];
            tmpstr = tmpstr.replace(":",".");
            tmpstr = tmpstr.replace(".","@");
            String timedata[] =tmpstr.split("@");
            Matcher matcher = pattern.matcher(timedata[0]);
            if(timedata.length==3 && matcher.matches()){
                int m = Integer.parseInt(timedata[0]);  //分
                int s = Integer.parseInt(timedata[1]);  //秒
                int ms = Integer.parseInt(timedata[2]); //毫秒
                int currTime = (m*60+s)*1000+ms*10;
                LyricObject item1= new LyricObject();
                item1.begintime = currTime;
                item1.lrc       = lrcContenet;
                lrc_read.put(currTime,item1);// 将currTime当标签  item1当数据 插入TreeMap里
            }
        }
    }
 
    /*
     * 遍历hashmap 计算每句歌词所需要的时间
     */
    private static void initLrc(TreeMap<Integer, LyricObject> lrc_read){
        lrc_map.clear();
        LyricObject oldval  = null;
        int i =0;
        for(Integer key : lrc_read.keySet()){
            LyricObject val = lrc_read.get(key);
            if (oldval == null) {
                oldval = val;
            } else {
                LyricObject item1 = oldval;
                item1.timeline = val.begintime-oldval.begintime;
                lrc_map.put(new Integer(i), item1);
                i++;
                oldval = val;
            }
            if (lrc_read.size() > i) {
                lrc_map.put(new Integer(i), val);
            }
        }
    }
 
 
}