DaMan56100
DaMan56100

Reputation: 65

Java plays audio on Windows but not on Ubuntu

I've been writing for fun a Java program to play some tunes in .wav format.

Whilst developing, the audio played fine on my Windows machine, but however when trying the same code on Ubuntu the audio does not play. There are no errors logged to the console, I press the 'Play' button and nothing happens.

Here's the code I've been using, also contains some logging code:

try {
    System.out.println("All mixers:");
    for (Mixer.Info m : AudioSystem.getMixerInfo()) {
        System.out.println(m.getName());
    }

    System.out.println("Default mixer: " + AudioSystem.getMixer(null).getMixerInfo());

    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("bsc.wav"));
    Clip clip = AudioSystem.getClip();
    clip.open(audioInputStream);
    clip.start();
} catch (UnsupportedAudioFileException ex) {
    ex.printStackTrace();
} catch (IOException ex) {
    ex.printStackTrace();
} catch (LineUnavailableException ex) {
    ex.printStackTrace();
}

This outputs:

All mixers:
Port HDMI [hw:0]
Port PCH [hw:1]
Port Snowball [hw:2]
HDMI [plughw:0,3]
HDMI [plughw:0,7]
HDMI [plughw:0,8]
PCH [plughw:1,0]
PCH [plughw:1,1]
PCH [plughw:1,2]
Snowball [plughw:2,0]
Default mixer: HDMI [plughw:0,3], version 5.3.0-51-generic

Thought this is more likely an issue with my setup than the code, so thought this was a fit for superuser. Did also write some code to try the other available mixers, but none of those worked either.

Looking around, did hear something about needing to install JMF but couldn't find whether it's really the solution.

Running Ubuntu 19.10 and Java 11.0.7 OpenJDK.

Edit1; found this page on StackOverflow about the Java Sound. Tried running the example code but also to no avail...

Upvotes: 1

Views: 549

Answers (1)

Phil Freihofner
Phil Freihofner

Reputation: 7910

Here are a couple things to investigate and (probably) rule out.

  • Is your button JavaFX? If so, have you verified JavaFX is working?
  • Have you verified the address of the File object being created?
  • Try using URL as your AudioSystem.getAudioInputStream argument, setting it with class.getResource(URL).
  • Use the ais to get a Dataline.Info object as part of the process.

The code for using Dataline.Info follows:

AudioInputStream ais = AudioSystem.getAudioInputStream(url);
DataLine.Info info = new DataLine.Info(Clip.class, ais.getFormat());
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(ais);

You might inspect the Clip in the debugger before playing it. Check the .audioData property, to verify the data was loaded into memory. You can also verify the audio format here.

I just went through a rigamarole trying to get a jar file made in Windows to execute on Ubuntu 18.04. It turned out to be JavaFX related actually. Once I fixed that, the program ran (including its audio). I always use URL when working with audio assets.

Upvotes: 2

Related Questions