0

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.Infos.

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).

Roy Ash
  • 1,078
  • 1
  • 11
  • 28
  • Did you try with javaFX mediaplayer? It is just one line of code and it is works much better with the default config – Volzy Nov 01 '22 at 21:35
  • @Volzy correct me if I wrong, but JavaFX doesn't included in JRE anymore, correct? can you give an example? – Roy Ash Nov 01 '22 at 21:51
  • 1
    it's correct but you said you are using maven. You can add it as maven dependency – Volzy Nov 01 '22 at 22:01
  • now I'm trying to figure out how to open the WAV file from resources directory, (`Media` constructor only recieves String of the location of the file) – Roy Ash Nov 01 '22 at 22:03
  • @Volzy as I can see, it guides me toward a GUI application, I want my application to be a CLI app. I keep getting errors like `Error: JavaFX runtime components are missing, and are required to run this application` after I extended my class to be type of `Application` even after I wrote `primaryStage.show();` inside the `start` method. – Roy Ash Nov 01 '22 at 22:22
  • In addition, it seems that it make me dependent on `javafx-maven-plugin` and I can only run it via `mvn javafx:run` instead of `java -jar .jar` and it's not acceptable – Roy Ash Nov 01 '22 at 23:07
  • Yes javaFx include GUI and cli components like mediaplayer. You get those errors because you must download the javaFX runtime and add it to the classpath as vm argument – Volzy Nov 02 '22 at 08:23
  • 1
    You can not read an input stream multiple times. It’s a bad idea to use the same input stream for multiple playback attempts when you don’t know whether the previous attempt(s) have read data from it (or how much). – Holger Nov 02 '22 at 11:17
  • @Holger playing the clip multiple times works, this is not the problem, the problem is about the mixers only. – Roy Ash Nov 02 '22 at 12:02
  • @Volzy I'm building a standalone jar that need to work on any (main) OS, in addition I'm already creating jar with dependencies so I can't demand that the users will need to install additional runtimes and play with classpath parameters. if you want to see what I'm already doing, the project is in https://github.com/ashr123/red-alert-listener – Roy Ash Nov 02 '22 at 12:08
  • Well i don't see the problem with dependency if it's not a malware you can still create an application easy to launch to launch the complex application. Java it's not "plug and play" it's an old language if you want plug and play support you should go for a modern language – Volzy Nov 02 '22 at 12:29
  • I know it's not a malware, I just want to keep it simple for people to download the jar-with-dependencies jar and run it via `java -jar`, it worked so far.. it still works on my MacOS and on Windows 10, I don't have Ubuntu installed on "regular" computer so I didn't check that case – Roy Ash Nov 02 '22 at 13:16
  • I repeat. Try if it's a raspberry issue, trying on normal pc. Otherwise consider using mediaplayer. I don't see usually people that use Clip. I see everyone that use the javafx mediaplayer. Or you if you don't like javafx then find another player. There are many library to play music. If you need to run on linux you can launch a music process with ProcessBuilder – Volzy Nov 02 '22 at 16:55
  • This is indeed seems to be a raspberry pi issue (on Ubuntu 22.10 at least), on other computers it works just fine, the goal is to find more long-term and **stable** lightweight solution that doesn't needed to be run with extra flags and parameters or tinkering with environment variables and packages outside the jar with dependencies. – Roy Ash Nov 02 '22 at 17:40

0 Answers0