gpt4 book ai didi

java - 如何让我的 JavaFX 程序加载文件?

转载 作者:行者123 更新时间:2023-12-02 09:12:51 28 4
gpt4 key购买 nike

我正在尝试使用 SceneBuilder 创建 JavaFX Mp3 项目。 我对此有点陌生,在其中一个按钮中,我试图让它打开一个 mp3 文件,以便它能够播放。但是,我收到以下错误。

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3862)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2590)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
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:174)
at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1784)
... 28 more
Caused by: java.lang.IllegalArgumentException: java.net.URISyntaxException: Illegal character in path at index 7: [insert music file name]
at javafx.scene.media.Media.<init>(Media.java:385)
at sample.Controller.fileImageBtnPressed(Controller.java:56)
... 39 more

请注意,[插入音乐文件名]不是实际的代码,但它基本上类似于 Music Recording.mp3

这是 JavaFX Controller 中文件打开方法的实际代码。

public void fileImageBtnPressed() {
fileOpenImageView.setImage(fileButtonPressed);
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter fileExtension = new FileChooser.ExtensionFilter("music files", "*.mp3");
fileChooser.getExtensionFilters().add(fileExtension);
fileChooser.setTitle("Choose an MP3 file");
File mp3File = fileChooser.showOpenDialog(new Stage());
if (mp3File != null) {
// listView.getItems().add(mp3File.getName());
Media mediaPlayer = new Media(mp3File.getName());
MediaPlayer mP = new MediaPlayer(mediaPlayer);
listView.getItems().add(mP);
} else {
System.out.println("Incorrect file");
}
}

我对 JavaFX 还很陌生,所以我想知道,我可能做错了什么?

我想象这可能是 UriException 错误,但如果是这样,我该怎么办?

最佳答案

尝试使用 mp3File.toURI().toString() 代替 mp3File.getName()

<小时/>

示例

下面的例子来自how I can play an video from FileChooser in javafx有一些小的修改:

public void fileImageBtnPressed() {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter fileExtension = new FileChooser.ExtensionFilter("select your music file (*.mp3)", "*.mp3");
fileChooser.getExtensionFilters().add(fileExtension);
File mp3File= fileChooser.showOpenDialog(new Stage());
if (mp3File != null) {
Media source = new Media(mp3File.toURI().toString());
MediaPlayer player = new MediaPlayer(source);
listView.getItems().add(player);
player.play();
} else {
System.out.println("Incorrect file");
}
}
<小时/>

文档

关于java - 如何让我的 JavaFX 程序加载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59259813/

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