0

I am trying to create a JavaFX Mp3 project with SceneBuilder. I'm kind of new to this, and in one of the buttons, I am trying to get it to open an mp3 file, so that it will play. However, I'm getting the following error.

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

Please note that the [insert music file name] is not the actual code, but it's basically like Music Recording.mp3

And here's the actual code for the file open method in the controller for JavaFX.

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");
        }
    }

I'm still kind of new to JavaFX, so I was wondering, what might I be doing wrong?

I'm imagining that it's probably the UriException error, but if so, what should I do about it?

CLCoder
  • 1
  • 2

1 Answers1

1

Instead of mp3File.getName() try using mp3File.toURI().toString().


Example

The example below is from how I can play an video from FileChooser in javafx with some minor modifications:

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");
    }
}

Documentation

Jesse Johnson
  • 1,638
  • 15
  • 25
  • 1
    [The `Media` constructor actually requires a URI (in string form), not just a file path](https://openjfx.io/javadoc/13/javafx.media/javafx/scene/media/Media.html#%3Cinit%3E(java.lang.String)). – Slaw Dec 10 '19 at 05:02
  • @Slaw I've updated my solution and example snippet. Instead of `mp3File.getName()` **try using `mp3File.toURI().toExternalForm()`**. – Jesse Johnson Dec 10 '19 at 06:52
  • There is no `URI.toExternalForm` method (at least not in java 13). – fabian Dec 10 '19 at 06:53
  • @Slaw just upated the answer once more. Sorry for the confusion, **try using `mp3File.toURI().toString()`** – Jesse Johnson Dec 10 '19 at 07:02
  • I tried that, but now, I'm getting a java.lang.reflect.InvocationTargetException – CLCoder Dec 11 '19 at 05:21
  • @CLCoder An `InvocationTargetException` is never the root cause of the error. You have to look at the cause(s) in the stack trace. For instance, the exception in your current question was ultimately an `IllegalArgumentException`. See [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) for more information. – Slaw Dec 11 '19 at 10:06
  • Sorry it's actually an Illegal Access Error Caused by: java.lang.IllegalAccessError: class com.sun.media.jfxmediaimpl.NativeMediaManager (in unnamed module @0x42fd6561 – CLCoder Dec 11 '19 at 18:44
  • @CLCoder Make sure to include `javafx.media` in your `--add-modules` option. That, or make your code modular and add the appropriate `requires` directives to the module-info file. See https://stackoverflow.com/questions/53237287/module-error-when-running-javafx-media-application – Slaw Dec 11 '19 at 23:21
  • @CLCoder Also see [How do comment @ replies work?](https://meta.stackexchange.com/questions/43019/how-do-comment-replies-work). – Slaw Dec 11 '19 at 23:23
  • @Slaw Lol, sorry about that. As far as the code is concerned, yep, I've tried that. There's still more work to be done, but I am getting there. – CLCoder Dec 12 '19 at 01:11