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

时间:2019-05-17 19:16:50

标签: javafx audio

当用户单击按钮时,我正在尝试播放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);
    }
}

控制器已正确配置为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

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

1 个答案:

答案 0 :(得分:0)

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

以下内容可能会帮助系统找到您的音频资源:

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

也许将这一行移到start()方法中,然后运行System.out.println(audio)来检查您此时的状态。

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