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);
|
}
|
}
|
}
|
|
|
}
|