RT,最近在写一个简单的MP3播放器,由于是新手,所以代码比较屎,大神不要嘲笑。。
从文件中(.lrc格式)中读取出的歌词可以在控制台完美输出,但是在Textarea中
是残缺状态,想知道原因及解决办法
以下是读取歌词并显示的代码:
int lastTime = 0;
int minute, second, milliSecond, total;
String str = "", time, words;
try {
while ((str = br.readLine()) != null) {
// 获取'[]'中的时间字符串
time = str.substring(str.indexOf('[') + 1, str.indexOf(']'));
minute = Integer.parseInt(time.substring(0, time.indexOf(':')));
second = Integer.parseInt(time.substring(time.indexOf(':') + 1,
time.indexOf('.')));
milliSecond = Integer
.parseInt(time.substring(time.indexOf(".") + 1));
// 计算总时间
total = minute * 60 * 1000 + second * 1000 + milliSecond * 10;
Thread.sleep(total - lastTime);
// 将歌词前面的时间信息去掉,在控制台输出歌词
words = str.substring(str.indexOf("]") + 1);
System.out.println(words);
textarea.append(words);
textarea.append("\n");
//立即绘制指定区域
textarea.paintImmediately(textarea.getBounds());
System.out.println(textarea.getBounds());
lastTime = total;
}
} catch (NumberFormatException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}