1

I am currently trying to play different sounds in my program that is built on JavaFX. I have tried creating a Media and AudioClip but both just make exceptions. The file is indeed valid because AudioSystem sees the file. Here is my current loading code and the terminal output:

Code:

File f = new File("src/main/resources/Sounds/gameOver.wav");

try {
    System.out.println(AudioSystem.getAudioFileFormat(f));
} catch (UnsupportedAudioFileException e) {
    e.printStackTrace();
}

System.out.println(f.toURI().toURL().toExternalForm());
AudioClip gameOver = new AudioClip(f.toURI().toURL().toExternalForm());

Terminal output:

WAVE (.wav) file, byte length: 850484, data format: PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian, frame length: 212610
file:/C:/Users/Fred/IdeaProjects/GameEngine/src/main/resources/Sounds/gameOver.wav
Exception in Application start method
  • 1
    Show the complete stack trace. It looks like the exception is unrelated to the code you posted. Also, and kind of off-topic, while the file-based URL will work running in your browser, it won’t work when you bundle as a jar file. – James_D May 20 '21 at 01:39
  • @James_D I apologize, but the terminal isn't showing me the complete error, I do not know why. The exception happens only if the line AudioClip is there. What is the correct way of passing the URL, for it to be bundled as a jar? – Frederik Tomlain May 20 '21 at 09:14
  • 2
    "_What is the correct way of passing the URL_" – In your case using `getClass().getResource("/Sounds/gameOver.wav").toExternalForm()` should work, but check out [this Q&A](https://stackoverflow.com/questions/61531317/) for more information. As for your problem: Can you share the WAV file and a proper [mre] demonstrating the problem? Perhaps if someone else runs the code they might be able to get the full stack trace. – Slaw May 20 '21 at 11:09
  • If you’re not seeing the full stack trace, you’re almost certainly suppressing it somewhere in your code, eg with a `catch` block that just passes the exception to `System.out.println()`. Creating and posting a [mre] is the best way to get help. – James_D May 20 '21 at 11:53
  • I would like to thank everyone that helped, I managed to print out the error in a different class and the error was that I haven't added javafxmedia into my VM options. Should I answer my own question? Or I can give credit to both of you? Thank you – Frederik Tomlain May 20 '21 at 12:04
  • I assume that means you were getting an `IllegalAccessError`. Based on that assumption I've closed this question as a duplicate. If that was not the error you were getting, or you don't feel the duplicate actually answers your question, then please let me know and I'll reopen the question. In any case, you should [edit] your question to add the stack trace you are getting. – Slaw May 20 '21 at 21:22

0 Answers0