JavaFX MediaPlayer" OnEndOfMedia"不在runnable jar文件中工作

时间:2016-02-02 21:10:54

标签: events audio javafx media executable-jar

如果我从项目中的包中加载音频文件并通过IDE启动它,那么setOnEndOfMedia()方法正常工作。但是当我将项目导出到一个可运行的jar文件(Eclipse库处理=提取)时,它根本没有反应。它是从默认目录加载文件时的反应,但我想从我的包中加载它作为一个ressource。这是一个示例代码:

public class MediaTest extends Application
{
    public static void main(String[] args)
    {
        launch();
    }

    @Override
    public void start(Stage primaryStage)
    {
        Button button1 = new Button("from outside");
        Button button2 = new Button("from package");
        primaryStage.setScene(new Scene(new HBox(button1, button2)));

        button1.setOnAction(e ->
        {
            Media media = new Media("file:/C:/sample.wav"); //use any audio
            MediaPlayer player = new MediaPlayer(media);
            player.setOnEndOfMedia(() -> System.out.println("End of Media"));
            player.play();
        });
        button2.setOnAction(e ->
        {
            Media media = new Media(ClassLoader.getSystemResource("sounds/sample.wav").toExternalForm()); //use any audio
            MediaPlayer player = new MediaPlayer(media);
            player.setOnEndOfMedia(() -> System.out.println("End of Media"));
            player.play();
        });

        primaryStage.show();
    }
}

0 个答案:

没有答案