写道
/**
* 把显示歌词封装在一个线程里
* @param <T>
*/
public class LRCDisplayThread<T extends LRCDisplayAction> implements Runnable
{
/**
* 读取lrc文件必须的路径
*/
private final String filename;
/**
* 解析出的lrc对象
* <p>一个lrc对应一个显示线程,歌词显示完后
* ,该歌词对应的线程自然死亡,所以歌词对象用final</p>
* <p>要显示下一个歌词应该重启一个线程</p>
*/
private final Lyrics ls;
/**
* 运行该线程所在的对象,要求改对象需要实现接口LRCDisplayAction
*/
private final T t;
/**
* 是否处于暂停状态
*/
private boolean isPause = false;
public LRCDisplayThread(String filename, T t)
{
this.filename = filename;
LRC lrc = ReadLRC.readLRC(filename);
Lyrics ls = ParseLRC.parseLRC(lrc);
this.ls = ls;
this.t = t;
}
private void actionPause()
{
try
{
this.wait();
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getFilename()
{
return filename;
}
/**
* 暂停
*/
public void pause()
{
isPause = true;
}
@Override
public void run()
{
// TODO Auto-generated method stub
System.out.println("艺术家:" + ls.getAr());
System.out.println("专辑:" + ls.getAl());
System.out.println("歌曲:" + ls.getTi());
System.out.println("歌词制作:" + ls.getBy());
//Thread.sleep(ls.getOffset());//时间补偿暂未处理
for (Lyric l : ls.getLyrics())
{
//System.out.println(l.getTxt());
t.action(l);//显示歌词【更新UI】
try
{
if (isPause)
{//是暂停状态,则线程等待
this.actionPause();
}
Thread.sleep(l.getTimeSize());
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void unpause()
{
isPause = false;
this.notify();
}
}