/** * 根据传递过来的已播放的毫秒数,计算应当对应到句子集合中的哪一句,再通知监听者播放到的位置。 * * @param millisecond 已播放的毫秒数 */ public void notifyTime(long millisecond) { // Log.i(TAG, "notifyTime"); if (mHasLyric && mLyricSentences != null && mLyricSentences.size() != 0) { int newLyricIndex = seekSentenceIndex(millisecond); if (newLyricIndex != -1 && newLyricIndex != mIndexOfCurrentSentence) { // 如果找到的歌词和现在的不是一句。 if (mLyricListener != null) { // 告诉一声,歌词已经变成另外一句啦! mLyricListener.onLyricSentenceChanged(newLyricIndex); } mIndexOfCurrentSentence = newLyricIndex; } } }
/** * 根据歌词文件的路径,读取出歌词文本并解析 * * @param lyricPath 歌词文件路径 * @return true表示存在歌词,false表示不存在歌词 */ public boolean loadLyric(String lyricPath) { C.logshow("LoadLyric begin,path is:" + lyricPath); mHasLyric = false; mLyricSentences.clear(); if (lyricPath != null) { File file = new File(lyricPath); if (file.exists()) { C.logshow("歌词文件存在"); mHasLyric = true; try { FileInputStream fr = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fr, mEncoding); BufferedReader br = new BufferedReader(isr); String line = null; // 逐行分析歌词文本 while ((line = br.readLine()) != null) { C.logshow("lyric line:" + line); parseLine(line); } // 按时间排序句子集合 Collections.sort( mLyricSentences, new Comparator<LyricSentence>() { // 内嵌,匿名的compare类 public int compare(LyricSentence object1, LyricSentence object2) { if (object1.getStartTime() > object2.getStartTime()) { return 1; } else if (object1.getStartTime() < object2.getStartTime()) { return -1; } else { return 0; } } }); for (int i = 0; i < mLyricSentences.size() - 1; i++) { mLyricSentences.get(i).setDuringTime(mLyricSentences.get(i + 1).getStartTime()); } mLyricSentences.get(mLyricSentences.size() - 1).setDuringTime(Integer.MAX_VALUE); fr.close(); } catch (Exception e) { e.printStackTrace(); } finally { } } else { C.logshow("歌词文件不存在"); } } // 如果有谁在监听,通知它歌词载入完啦,并把载入的句子集合也传递过去 if (mLyricListener != null) { mLyricListener.onLyricLoaded(mLyricSentences, mIndexOfCurrentSentence); } if (mHasLyric) { // Log.i(TAG, "Lyric file existed.Lyric has " + // mLyricSentences.size() // + " Sentences"); } else { // Log.i(TAG, "Lyric file does not existed"); } return mHasLyric; }