Adding manually

You can add this alsa-lib configuration to your project manually. This method is limited to the Ubuntu-distributed version of ALSA only, because the changes required for compiling ALSA from source are too extensive to detail succinctly here. Such complex changes are the reason that sc-jsonnet was created.

Howto

To use Ubuntu’s ALSA, copy the following part into your snapcraft.yaml:

parts:
  alsa-mixin:
    plugin: dump
    source: https://github.com/diddlesnaps/snapcraft-alsa.git
    source-subdir: snapcraft-assets
    build-packages:
      - libasound2-dev
    stage-packages:
      - libasound2
      - libasound2-plugins
      - yad
    stage:
      # restrict to only audio-related files - you need to ensure
      # that gtk3 is staged for yad to work correctly, to prompt
      # users to connect the alsa plug or proceed with pulseaudio.
      #
      # This helps prevent symbol conflicts in situations where
      # you're using a non-default library, such as those that the
      # gnome-3-34 extension for core18 provides.
      - etc/asound.conf
      - snap/command-chain/alsa-launch
      - usr/bin/yad*
      - usr/lib/$SNAPCRAFT_ARCH_TRIPLET/alsa-lib
      - usr/lib/$SNAPCRAFT_ARCH_TRIPLET/libasound*
      - usr/lib/$SNAPCRAFT_ARCH_TRIPLET/libdnsfile*
      - usr/lib/$SNAPCRAFT_ARCH_TRIPLET/libFLAC*
      - usr/lib/$SNAPCRAFT_ARCH_TRIPLET/libjack*
      - usr/lib/$SNAPCRAFT_ARCH_TRIPLET/libpulse*
      - usr/lib/$SNAPCRAFT_ARCH_TRIPLET/libsamplerate*
      - usr/lib/$SNAPCRAFT_ARCH_TRIPLET/libspeex*
      - usr/lib/$SNAPCRAFT_ARCH_TRIPLET/libvorbis*
      - usr/lib/$SNAPCRAFT_ARCH_TRIPLET/pulseaudio

Next add a layout definition so that the ALSA library can find the pulseaudio plugin, along with any other plugins it might desire to load:

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

Now, add after: [alsa-mixin] to any parts that require ALSA:

parts:
  ... # other parts here

  my-app:
    after: [alsa-mixin]
    ... # rest of my-app part here

Finally, add snap/command-chain/alsa-launch to the command chain of any apps that need to use the sound system and specify the alsa, audio-playback plug, and the audio-record plug if recording is required e.g.

apps:
  my-app:
    command-chain: ["snap/command-chain/alsa-launch"]
    command: bin/my-app
    plugs: [alsa, audio-playback, audio-record]

Always forcing PulseAudio

If you want your users to never be prompted and to always force the use of PulseAudio, you may specify an environment block in your snapcraft.yaml:

environment:
  ALWAYS_USE_PULSEAUDIO: '1'