JavaFx音频剪辑:URL don';当音频文件放在项目的src文件夹中时,是否不工作?
我的代码JavaFx音频剪辑:URL don';当音频文件放在项目的src文件夹中时,是否不工作?,java,javafx,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.A
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);
}
}
我的项目目录
输出
原因:java.lang.IllegalArgumentException:uri.getScheme()==
无效的uri=='resources/sounds/win.wav'
图像url在src文件夹外部和src文件夹内部时的工作。当它们在外部时,我必须在url之前包含“file:”,如代码所示
音频剪辑IRL在文件位于src之外时工作。但是当它在里面的时候就不起作用了
编辑
这样做很有效。但我不确定为什么
AudioClip audio = new AudioClip("file:src/resources/sounds/win.wav");
我还将src文件夹添加到url中,然后在其前面添加了“file:”。
为什么它不适用于相对路径?加载映像时没有任何问题。通常使用
SomeClassInstance.getReource().toExternalForm()
作为资源。有些IDE在没有正确添加资源的情况下似乎会弄糟这一点,尽管…@fabian您能举一个例子说明如何使用该方法获取资源吗?通常您使用SomeClassInstance.getreSource().toExternalForm()
获取资源。有些IDE在没有正确添加资源的情况下似乎会弄糟这一点…@fabian你能举个例子说明如何使用这种方法获取资源吗?