JavaFX 播放 AudioClip,InvocationTargetException

JavaFX Playing AudioClip,InvocationTargetException

我正在尝试播放 mp3 文件并遇到异常。这是我的代码和堆栈跟踪。我的声音在我的工作区。

public class Main extends Application {
//my code
    @Override 
    public void start(Stage primaryStage) {
        URL resource = getClass().getResource("/Alistirma/src/IkinciAlistirma/hated.mp3"); //its in my workspace
        AudioClip clip = new AudioClip(resource.toString()); //line 21

        final Button button = new Button("Play"); //simple button just for playing clip
        button.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub
                clip.play();
            }
        });
               //after here nothing important for my problem.
               //basic FX stuff
    }

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

这只是播放 mp3 的简单代码,但出现异常,这是堆栈跟踪

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    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(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication5(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at IkinciAlistirma.Main.start(Main.java:21)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater4(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null(GtkApplication.java:139)
    ... 1 more
Exception running application IkinciAlistirma.Main

问题在这里:

URL resource = URL resource = getClass().getResource("/Alistirma/src/IkinciAlistirma/hated.mp3"); 

首先:使用 getResource(String path) 方法无法从您的工作区访问您的 mp3 文件。根据您的环境和 IDE,它会在您项目的不同位置搜索 "Alistirma/src/IkinciAlistirma/hated.mp3"。您可以通过打印getResource(".")给出的URL找到getResource(String path)正在搜索您的资源的起点文件夹,然后扣除您要放置文件的位置。如果您想将文件保留在原处,您仍然可以使用 File class.

访问它

其次:为什么URL resource = URL resource =? copy/paste 我想失败了 ;) ?