Home > Back-end >  I am trying to implement some sounds into a JavaFX project I am working on, and running into a Illeg
I am trying to implement some sounds into a JavaFX project I am working on, and running into a Illeg

Time:11-03

So as you read, I'm trying to get some sound working on a main menu of a JavaFX program I am writing. I have put the mp3 I'm trying to play inside of a sounds folder, in a res folder in the project package. Yet when I call it similar to how I call an image for the window, I get an IllegalArgumentException error.

    Media menuMusic = new Media("sounds/menu_music.mp3");
    menuPlayer = new MediaPlayer(menuMusic);
    menuPlayer.setAutoPlay(true);
    menuPlayer.setVolume(0.8);

Here is a link to the photo of where the file is stored in the project.

And the error I get:

   Caused by: java.lang.IllegalArgumentException: uri.getScheme() == null! uri == 'sounds/menu_music.mp3'

If anyone could help me out it would be much appreciated. I've never worked with Media in JavaFX, so I could be doing this completely wrong for all I know, thanks!

CodePudding user response:

menuPlayer = new Media(Paths.get("sounds/menu_music.mp3").toUri().toString());

" java.lang.IllegalArgumentException: uri.getScheme() == null! " error using JavaFX to play audio

EDIT: As @James_D points out. The above code will fail if you try to compile it into a jar. The correct why to load a file from a resource is as follows.

menuPlayer = new Media(getClass().getResource("/sounds/menu_music.mp3").toExternalForm());
  • Related