gpt4 book ai didi

播放 JMF 视频时更新视频控制 UI 时出现 Java 并发问题

转载 作者:行者123 更新时间:2023-12-01 05:26:22 25 4
gpt4 key购买 nike

我正在围绕 JMF 用纯 Java 构建一个视频播放器,具有完全自定义的 UI 控件。一切都工作得很好,直到我放入 JLabel,它以 hh:mm:ss.ss 格式更新当前播放时间。标签更新可以接受,但偶尔会一次停止 10 秒以上,这是 Not Acceptable 。

JMF Player 是在 SwingUtilities.invokeLater(new Runnable()... block 中创建的。以下是 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));
}

它在 StartEvent 上的 ControllerListener 中调用。在此期间正在播放音频/视频。

不可否认,当涉及到 Java 并发性时,我还是个新手,而且我确信有更好的方法来处理这个单独的线程。有什么建议吗?

最佳答案

你可以用 ScheduledExecutorService 替换你的 Thread/sleep : http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html

因为使用 sleep 时无法保证线程实际会 hibernate 多长时间,因此可能需要比您指定的时间更长的时间才能返回到运行状态。

关于播放 JMF 视频时更新视频控制 UI 时出现 Java 并发问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9623557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com