我正在用JMF構建純Java視頻播放器,並使用完全自定義的UI控件。一切都很好,直到我放入一個JLabel,它以hh:mm:ss.ss格式更新當前播放時間。標籤更新可以接受,但偶爾會暫停10秒以上,這是不可接受的。在JMF視頻播放期間更新視頻控制UI時的併發問題
JMF Player
創建於SwingUtilities.invokeLater(new Runnable()...
塊。下面是UI更新代碼:
protected void createUIUpdater() {
System.out.println("Creating UI updating worker thread");
new Thread() {
@Override
public void run() {
while(mediaPlayer.getState() == Controller.Started) {
updatePlayPosition();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.err.println("Sleep interrupted!");
}
}
System.out.println("UI Updater thread finished.");
}
}.start();
}
protected void updatePlayPosition() {
Movie movie = timelineEditor.getMovie();
movie.setInsertionPoint(Rational.valueOf(mediaPlayer.getMediaTime().getSeconds()));
updateTimeLabel();
}
protected void updateTimeLabel() {
Movie movie = timelineEditor.getMovie();
Rational time = movie == null ? new Rational(0,1) : movie.getInsertionPoint();
// ... hours/minutes/seconds calculated
timeLabel.setText((hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes)
+ ":" + (seconds < 10 ? "0" + seconds : seconds)
+ "." + (frame < 10 ? "0" + frame : frame));
}
這就是所謂的ControllerListener
上StartEvent
。在此期間,音頻/視頻正在播放。
對於Java中的併發性,我承認有點新手,而且我確信有更好的方法來處理這個單獨的線程。有什麼建議麼?
嘗試調用標籤invalidate,然後在每次更新後進行驗證。 – roni 2012-03-08 19:52:51
問題不在於JLabel不重繪,而是線程似乎「掛起」。我在'updateTimeLabel()'中輸入了一個sysout語句,當問題發生時,系統不會被調用。 – alalonde 2012-03-08 22:21:27