Sound in java: Best practice 2022

So, my java application is silent when run as snap. I have added plugs for audio-playback + pulseaudio (and connect it)

Then I found a couple of related threads here, is this still valid in late 2022? (The links seems to be broken though)

The java version you use would determine if you need to setup alsa or not, the instructions seem pretty much still valid.

I can’t edit the post on the linked thread anymore, the only thing I’d change now is in regards to point 1; it’s been a longtime since the Pulseaudio interface has been deprecated and as of now I’d run with audio-playback and audio-record exclusively.

Otherwise, nothing else has changed since.

Tl;Dr

  • Correct interfaces (audio-playback)
  • Use extensions to get the socket setup
  • If Java 8, stage libpulse (might be in the newest extensions automatically?)
  • If Java 9+, additionally stage alsa, use layouts and ALSA_CONFIG_PATH variable to use alsa-pulse backend.

This isn’t particularly a Java specific problem, the only Java nuance here, is that Java 8 from the Ubuntu/Debian repositories has native Pulseaudio support whereas 9+ do not, and this can be confusing since you’d usually assume newer versions would have more features/compatibility but isn’t true in this specific scenario.

I’m aiming for 17 or 19.

Thanks @James-Carroll, I’ll give it a try then. :slight_smile:

it should be in all extensions (core20/22 at least), there should be no need to stage libpulse nowadays …

It works! Now I got sound in my snap. :slight_smile: Not sure of the contents of asound.conf though, should I do anything different?

name: yaya
title: Yaya
base: core22
version: '0.1-SNAPSHOT'
summary: Yet Another Yahtzeeesque Application
description: |
    **TODO write a description
grade: stable
architectures: [ amd64 ]
confinement: strict
icon: snap/gui/yaya.png
license: Apache-2.0

layout:
  /usr/lib/$SNAPCRAFT_ARCH_TRIPLET/alsa-lib:
    bind: $SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/alsa-lib

apps:
  yaya:
    command: yaya/bin/yaya
    extensions: [gnome]
    plugs:
        - audio-playback
    environment:
      ALSA_CONFIG_PATH: $SNAP/etc/asound.conf

parts:
  yaya:
    plugin: dump
    source: /root/project/yaya-$SNAPCRAFT_PROJECT_VERSION-linux.zip
    source-type: zip
    stage-packages:
      - libasound2
      - libasound2-plugins

  config:
    after:
      - yaya
    plugin: nil
    override-prime: |
        #sed -i 's/${HOME}\/.${APPNAME}/${SNAP_USER_COMMON}\/.yaya/' /root/prime/yaya/bin/yaya
        #sed -i 's/${HOME}\/.cache\/${APPNAME}/${SNAP_USER_COMMON}\/.cache\/yaya/' /root/prime/yaya/bin/yaya
        rm /root/prime/yaya/platform/lib/*.dll
        rm /root/prime/yaya/platform/lib/*.exe
        
  apt:
    plugin: nil
    override-build: |
        apt install openjdk-19-jdk -y

  jlink:
    plugin: nil
    after: [apt]
    override-build: |
        rm -rf $SNAPCRAFT_PRIME/yaya/jre

        jlink --no-header-files \
            --no-man-pages \
            --strip-debug \
            --compress=1 \
            --add-modules java.base,java.datatransfer,java.xml,java.prefs,java.desktop,java.instrument,java.logging \
            --output $SNAPCRAFT_PRIME/yaya/jre

        strip -p --strip-unneeded $SNAPCRAFT_PRIME/yaya/jre/lib/server/libjvm.so

  alsa-pulseaudio:
    plugin: dump
    source: .
    override-pull: |
      mkdir etc -p
      cat > etc/asound.conf <<EOF
      pcm.!default {
          type pulse
          fallback "sysdefault"
          hint {
              show on
              description "Default ALSA Output (currently PulseAudio Sound Server)"
          }
      }
      ctl.!default {
          type pulse
          fallback "sysdefault"
      }
      EOF
1 Like

I forgot to mention that it’s published now.

1 Like