【问题标题】:Trying play audio file with JavaFX [duplicate]尝试使用 JavaFX 播放音频文件 [重复]
【发布时间】:2022-01-12 22:24:23
【问题描述】:

我正在尝试在我的 JavaFX 应用程序中播放音频文件,代码对我来说看起来不错,但我遇到了错误

我认为问题出在第 28 行:

“引起:java.lang.NullPointerException:无法调用“java.net.URL.toString()”,因为“resource”为空 在 sample.Main.start(Main.java:28)"

为什么资源为空?在我的文件结构中我得到了资源包,但是音频文件在哪里并不重要,错误是一样的

我是编码新手,所以我不知道如何准确阅读这些错误...如果有人可以提供帮助,那就太好了

亲爱的代码和错误

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;

import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.IOException;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws UnsupportedAudioFileException, IOException, LineUnavailableException
    {
        String css = this.getClass().getResource("styles.css").toExternalForm();
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        root.getStylesheets().add(css);
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();

        final java.net.URL resource = getClass().getResource("test2.mp3");
        final Media media = new Media(resource.toString());
        final MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaPlayer.play();
    }

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

运行此代码后,我收到这些错误

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NullPointerException: Cannot invoke "java.net.URL.toString()" because "resource" is null
    at sample.Main.start(Main.java:28)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    ... 1 more
Exception running application sample.Main

Process finished with exit code 1

感谢任何帮助!

【问题讨论】:

  • 请注意,在使用 JavaFX 的媒体 API 时,您不需要导入 Java 声音。另请注意,您应该保持对MediaPlayer 的强烈引用;如果它被垃圾收集,媒体将停止播放。至于错误,如果使用指定路径找不到资源,getResource(String) 将返回 null。查看How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application? 以获得一些帮助。
  • 谢谢你的建议,我现在就去做!
  • 据我所知,"test2.mp3" 应该是 "/sample/test2.mp3"
  • @AndrewThompson,我在想,因为 Main 类与音频文件在同一个文件夹中,所以相对形式实际上可能是正确的,即使你的形式是资源文件夹中的音频文件会更好。 getClass() 方法将引用 Main 类及其文件夹位置,是吗?这些东西会让人感到困惑。
  • @PhilFreihofner 不,我认为我无法避免获得金徽章(但也许我错了)。无论如何,OP发布的答案似乎同意他们的问题是重复的,因此我已将其标记为这样。

标签: java javafx audio nullpointerexception embedded-resource


【解决方案1】:

正如@Slaw 所写的问题是资源路径,链接中有更多信息,

THX 色拉!

“请注意,使用 JavaFX 的媒体 API 时不需要 Java 声音导入。还请注意,您应该保持对 MediaPlayer 的强引用;如果它被垃圾收集,媒体将停止播放。至于错误,getResource(String)如果无法使用指定的路径找到资源,则返回 null。请查看 How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application? 以获得一些帮助。"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 2015-04-15
    • 2017-09-04
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多