JavaFx AudioClip:当音频文件放在项目的src文件夹中时,URL不起作用?

时间:2017-11-19 10:10:31

标签: java javafx

我的代码

package main;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.media.AudioClip;
import javafx.scene.text.Font;
import javafx.stage.Stage;


public class Main extends Application {
    public void start(Stage primaryStage) throws Exception {
        Font font = Font.loadFont( Main.class.getClassLoader().getResourceAsStream("resources/TRON.TTF"), 10);

        //ImageView image = new ImageView(new Image("file:outside/bell.png"));

       ImageView image = new ImageView(new Image("resources/images/redseven.png"));

       //This works when file is outside src folder
        //AudioClip audio = new AudioClip("file:sounds/win.wav");

        //doesn't work
       //AudioClip audio = new AudioClip("file:resources/sounds/win.wav");

        //doesn't work
        AudioClip audio = new AudioClip("resources/sounds/win.wav");

        audio.play();



        VBox pane = new VBox();
        Label label = new Label("Text");
        label.setStyle("-fx-font-family:'Tron'; -fx-font-size: 50px;-fx-text-fill: red");

        pane.getChildren().addAll(label, image);

        Scene scene = new Scene(pane, 500, 500);
        primaryStage.setScene(scene);
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

我的项目目录

enter image description here

输出

  

引起:java.lang.IllegalArgumentException:uri.getScheme()==   空值! uri =='resources / sounds / win.wav'

图片网址在src文件夹之外和src文件夹内时工作。当他们在外面时,我必须在代码中显示的url之前包含“file:”。

Audioclip irls在文件超出src时工作。但是当它在里面时不起作用。

修改

这样做有效。但我不确定为什么?

AudioClip audio = new AudioClip("file:src/resources/sounds/win.wav");

我还将src文件夹添加到了url,然后在其前面添加了“file:”。 为什么它不适用于相对路径?加载图像没有任何问题。

0 个答案:

没有答案