提问人:Patryk Kobyłka 提问时间:1/13/2022 最后编辑:Andrew ThompsonPatryk Kobyłka 更新时间:1/15/2022 访问量:561
尝试使用 JavaFX 播放音频文件 [重复]
Trying play audio file with JavaFX [duplicate]
问:
我正在尝试在我的 JavaFX 应用程序中播放音频文件,代码对我来说看起来不错,但我遇到错误
我认为问题出在第 28 行:
“原因:java.lang.NullPointerException:无法调用”java.net.URL.toString()“,因为”resource“为空 在样品中。Main.start(Main.java:28)”
为什么 resource 为 null?在我的文件结构中,我得到了资源包,但是音频文件在哪里并不重要,错误是一样的
我是编码新手,所以我不知道如何准确阅读这些错误......如果有人能帮忙,那就太好了
代码和错误是爱
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
谢谢任何帮助!
答:
2赞
Patryk Kobyłka
1/15/2022
#1
由于@Slaw所写的问题是资源路径,因此在链接中提供了更多信息,
谢谢!
“请注意,在使用 JavaFX 的媒体 API 时,您不需要 Java Sound 导入。另请注意,您应该保持对 MediaPlayer 的强引用;如果是垃圾回收,媒体将停止播放。对于错误,如果使用指定路径找不到资源,getResource(String) 将返回 null。查看如何确定 JavaFX 应用程序所需的 FXML 文件、CSS 文件、图像和其他资源的正确路径?寻求一些帮助。
评论
MediaPlayer
getResource(String)
"test2.mp3"
"/sample/test2.mp3"