I'm using Raspberry Pi with Ubuntu 22.10 and Java 19.
I'm trying to play a sound with the following code:
private static void testMixers() throws IOException, UnsupportedAudioFileException
{
try (AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new BufferedInputStream(Objects.requireNonNull(Listener.class.getResourceAsStream("/alarmSound.wav")))))
{
final Scanner scanner = new Scanner(System.in);
for (Mixer.Info info : AudioSystem.getMixerInfo())
{
System.out.println("Testing sound for mixer \"" + info + "\"...");
try (Clip clip = AudioSystem.getClip(info))
{
clip.open(audioInputStream);
clip.setFramePosition(0);
clip.start();
System.out.println("Did you hear any sound? (true|false)");
if (Boolean.parseBoolean(scanner.nextLine()))
return;
}
catch (Exception e)
{
System.out.println(e);
}
}
}
System.out.println("Mixer not found!");
}
alarmSound.wav
is under src/main/resources
(this is a Maven project).
AudioSystem.getMixerInfo()
gives me the following Mixer.Info[]
:
Port Headphones [hw:0], version 5.19.0-1006-raspi
Port vc4hdmi0 [hw:1], version 5.19.0-1006-raspi
Port vc4hdmi1 [hw:2], version 5.19.0-1006-raspi
Headphones [default], version 5.19.0-1006-raspi
Headphones [plughw:0,0], version 5.19.0-1006-raspi
vc4hdmi0 [plughw:1,0], version 5.19.0-1006-raspi
vc4hdmi1 [plughw:2,0], version 5.19.0-1006-raspi
Only the Headphones
Mixers don't give the error:
java.lang.IllegalArgumentException: Line unsupported: interface Clip supporting format PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian
I didn't tested the headphone jack.
This is completely different from Ubuntu 22.04 which gave more Mixer.Info
s.
Is there any workaround? it seems that every Ubuntu version requires different workaround for being able to play a sound that can be heard not through the headphones (but from bluetooth speakers or via HDMI for example).