gpt4 book ai didi

javafx - JAVAFX在单击按钮时尝试播放声音的问题

转载 作者:行者123 更新时间:2023-12-03 01:32:47 24 4
gpt4 key购买 nike

当用户单击按钮时,我正在尝试播放Windows声音。代码如下:

public class TestController extends Application {

public String audio = getClass().getResource("src/Sounds/WindowsError.wav").toString();

@FXML
private Button playbt;

@FXML
void playtest(MouseEvent event)
{

System.out.println("Clicked!");
AudioClip clip = new AudioClip(audio);// 1
clip.play(); // 2

}

@Override
public void start(Stage primaryStage) {


try

{

FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/FXML/Test.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root);
primaryStage.setTitle("Test");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();

}

catch (IOException e)

{
// TODO Auto-generated catch block
e.printStackTrace();
}


}

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

Controller 已正确配置为FXML。但是,当我尝试运行项目时,发生以下错误。
Exception in Application constructor java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class Controller.TestController
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$159(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$165(LauncherImpl.java:819)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
... 1 more
Caused by: java.lang.NullPointerException
at Controller.TestController.<init>(TestController.java:17)
... 13 more Exception running application Controller.TestController

我项目中的文件安排如下:

enter image description here

有谁知道为什么会发生此错误?由于声音播放代码,打开项目时将挂起。

最佳答案

我对此并不完全满意,但我认为音频变量的getResource语法采用“相对地址”形式,因为它不是以“/”开头。换句话说,它可能正在Controller包文件夹中寻找子文件夹src。

也许以下内容将帮助系统找到您的音频资源:

public String audio = getClass().getResource("../Sounds/WindowsError.wav").toString();

将这行代码移到start()方法中,然后运行System.out.println(audio)来检查此时的内容,可能会很好。

编辑:刚看到fabian的评论。按照他的建议,使用classpath根目录更改为表单可能比我建议使用相对路径更好。

关于javafx - JAVAFX在单击按钮时尝试播放声音的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56192345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com