用输入流读取歌词文件,根据歌词特有的正则表达式逐行解释,用HashMap存储每行歌词,每行的时间转换成Long用来当作关键字关键字。
package com.my.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LrcParser {
class LrcInfo {
private String title;
public String getTitle(){return title;}
public void setTitle(String t){title=t;}
private String singer;
public String getSinger(){return singer;}
public void setSinger(String s){singer=s;}
private String album;
public String getAlbum(){return album;}
public void setAlbum(String a){album=a;}
private String by;
public String getBy(){return by;}
public void setBy(String b){by=b;}
private Map<Long, String> infos;
public Map<Long, String> getInfos(){return infos;}
public void setInfos(Map<Long, String> i){infos=i;}
}
private LrcInfo lrcinfo = new LrcInfo();
private long currentTime = 0;//存放临时时间
private String currentContent = "";//存放临时歌词
public Map<Long, String> maps = new HashMap<Long, String>();//用户保存所有的歌词和时间点信息间的映射关系的Map
public Vector<Long> timeSpec=new Vector<Long>();
private InputStream readLrcFile(String path) throws FileNotFoundException {
File f = new File(path);
/* FileInputStream fis =
initFileEncode(fis);*/
InputStream ins =new FileInputStream(f);;
return ins;
}
public static String initFileEncode(FileInputStream fileInputStream) {
String encode = "gb2312";
try{
byte[] head = new byte[3];
fileInputStream.read(head);
if(head[0]==-17 && head[1]==-69 && head[2] ==-65)
encode = "UTF-8";
else if (head[0] == -1 && head[1] == -2 )
encode = "UTF-16";
else if (head[0] == -2 && head[1] == -1 )
encode = "Unicode";
// else
// System.out.println(head[0]+","+head[1]+","+head[2]);
}catch (IOException e) {
System.out.println(e.getMessage());
}
return encode;
}
public LrcInfo parser(String path) throws Exception {
InputStream in = readLrcFile(path);
if(in!=null){
lrcinfo = parser(in);
return lrcinfo;
}else {
return null;
}
}
public LrcInfo parser(InputStream inputStream) throws IOException {
InputStreamReader inr = new InputStreamReader(inputStream,initFileEncode((FileInputStream) inputStream));
BufferedReader reader = new BufferedReader(inr);
String line = null;
while ((line = reader.readLine()) != null)
parserLine(line);
lrcinfo.setInfos(maps);
return lrcinfo;
}
private void parserLine(String str) {
if (str.startsWith("[ti:")) {
String title = str.substring(4, str.length() - 1);
//System.out.println("title--->" + title);
lrcinfo.setTitle(title);
}
else if (str.startsWith("[ar:")) {
String singer = str.substring(4, str.length() - 1);
//System.out.println("singer--->" + singer);
lrcinfo.setSinger(singer);
}
else if (str.startsWith("[al:")) {
String album = str.substring(4, str.length() - 1);
//System.out.println("album--->" + album);
lrcinfo.setAlbum(album);
}
else if (str.startsWith("[by:")) {
String by = str.substring(4, str.length() - 1);
//System.out.println("by--->" + by);
lrcinfo.setBy(by);
}
else {
String reg = "\\[(\\d{2}:\\d{2}\\.\\d{2})\\]";
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
String msg = matcher.group();//把所有行加入组
int start = matcher.start();
int end = matcher.end();
int groupCount = matcher.groupCount();
for (int i = 0; i <= groupCount; i++) {
String timeStr = matcher.group(i);
if (i == 1) { //毫秒
currentTime = strToLong(timeStr);
timeSpec.add(currentTime);
}
}
String[] content = pattern.split(str);
String oneWord=null;
for (int i = 0; i < content.length; i++) {
if (i == content.length - 1) {
for (int j = 0; j < content[i].length(); j++) {
oneWord=content[i].substring(j, j+1);
byte[] byteWord=oneWord.getBytes();
if(byteWord.length==3){//utf-8,三字节
currentContent+=oneWord;
}else{
continue;
}
}
/* StringTokenizer st = new StringTokenizer(content[i]);
while(st.hasMoreTokens()){
currentContent+=st.nextToken();
}*/
}
}
maps.put(currentTime, currentContent);
currentContent="";
//System.out.println("put---currentTime--->" + currentTime
//+ "----currentContent---->" + currentContent);
}
}
}
private long strToLong(String timeStr) {
String[] s = timeStr.split(":");
int min = Integer.parseInt(s[0]);
String[] ss = s[1].split("\\.");
int sec = Integer.parseInt(ss[0]);
int mill = Integer.parseInt(ss[1]);
return min * 60 * 1000 + sec * 1000 + mill * 10;
}
}
|