JavaFX Thread Freezing GUI

时间:2014-04-14 20:12:57

标签: java multithreading javafx

我仍然是线程新手,我第一次使用javaFX,所以这可能是双重打击!我相信,我的主要问题是线程。我想在跳过按钮上交叉淡化两个媒体播放器音频。所以,我相信我创建了一个线程来交叉淡化音频两秒钟,然后我应该结束线程。这就是我所想的"我正在做。但是,当程序运行时,它通常会在第二次跳过后冻结。我用于javaFX的大多数代码来自教程,并且在我尝试交叉淡入淡出之前按预期工作。任何建议都将得到充分利用,TIA! CrossFade课程:

import javafx.scene.media.MediaPlayer;
public class CrossFade extends Thread
{
private MediaPlayer mp1;
private MediaPlayer mp2;
private double currentVol;

public CrossFade(MediaPlayer mp1, MediaPlayer mp2)
{
    this.mp1 = mp1;
    this.mp2 = mp2;
    currentVol = mp1.getVolume();
    System.out.println("Vol: " + currentVol);
}

@Override
public void run() {

    mp2.setVolume(0);
    mp2.play();

    //20 increments in volume, every 100ms
    for (int i = 0; i < 20; i ++)
    {
           mp1.setVolume(mp1.getVolume()-currentVol/20);
           mp2.setVolume(mp2.getVolume()+currentVol/20);

           try
           {
               //sleep 100 ms
               Thread.sleep(100);
           }
           catch (InterruptedException e)
           {
               System.out.println("Unable to sleep on xFade");
               e.printStackTrace();
           }
    }
    mp1.stop();
    //this.interrupt();
}

}

音频课程:

import javafx.scene.media.MediaPlayer;
public class Audio{
public static void crossfade(MediaPlayer mp1, MediaPlayer mp2)
{
    Thread xFade = new Thread(new CrossFade(mp1,mp2));
    xFade.start();
}
}

跳过按钮的代码:

skip.setOnAction(new EventHandler<ActionEvent>() {
  @Override public void handle(ActionEvent actionEvent) {
    final MediaPlayer curPlayer = mediaView.getMediaPlayer();
    MediaPlayer nextPlayer = players.get((players.indexOf(curPlayer) + 1) % players.size());
    mediaView.setMediaPlayer(nextPlayer);
    curPlayer.currentTimeProperty().removeListener(progressChangeListener);

    //calls the crossfade in audio class (supposed to start that thread)
    Audio.crossfade(curPlayer,nextPlayer);
    //these were the "old" stop and play calls to the media
    //curPlayer.stop();
    //nextPlayer.play();
  }
});

1 个答案:

答案 0 :(得分:2)

通常,您不应该在FX应用程序线程之外更改UI的任何内容。我没有与MediaPlayers合作过多,但我相信他们遵守相同的规则。

使用Animation API可以大大简化这一过程。 PauseTransition可用于管理每个暂停,您可以使用SequentialTransition按顺序“播放”这些暂停。这基本上可以为您管理所有线程。像(这没有经过测试......)

public class Audio{
public static void crossfade(MediaPlayer mp1, MediaPlayer mp2)
{
    double currentVol = mp1.getVolume();
    mp2.setVolume(0);
    mp2.play();
    SequentialTransition crossfade = new SequentialTransition();
    for (int i=0; i<20 i++) {
        PauseTransition pause = new PauseTransition(Duration.millis(100));
        pause.setOnFinished(event -> {
               mp1.setVolume(mp1.getVolume()-currentVol/20);
               mp2.setVolume(mp2.getVolume()+currentVol/20);
        });
        crossfade.getChildren().add(pause);
    }
    crossfade.setOnFinished(event -> mp1.stop());
    crossfade.play();
}
}

当然,如果您打算使用动画API,那么您也可以以“顺利”完成所有操作的方式使用它,而不是采用不连续的步骤:

public class Audio {
public static void crossfade(MediaPlayer mp1, MediaPlayer mp2) {
    final double currentVolume = mp1.getVolume();
    mp2.setVolume(0);
    mp2.play();
    Timeline crossfade = new Timeline(new KeyFrame(Duration.seconds(2), 
            new KeyValue(mp1.volumeProperty(), 0), 
            new KeyValue(mp2.volumeProperty(), currentVolume)));
    crossfade.setOnFinished(event -> mp1.stop());
    crossfade.play();
}
}

更新:如果您仍在使用JavaFX 2.2,请将lambda表达式(crossfade.setOnFinished(event -> mp1.stop());)替换为内部类:

crossfade.setOnFinished(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        mp1.stop();
    }
});

您还需要将mp1声明为final

public static void crossfade(final MediaPlayer mp1, final MediaPlayer mp2) { ... }