3

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:

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?

2 Answers2

1

I have the same problem with same environment.But today I tried running my JavaFX application on JDK 8 and it has done with success.

Download the Oracle JDK 1.8.0_251 from the following url: https://www.oracle.com/java/technologies/javase-downloads.html

1

The problem is in java's "sound.properties" config file. I'm now on Ubuntu 20.04, with java-11-openjdk, so, edited /etc/java-11-openjdk/sound.properties and added the following lines at the end:

javax.sound.sampled.Clip=com.sun.media.sound.DirectAudioDeviceProvider
javax.sound.sampled.Port=com.sun.media.sound.PortMixerProvider
javax.sound.sampled.SourceDataLine=com.sun.media.sound.DirectAudioDeviceProvider
javax.sound.sampled.TargetDataLine=com.sun.media.sound.DirectAudioDeviceProvider

For deep in, read: https://keithp.com/blogs/Java-Sound-on-Linux/

leoperbo
  • 763