如题所述
å
ä»æåºæ¬ç读åæè¯æ件å¼å§ï¼
ããPublic class LrcHandle {
ããprivate List mWords = new ArrayList();
ããprivate List mTimeList = new ArrayList();
ãã//å¤çæè¯æ件
ããpublic void readLRC(String path) {
ããFile file = new File(path);
ããtry {
ããFileInputStream fileInputStream = new FileInputStream(file);
ããInputStreamReader inputStreamReader = new InputStreamReader(
ããfileInputStream, "utf-8");
ããBufferedReader bufferedReader = new BufferedReader(
ããinputStreamReader);
ããString s = "";
ããwhile ((s = bufferedReader.readLine()) != null) {
ããaddTimeToList(s);
ããif ((s.indexOf("[ar:") != -1) || (s.indexOf("[ti:") != -1)
ãã|| (s.indexOf("[by:") != -1)) {
ããs = s.substring(s.indexOf(":") + 1, s.indexOf("]"));
ãã} else {
ããString ss = s.substring(s.indexOf("["), s.indexOf("]") + 1);
ããs = s.replace(ss, "");
ãã}
ããmWords.add(s);
ãã}
ããbufferedReader.close();
ããinputStreamReader.close();
ããfileInputStream.close();
ãã} catch (FileNotFoundException e) {
ããe.printStackTrace();
ããmWords.add("没ææè¯æ件ï¼èµ¶ç´§å»ä¸è½½");
ãã} catch (IOException e) {
ããe.printStackTrace();
ããmWords.add("没æ读åå°æè¯");
ãã}
ãã}
ããpublic List getWords() {
ããreturn mWords;
ãã}
ããpublic List getTime() {
ããreturn mTimeList;
ãã}
ãã// å离åºæ¶é´
ããprivate int timeHandler(String string) {
ããstring = string.replace(".", ":");
ããString timeData[] = string.split(":");
ãã// å离åºåãç§å¹¶è½¬æ¢ä¸ºæ´å
ããint minute = Integer.parseInt(timeData[0]);
ããint second = Integer.parseInt(timeData[1]);
ããint millisecond = Integer.parseInt(timeData[2]);
ãã// 计ç®ä¸ä¸è¡ä¸ä¸ä¸è¡çæ¶é´è½¬æ¢ä¸ºæ¯«ç§æ°
ããint currentTime = (minute * 60 + second) * 1000 + millisecond * 10;
ããreturn currentTime;
ãã}
ããprivate void addTimeToList(String string) {
ããMatcher matcher = Pattern.compile(
ãã"[d{1,2}:d{1,2}([.:]d{1,2})?]").matcher(string);
ããif (matcher.find()) {
ããString str = matcher.group();
ããmTimeList.add(new LrcHandle().timeHandler(str.substring(1,
ããstr.length() - 1)));
ãã}
ãã}
ãã}
ããä¸è¬æè¯æ件çæ ¼å¼å¤§æ¦å¦ä¸:
ãã[ar:èºäººå]
ãã[ti:æ²å]
ãã[al:ä¸è¾å]
ãã[by:ç¼è (æç¼è¾LRCæè¯ç人)]
ãã[offset:æ¶é´è¡¥å¿å¼] å ¶åä½æ¯æ¯«ç§ï¼æ£å¼è¡¨ç¤ºæ´ä½æåï¼è´å¼ç¸åãè¿æ¯ç¨äºæ»ä½è°æ´æ¾ç¤ºå¿«æ ¢çã
ããä½ä¹ä¸ä¸å®ï¼ææ¶å并没æåé¢é£äºar:çæ è¯ç¬¦ï¼æ以æ们è¿éä¹æä¾äºå¦ä¸ç§è§£ææ¹å¼ã
ããæè¯æ件ä¸çæ¶é´æ ¼å¼åæ¯è¾ç»ä¸:[00:00.50]ççï¼00:表示åéï¼00.表示ç§æ°ï¼.50表示毫ç§æ°ï¼å½ç¶ï¼æ们æåæ¯è¦å°å®ä»¬è½¬å为毫ç§æ°å¤çææ¯è¾æ¹ä¾¿ã
ããPublic class LrcHandle {
ããprivate List mWords = new ArrayList();
ããprivate List mTimeList = new ArrayList();
ãã//å¤çæè¯æ件
ããpublic void readLRC(String path) {
ããFile file = new File(path);
ããtry {
ããFileInputStream fileInputStream = new FileInputStream(file);
ããInputStreamReader inputStreamReader = new InputStreamReader(
ããfileInputStream, "utf-8");
ããBufferedReader bufferedReader = new BufferedReader(
ããinputStreamReader);
ããString s = "";
ããwhile ((s = bufferedReader.readLine()) != null) {
ããaddTimeToList(s);
ããif ((s.indexOf("[ar:") != -1) || (s.indexOf("[ti:") != -1)
ãã|| (s.indexOf("[by:") != -1)) {
ããs = s.substring(s.indexOf(":") + 1, s.indexOf("]"));
ãã} else {
ããString ss = s.substring(s.indexOf("["), s.indexOf("]") + 1);
ããs = s.replace(ss, "");
ãã}
ããmWords.add(s);
ãã}
ããbufferedReader.close();
ããinputStreamReader.close();
ããfileInputStream.close();
ãã} catch (FileNotFoundException e) {
ããe.printStackTrace();
ããmWords.add("没ææè¯æ件ï¼èµ¶ç´§å»ä¸è½½");
ãã} catch (IOException e) {
ããe.printStackTrace();
ããmWords.add("没æ读åå°æè¯");
ãã}
ãã}
ããpublic List getWords() {
ããreturn mWords;
ãã}
ããpublic List getTime() {
ããreturn mTimeList;
ãã}
ãã// å离åºæ¶é´
ããprivate int timeHandler(String string) {
ããstring = string.replace(".", ":");
ããString timeData[] = string.split(":");
ãã// å离åºåãç§å¹¶è½¬æ¢ä¸ºæ´å
ããint minute = Integer.parseInt(timeData[0]);
ããint second = Integer.parseInt(timeData[1]);
ããint millisecond = Integer.parseInt(timeData[2]);
ãã// 计ç®ä¸ä¸è¡ä¸ä¸ä¸è¡çæ¶é´è½¬æ¢ä¸ºæ¯«ç§æ°
ããint currentTime = (minute * 60 + second) * 1000 + millisecond * 10;
ããreturn currentTime;
ãã}
ããprivate void addTimeToList(String string) {
ããMatcher matcher = Pattern.compile(
ãã"[d{1,2}:d{1,2}([.:]d{1,2})?]").matcher(string);
ããif (matcher.find()) {
ããString str = matcher.group();
ããmTimeList.add(new LrcHandle().timeHandler(str.substring(1,
ããstr.length() - 1)));
ãã}
ãã}
ãã}
ããä¸è¬æè¯æ件çæ ¼å¼å¤§æ¦å¦ä¸:
ãã[ar:èºäººå]
ãã[ti:æ²å]
ãã[al:ä¸è¾å]
ãã[by:ç¼è (æç¼è¾LRCæè¯ç人)]
ãã[offset:æ¶é´è¡¥å¿å¼] å ¶åä½æ¯æ¯«ç§ï¼æ£å¼è¡¨ç¤ºæ´ä½æåï¼è´å¼ç¸åãè¿æ¯ç¨äºæ»ä½è°æ´æ¾ç¤ºå¿«æ ¢çã
ããä½ä¹ä¸ä¸å®ï¼ææ¶å并没æåé¢é£äºar:çæ è¯ç¬¦ï¼æ以æ们è¿éä¹æä¾äºå¦ä¸ç§è§£ææ¹å¼ã
ããæè¯æ件ä¸çæ¶é´æ ¼å¼åæ¯è¾ç»ä¸:[00:00.50]ççï¼00:表示åéï¼00.表示ç§æ°ï¼.50表示毫ç§æ°ï¼å½ç¶ï¼æ们æåæ¯è¦å°å®ä»¬è½¬å为毫ç§æ°å¤çææ¯è¾æ¹ä¾¿ã
温馨提示:答案为网友推荐,仅供参考