读取Lrc-Java

来源:互联网 发布:足球大师小贝捏脸数据 编辑:程序博客网 时间:2024/06/16 16:37
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.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 = null;//存放临时歌词private Map<Long, String> maps = new HashMap<Long, String>();//用户保存所有的歌词和时间点信息间的映射关系的Mapprivate InputStream readLrcFile(String path) throws FileNotFoundException {File f = new File(path);InputStream ins = new FileInputStream(f);return ins;        }public LrcInfo parser(String path) throws Exception {InputStream in = readLrcFile(path);lrcinfo = parser(in);return lrcinfo;}public LrcInfo parser(InputStream inputStream) throws IOException {InputStreamReader inr = new InputStreamReader(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);}}String[] content = pattern.split(str);for (int i = 0; i < content.length; i++) {if (i == content.length - 1) {currentContent = content[i];}}maps.put(currentTime, 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;}public static void main(String[] args) {LrcParser lp = new LrcParser();try {lp.parser(args[0]);} catch (Exception e) {System.out.println("parser error");e.printStackTrace();}}}

原创粉丝点击