No sound in java snap

The snap builds without any error but it throws exception with no sound when the application is launched:
Exception in thread “Thread-6” java.lang.IllegalArgumentException: No line matching interface SourceDataLine supporting format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian is supported.

my snapcraft.yaml file:

name: snake-on-the-chase # you probably want to 'snapcraft register <name>'
base: core18 # the base snap is the execution environment for this snap, with core18 Java Sound didn't work, so used core instead!!
version: '3.0' # just for humans, typically '1.2+git' or '1.3.2'
summary: Snake on the chase is a classic snake game with added flavours # 79 char long summary
description: |
  A Game with simple goal: Guide snake to the rats, feeding on them give you points and increases stamina
  Stamina of snake decreases gradually, so keep eating rats to recover
  Touching or passing throuh cactuses drains stamina drastically, avoid them
  Once all rats are eaten, you move to next level
  Game is Over if you drain all stamina 

icon: share/doc/SnakeOnTheChase/logo.png
grade: stable # must be 'stable' to release into candidate/stable channels
confinement: strict # use 'strict' once you have the right plugs and slots

apps:
  snake-on-the-chase:
    command: bin/SnakeOnTheChase
    desktop: share/applications/SnakeOnTheChase.desktop    
    plugs: [unity7, network, home, pulseaudio, alsa, x11]
    environment:
      JAVA_HOME: $SNAP/usr/lib/jvm/java-11-openjdk-amd64 
      CLASSPATH: $SNAP/SnakeOnTheChase.jar
      PATH: $SNAP/usr/lib/jvm/java-11-openjdk-amd64/bin:$PATH
      # pulseaudio
      LD_LIBRARY_PATH: ${SNAP}/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/pulseaudio

parts:
  snake-on-the-chase:
    source: .
    plugin: dump
    organize:
      SnakeOnTheChase: bin/SnakeOnTheChase      
 

    stage-packages: [openjdk-11-jre, libpulse0, x11-utils]
    build-packages:
      - ca-certificates
      - ca-certificates-java
      - openjdk-11-jre

what am I missing??

There are a few things that need to work together here to get sound working properly.

You have 3 main problems to consider:

  1. The correct snap interfaces need to be set up, these are pulseaudio, audio-playback, and audio-record (if you have record functionality). You want pulseaudio AND audio-playback in most cases, not either/or. This is because some versions of snapd will autoconnect one but not the other, for example, older Snapd might not even have an audio-playback interface, whereas newer snapd will connect audio-playback automatically but not Pulseaudio without store attestation. If you don’t have both, you will cause issues with older snapd or newer snapd.

  2. The correct libraries need to be set up in the snap. At a minimum, libpulse0 in the case of Java 8. This is because Java 8 has the backend to communicate to Pulseaudio directly. If you’re using Java 8, skip to 3) However for Java 11, Java can only talk via ALSA. Since ALSA is privileged in snapd world, you need to make use of the packages libasound2 (for ALSA directly) and ibasound2-plugins (For ALSA --> Pulse, which is what you’ll want to use in most instances) in addition to libpulse0.

Just having the packages installed isn’t enough, you need to override where the ALSA libraries look for their default configuration, as well as set their default configuration. The default configuration should look like this https://github.com/MrCarroll/runescape-snap/blob/master/utils/etc/asound.conf and you set it with ALSA_CONFIG_PATH=$SNAP/etc/asound.conf (or wherever it would be in your resultant snaps structure).

You’ll also need this in your Snapcraft build file

layout:
  /usr/lib/$SNAPCRAFT_ARCH_TRIPLET/alsa-lib:
    bind: $SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/alsa-lib
  1. The environment variables need to be set up properly so that Pulse can be reliably found. Honestly this part is easiest, just throw the Gnome Snapcraft extensions on them and let them deal with it, since they’ll also deal with a lot of other stuff for you too.

Luckily, I’ve an example of a Snapcraft definition for you that does all of this (apart from it should add the pulseaudio interface for the reasons mentioned above). Take a look here at an example of a Snapcraft build file that incorporates everything here, and you should be able to adapt your own example towards it.

The alsa-pulseaudio part will write the config file when being built, there’s an ALSA_CONFIG_PATH environment variable set up, the layouts are setup, the correct packages are included, interfaces are setup (This example is missing Pulseaudio itself, I’d recommend it), and the Gnome extensions are setting up the runtime environment to finish it all off.

( Thanks Daniel and Wimpress Reusable alsa-lib part )

1 Like

Try either running snap connect snake-on-the-chase:pulseaudio after you have it installed, or rebuild with the audio-playback plug added, which will be automatically connected on classic distros (i.e. not Ubuntu Core, which still uses the pulseaudio plug)

You probably also want to be using either the gnome-3-28 or similar extension or a desktop-helper from https://github.com/ubuntu/snapcraft-desktop-helpers