我正在制作一个屏幕保护程序,该屏幕保护程序使用Java FX循环显示随机视频剪辑。
我的应用程序显示一个时钟,该时钟显示当前时间,向后显示一个影片剪辑。
通常它可以正常工作。但是当我打开它几分钟后,MediaPlayer冻结了。时钟一直在响,但是只有MediaPlayer没有响。
我做了两个主题。一个负责更新时钟,另一个负责更改MediaPlayer。我怀疑更改MediaPlayer的线程处理是否有问题。但是我不知道这是什么。
这是我的代码。
public void start() {
showIntro();
mediaV.setMediaPlayer(mediaPlayer);
if (thread == null) {
thread = new Thread(this);
thread.start();
}
if (changer_thread == null) {
changer_thread = new Thread(new Changer());
changer_thread.start();
}
}
@Override
public void run() {
while (true) {
showCurrentTime();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Changer implements Runnable {
private int interval;
public Changer(int interval) {
this.interval = interval;
}
public Changer() {
this.interval = 10000;
}
@Override
public void run() {
try {
while (true) {
Thread.sleep(interval);
showClip();
Thread.sleep(interval);
showIntro();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void showIntro() {
int interval = 5850;
/* I skipped the code handling about clipInd and groupInd */
main.setBg("white");
timeLabel.setStyle("-fx-opacity: 1.0");
Media media = new Media(
this.getClass().getResource("view/Intro_" + IntroString[groupInd] + ".mp4").toExternalForm());
if (mediaPlayer != null)
mediaPlayer.dispose();
mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
bg_thread = new Thread(new BgHandler(interval));
bg_thread.start(); //bg_thread changes main pane's bg color
mediaV.setMediaPlayer(mediaPlayer); //type of mediaV is MediaView
}
public void showClip() {
main.setBg("white");
Media media = new Media(
this.getClass().getResource("view/Clip_" + IntroString[groupInd] + ".mp4").toExternalForm());
if (mediaPlayer != null)
mediaPlayer.dispose();
mediaPlayer = new MediaPlayer(media);
double millis = 10000*clipInd;
mediaPlayer.setStartTime(Duration.millis(millis + 200));
mediaPlayer.setStopTime(Duration.millis(millis + 10000));
mediaPlayer.setAutoPlay(true);
mediaV.setMediaPlayer(mediaPlayer);
}