Java snap application is not able to play sounds (core24)

Hello,

could someone please help me to understand why my Java snap is not able to play sounds? If I create a deb package with a custom runtime (jpackage), it works fine.

I am sharing the yml file:

name: fromgtog
version: '7.0.0'
summary: Clone ALL GitHub/Gitea/Local to GitHub/Gitea.
description: |
  Helps to clone all repositories from GitHub on Gitea/Local and vice versa.
grade: stable
confinement: strict
base: core24
icon: snap/gui/icon.png
title: FromGtoG
website: https://andre-i.eu
issues: https://github.com/goto-eof
license: MIT
compression: xz
slots:
  fromgtog-dbus:
    interface: dbus
    bus: session
    name: it.es5.fromgtog.dbus
apps:
  fromgtog:
    environment:
      LD_LIBRARY_PATH: $LD_LIBRARY_PATH:$SNAP_DESKTOP_RUNTIME/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/libproxy:$SNAP_DESKTOP_RUNTIME/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/gio/modules
    command: executor
    extensions: [ gnome ]
    plugs:
      - network
      - x11
      - browser-support
      - unity7
      - home
      - desktop
      - desktop-legacy
      - removable-media
      - wayland
      - network-bind
      - pulseaudio
      - alsa
    slots:
      - fromgtog-dbus
platforms:
  amd64:
    build-on: [ amd64 ]
    build-for: [ amd64 ]
  arm64:
    build-on: [ arm64 ]
    build-for: [ arm64 ]
parts:
  wrapper:
    plugin: dump
    source: snap/local
    source-type: local
  application:
    plugin: maven
    source: .
    build-packages:
      - openjdk-21-jdk
      - maven
    stage-packages:
      - openjdk-21-jre
      - glib-networking
      - libgtk-3-0
      - xdg-utils
    override-prime: |
      snapcraftctl prime
      rm -vf usr/lib/jvm/java-*-openjdk-*64/lib/security/blacklisted.certs

Have a look here:

In summary, Java is attempting to use ALSA directly and whilst snap can support this, it’s better to use PulseAudio (which might also actually be Pipewire, to your snap, they’re one and the same).

So you need to configure the environment just a little to get the ALSA/Pulse wrapper going, which would be applicable to both Java but other platforms in the same position of only having ALSA support natively.

Thanks James for your response.

I have updated my yml file:

name: fromgtog
version: '7.0.0'
summary: Clone ALL GitHub/Gitea/Local to GitHub/Gitea.
description: |
  Helps to clone all repositories from GitHub on Gitea/Local and vice versa.
grade: stable
confinement: strict
base: core24
icon: snap/gui/icon.png
title: FromGtoG
website: https://andre-i.eu
issues: https://github.com/goto-eof
license: MIT
compression: xz
slots:
  fromgtog-dbus:
    interface: dbus
    bus: session
    name: it.es5.fromgtog.dbus
apps:
  fromgtog:
    environment:
      ALSA_CONFIG_PATH: $SNAP/etc/asound.conf
      LD_LIBRARY_PATH: $LD_LIBRARY_PATH:$SNAP_DESKTOP_RUNTIME/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/libproxy:$SNAP_DESKTOP_RUNTIME/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/gio/modules
    command: executor
    extensions: [ gnome ]
    plugs:
      - network
      - x11
      - browser-support
      - unity7
      - home
      - desktop
      - desktop-legacy
      - removable-media
      - wayland
      - network-bind
      - pulseaudio
      - alsa
    slots:
      - fromgtog-dbus
parts:
  wrapper:
    plugin: dump
    source: snap/local
    source-type: local
  alsa-config:
    plugin: dump
    source: snap/audio/asound.conf
    source-type: file
    organize:
      asound.conf: etc/asound.conf
    prime:
      - etc/asound.conf
  application:
    plugin: maven
    source: .
    build-packages:
      - openjdk-21-jdk
      - maven
    stage-packages:
      - openjdk-21-jre
      - glib-networking
      - libgtk-3-0
      - xdg-utils
      - libasound2
      - libasound2-plugins
      - alsa-utils
    after:
      - alsa-config
    prime:
      - etc/asound.conf
    override-prime: |
      snapcraftctl prime
      rm -vf usr/lib/jvm/java-*-openjdk-*64/lib/security/blacklisted.certs

But i am still obtaining the same error:

No line matching interface Clip supporting format PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian is supported.
        at java.desktop/javax.sound.sampled.AudioSystem.getLine(AudioSystem.java:423)
        at java.desktop/javax.sound.sampled.AudioSystem.getClip(AudioSystem.java:459)

event if the .wav file is little-endian. I want to reaffirm that from itellij the sounds works fine.

Do you observe any denials in the logs (journal specifically) when the app attempts to play the file?

Out of curiosity, can you download a random mp3 file and try e.g.,

> snap run --shell fromgtog
> aplay $SNAP_REAL_HOME/test.mp3

This should be able to play the file and if it can’t, it suggests something more confinement related. If it can, then it suggests the problem is more specifically Java related, since aplay would use the same Alsa/Pulse wrappers when it’s set up correctly.

Alongside the logs above (which you can try getting using the snappy-debug snap, running in a second terminal window alongside you trying the above), it should hopefully help determine what’s going on.

Hello James, the result is the following:

> aplay success.wav 
> aplay: command not found

I fixed a little bit the yml file, and succeed to have aplay working:

Unknown PCM sysdefault
aplay: main:834: audio open error: No such file or directory

The new version of the yml:

name: fromgtog
version: '7.0.0'
summary: Clone ALL GitHub/Gitea/Local to GitHub/Gitea.
description: |
  Helps to clone all repositories from GitHub on Gitea/Local and vice versa.
grade: stable
confinement: strict
base: core24
icon: snap/gui/icon.png
title: FromGtoG
website: https://andre-i.eu
issues: https://github.com/goto-eof
license: MIT
compression: xz
slots:
  fromgtog-dbus:
    interface: dbus
    bus: session
    name: it.es5.fromgtog.dbus
apps:
  fromgtog:
    environment:
      ALSA_CONFIG_PATH: $SNAP/etc/asound.conf
      LD_LIBRARY_PATH: $LD_LIBRARY_PATH:$SNAP_DESKTOP_RUNTIME/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/libproxy:$SNAP_DESKTOP_RUNTIME/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/gio/modules
    command: executor
    extensions: [ gnome ]
    plugs:
      - network
      - x11
      - browser-support
      - unity7
      - home
      - desktop
      - desktop-legacy
      - removable-media
      - wayland
      - network-bind
      - pulseaudio
      - alsa
      - alsa-utils
    slots:
      - fromgtog-dbus
parts:
  wrapper:
    plugin: dump
    source: snap/local
    source-type: local
  alsa-config:
    plugin: dump
    source: snap/local/asound.conf
    source-type: file
    organize:
      asound.conf: etc/asound.conf
    prime:
      - etc/asound.conf
  application:
    plugin: maven
    source: .
    build-packages:
      - openjdk-21-jdk
      - maven
    stage-packages:
      - openjdk-21-jre
      - glib-networking
      - libgtk-3-0
      - xdg-utils
      - libasound2
      - libasound2-plugins
      - alsa-utils
    after:
      - alsa-config
#    prime:
#      - etc/asound.conf
    override-prime: |
      snapcraftctl prime
      rm -vf usr/lib/jvm/java-*-openjdk-*64/lib/security/blacklisted.certs


Hello, no i did not observe any denials

I have a new update: I tried on my local the following snapcraft.yml

name: fromgtog
version: '7.0.1'
summary: Clone ALL GitHub/Gitea/Local to GitHub/Gitea.
description: |
  Helps to clone all repositories from GitHub on Gitea/Local and vice versa.
grade: stable
confinement: strict
base: core24
icon: snap/gui/icon.png
title: FromGtoG
website: https://andre-i.eu
issues: https://github.com/goto-eof
license: MIT
compression: xz
slots:
  fromgtog-dbus:
    interface: dbus
    bus: session
    name: it.es5.fromgtog.dbus
apps:
  fromgtog:
    environment:
      ALSA_CONFIG_PATH: $SNAP/etc/asound.conf
      LD_LIBRARY_PATH: $LD_LIBRARY_PATH:$SNAP_DESKTOP_RUNTIME/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/libproxy:$SNAP_DESKTOP_RUNTIME/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/gio/modules
    command: executor
    extensions: [ gnome ]
    plugs:
      - network
      - x11
      - browser-support
      - unity7
      - home
      - desktop
      - desktop-legacy
      - removable-media
      - wayland
      - network-bind
      - pulseaudio
      - alsa
    slots:
      - fromgtog-dbus
parts:
  wrapper:
    plugin: dump
    source: snap/local
    source-type: local
  alsa-config:
    plugin: dump
    source: snap/local/asound.conf
    source-type: file
    organize:
      asound.conf: etc/asound.conf
    prime:
      - etc/asound.conf
  application:
    plugin: maven
    source: .
    build-packages:
      - openjdk-21-jdk
      - maven
    stage-packages:
      - openjdk-21-jre
      - glib-networking
      - libgtk-3-0
      - xdg-utils
      - libasound2
      - libasound2-plugins
      - alsa-utils
    after:
      - alsa-config
    override-prime: |
      snapcraftctl prime
      rm -vf usr/lib/jvm/java-*-openjdk-*64/lib/security/blacklisted.certs


After enabling manually the connections it worked:

sudo snap connect fromgtog:pulseaudio
sudo snap connect fromgtog:alsa
snap connections fromgtog
Interface               Plug                      Slot                            Notes
alsa                    fromgtog:alsa             -                               -
browser-support         fromgtog:browser-support  :browser-support                -
content[gnome-46-2404]  fromgtog:gnome-46-2404    gnome-46-2404:gnome-46-2404     -
content[gpu-2404]       fromgtog:gpu-2404         mesa-2404:gpu-2404              -
content[gtk-3-themes]   fromgtog:gtk-3-themes     gtk-common-themes:gtk-3-themes  -
content[icon-themes]    fromgtog:icon-themes      gtk-common-themes:icon-themes   -
content[sound-themes]   fromgtog:sound-themes     gtk-common-themes:sound-themes  -
dbus                    -                         fromgtog:fromgtog-dbus          -
desktop                 fromgtog:desktop          :desktop                        -
desktop-legacy          fromgtog:desktop-legacy   :desktop-legacy                 -
gsettings               fromgtog:gsettings        :gsettings                      -
home                    fromgtog:home             :home                           -
network                 fromgtog:network          :network                        -
network-bind            fromgtog:network-bind     :network-bind                   -
opengl                  fromgtog:opengl           :opengl                         -
pulseaudio              fromgtog:pulseaudio       -                               -
removable-media         fromgtog:removable-media  -                               -
unity7                  fromgtog:unity7           :unity7                         -
wayland                 fromgtog:wayland          :wayland                        -
x11                     fromgtog:x11              :x11                            -

I discovered that the interface pulseaudio was deprecated: The pulseaudio interface

So i replaced it with sound-playback and now my application is able to play sounds!

Thank you again James! You have led me on the right path.

For completeness I am sharing the working yaml file:

name: fromgtog
version: '7.0.1'
summary: Clone ALL GitHub/Gitea/Local to GitHub/Gitea.
description: |
  Helps to clone all repositories from GitHub on Gitea/Local and vice versa.
grade: stable
confinement: strict
base: core24
icon: snap/gui/icon.png
title: FromGtoG
website: https://andre-i.eu
issues: https://github.com/goto-eof
license: MIT
compression: xz
slots:
  fromgtog-dbus:
    interface: dbus
    bus: session
    name: it.es5.fromgtog.dbus
apps:
  fromgtog:
    environment:
      ALSA_CONFIG_PATH: $SNAP/etc/asound.conf
      LD_LIBRARY_PATH: $LD_LIBRARY_PATH:$SNAP_DESKTOP_RUNTIME/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/libproxy:$SNAP_DESKTOP_RUNTIME/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/gio/modules
    command: executor
    extensions: [ gnome ]
    plugs:
      - network
      - x11
      - browser-support
      - unity7
      - home
      - desktop
      - desktop-legacy
      - removable-media
      - wayland
      - network-bind
      - audio-playback
      - alsa
    slots:
      - fromgtog-dbus
platforms:
  amd64:
    build-on: [ amd64 ]
    build-for: [ amd64 ]
  arm64:
    build-on: [ arm64 ]
    build-for: [ arm64 ]
parts:
  wrapper:
    plugin: dump
    source: snap/local
    source-type: local
  alsa-config:
    plugin: dump
    source: snap/local/asound.conf
    source-type: file
    organize:
      asound.conf: etc/asound.conf
    prime:
      - etc/asound.conf
  application:
    plugin: maven
    source: .
    build-packages:
      - openjdk-21-jdk
      - maven
    stage-packages:
      - openjdk-21-jre
      - glib-networking
      - libgtk-3-0
      - xdg-utils
      - libasound2
      - libasound2-plugins
      - alsa-utils
    after:
      - alsa-config
    override-prime: |
      snapcraftctl prime
      rm -vf usr/lib/jvm/java-*-openjdk-*64/lib/security/blacklisted.certs


While in Java I do something like that to load the clip:

    private Clip loadClip(String soundFilename) {
        try {
            InputStream soundStream = SoundPlayer.class.getResourceAsStream(soundFilename);
            URL soundURL = getClass().getClassLoader().getResource(soundFilename);
            if (soundURL == null) {
                logger.error("File not found: {}", soundFilename);
                return null;
            }
            logger.info("Loading file: {}", soundFilename);
            AudioInputStream audioStream = AudioSystem.getAudioInputStream(soundURL);
            Clip clip = AudioSystem.getClip();
            clip.open(audioStream);
            audioStream.close();
            return clip;
        } catch (RuntimeException | UnsupportedAudioFileException | IOException |
                 LineUnavailableException e) {
            logger.error("SoundPlayer failed to load clip", e);
        }
        return null;
    }
2 Likes