After installing Ubuntu 19.10 recently, I've been working on projects that involve playing audio. The file type isn't overly important, wav, mp3, whatever works.
However, I've been unable to get Java to play any audio. No errors are thrown, there is just a lack of sound. Other programs can play sound, and I can open the WAV / MP3 perfectly fine, just not with a Java program.
I'm 99% sure this isn't my code's fault, as:
- The code works fine when run on Windows (I dual boot)
- After copying a stackoverflow example program and running it, still nothing happened.
Do find it amusing that the 'run anywhere' Java suffers from this problem, but I digress...
Did try a few things, listing available mixers and attempting to use those, but to no avail. It wasn't easy to search for an error with no errors thrown.
So, I then tried JavaFX, as I'd heard it had audio playing facilities. This lead to a slightly better outcome, an error was thrown; Hooray! But the error wasn't very descriptive; Rats!
My JavaFX source code is:
Media testMp3 = new Media(new File("chime2.wav").toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(testMp3);
mediaPlayer.setAutoPlay(true);
Causing:
Caused by: MediaException: UNKNOWN : com.sun.media.jfxmedia.MediaException: Could not create player! : com.sun.media.jfxmedia.MediaException: Could not create player!
at javafx.media/javafx.scene.media.MediaException.exceptionToMediaException(MediaException.java:146)
at javafx.media/javafx.scene.media.MediaPlayer.init(MediaPlayer.java:518)
at javafx.media/javafx.scene.media.MediaPlayer.<init>(MediaPlayer.java:421)
at sample.Controller.sayHelloWorld(Controller.java:20)
... 59 more
Caused by: com.sun.media.jfxmedia.MediaException: Could not create player!
at javafx.media/com.sun.media.jfxmediaimpl.NativeMediaManager.getPlayer(NativeMediaManager.java:295)
at javafx.media/com.sun.media.jfxmedia.MediaManager.getPlayer(MediaManager.java:118)
at javafx.media/javafx.scene.media.MediaPlayer.init(MediaPlayer.java:474)
... 61 more
Again, searched online but could only find threads from 2015 or so about how this is fixed in Java 9 (I'm running Java 11), and about installing libavcodec53, which I couldn't find much info on.
Things I've tried:
- Changing Java (and JavaFX version) from 14 to 11 LTS
- apt-get install ffmpeg
- apt-get install ubuntu-restricted-extras
- Installing VLC, and re-exporting the WAV file using VLC
Is there something else I need to install to get Java playing my audio?