歌词滚动的Java代码 java歌词滚动字幕( 二 )


* get lrc word
*/
public String getLyric() {
return lyric;
}
/*
* set lrc word
*/
public void setLyric(String lyric) {
this.lyric = lyric;
}
}
private BufferedReader bufferReader = null;
private String title = "";
private String artist = "";
private String album = "";
private String lrcMaker = "";
private ListStatement statements = new ArrayListStatement();
/*
*
* fileName
*/
public utilLrc(String fileName) throws IOException{
FileInputStream file = new FileInputStream(fileName);
bufferReader = new BufferedReader(new InputStreamReader(file, "utf-8"));
readData();
}
/*
* read the file
*/
private void readData() throws IOException{
statements.clear();
String strLine;
while(null != (strLine = bufferReader.readLine()))
{
if("".equals(strLine.trim()))
{
continue;
}
if(null == title || "".equals(title.trim()))
{
Pattern pattern = Pattern.compile("\\[ti:(.+?)\\]");
Matcher matcher = pattern.matcher(strLine);
if(matcher.find())
{
title=matcher.group(1);
continue;
}
}
if(null == artist || "".equals(artist.trim()))
{
Pattern pattern = Pattern.compile("\\[ar:(.+?)\\]");
Matcher matcher = pattern.matcher(strLine);
if(matcher.find())
{
artist=matcher.group(1);
continue;
}
}
if(null == album || "".equals(album.trim()))
{
Pattern pattern = Pattern.compile("\\[al:(.+?)\\]");
Matcher matcher = pattern.matcher(strLine);
if(matcher.find())
{
album=matcher.group(1);
continue;
}
}
if(null == lrcMaker || "".equals(lrcMaker.trim()))
{
Pattern pattern = Pattern.compile("\\[by:(.+?)\\]");
Matcher matcher = pattern.matcher(strLine);
if(matcher.find())
{
lrcMaker=matcher.group(1);
continue;
}
}
int timeNum=0;
String str[] = strLine.split("\\]");
for(int i=0; istr.length; ++i)
{
String str2[] = str[i].split("\\[");
str[i] = str2[str2.length-1];
if(isTime(str[i])){
++timeNum;
}
}
for(int i=0; itimeNum;++i)
{
Statement sm = new Statement();
sm.setTime(str[i]);
if(timeNumstr.length)
{
sm.setLyric(str[str.length-1]);
}
statements.add(sm);
}
}
sortLyric();
}
/*
* judge the string is or not date format.
*/
private boolean isTime(String string)
{
String str[] = string.split(":|\\.");
if(3!=str.length)
{
return false;
}
try{
for(int i=0;istr.length;++i)
{
Integer.parseInt(str[i]);
}
}
catch(NumberFormatException e)
{
Log.e(TAG, "isTime exception歌词滚动的Java代码:"+e.getMessage());
return false;
}
return true;
}
/*
* sort the word by time.
*/
private void sortLyric()
{
for(int i=0;istatements.size()-1;++i)
{
int index=i;
double delta=Double.MAX_VALUE;
boolean moveFlag = false;
for(int j=i+1;jstatements.size();++j)
{
double sub;
if(0=(sub=statements.get(i).getTime()-statements.get(j).getTime()))
{
continue;
}
moveFlag=true;
if(subdelta)
{
delta=sub;
index=j+1;
}
}
if(moveFlag)
{
statements.add(index, statements.get(i));
statements.remove(i);
--i;
}
}
}
/**
* get title
* @return
*/
public String getTitle(){
return title;
}
/**
* get artist
* @return
*/
public String getArtist(){

推荐阅读