Snap example with audio-playback and play (sox) command

I am trying to enable audio playback to my snap and so far without success. My application is just executing available command line play command to play mp3 files. I know that I should use audio-playback interface instead of deprecated pulseaudio interface and that audio-playback enables only pulseaudio and not ALSA. For ALSA support there are additional more complex steps. As my application is not using ALSA interface directly and it executes only play command then it would be sufficient to do any play command which can play directly through pulseaudio. Originally I used ‘play’ command which is in fact SoX player. It supports multiple sound backends as well as pulseaudio. For that I added additional packages:
stage-packages:
- sox
- libsox-fmt-mp3
- libsox-fmt-pulse
- libpulse0
See https://app.zdechov.net/c-evo/browser/trunk/Install/snap/snapcraft.yaml for full snap configuration.
When play command is executed then it reports “/snap/c-evo/3/usr/bin/play FAIL sox: Sorry, there is no default audio device configured”.
So I guess I am still missing some pulseaudio configuration in my snap so SoX can’t connect to pulseaudio.

Basically I need simple way to play mp3 files inside snap. Another requirement would be to not make snap much bigger so some low space demanding solution would be nice. There are other players which could be used. But for example ffplay would probably take too much libraries with itself. Another play tool is mpg123/mpg321.

Are there some example snaps which can play audio files through pulseaudio and don’t take too much space? Thanks to github ability to search in specified files we can see some examples but usually there are too complex and not doing simply what I need.



Do snaps using audio-playback need some explicit configuration so applications supporting pulseaudio work?

I was not able to get it working yet but I have found other useful links:

According this topic Audio-playback interface in snaps running in daemon mode it may be needed to set some env variables like PULSE_SERVER.

I have also find https://snapcraft.io/test-snapd-pulseaudio snap which is for older pulseaudio interface but it has also snapcraft.yaml which contains also:
environment:
LD_LIBRARY_PATH: $SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/pulseaudio
So perhaps snap which uses pulseaudio needs additional env variables. By default libraries are searched for only in $SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET ?
Here somebody is trying to use this snap https://bugs.launchpad.net/ubuntu/+source/pulseaudio/+bug/1781428 But it doesn’t play sound for me on Ubuntu 21.04.

This is also useful hint: https://bugs.launchpad.net/ubuntu/+source/snapd/+bug/1879229
Although I see some pid and native files in /run/user/1000/pulse, sox is still not able to connect to pulseaudio.

This looks to be a desktop application or game, so the best way to get everything functional is to use the gnome-3-38 extension (because it’s based on core20 - for core18 you would want either gnome-3-28 or gnome-3-34):

apps:
  c-evo:
    command: usr/share/c-evo/c-evo
    desktop: usr/share/applications/c-evo.desktop
    extensions: [gnome-3-38]
    plugs:
      - home
      - audio-playback

Thanks for the replay. But how that can help me to resolve sound support? Are there some commands included to play sound files?
My latest finding is that standard sox package from Ubuntu probably won’t work with pulseaudio in snap because needed sox libs are placed in different directory like /usr/lib/sox. But in snap they are located in $SNAP/usr/lib/sox. So sox won’t find them and report missing formats and device drivers. So perhaps sox can be recompiled for snap package but that would be kind of overkill to rebuild another application during snapcraft build just to be able to use play command.

I’ve tried gnome-3-38, gnome-3-34 but I’ve got linking errors from FPC. I don’t know what is exactly problem here but Lazarus/FPC probably expects different version of those libraries. And currently GTK applications need to be built with Gtk2 and not GTK 3 or 4. So perhaps there are some libraries missing in those gnome extension for Gtk2. Or it is just version mismatch.

/snap/gnome-3-34-1804-sdk/current/usr/bin/ld.bfd: warning: link.res contains output sections; did you forget -T?
/snap/gnome-3-34-1804-sdk/current/usr/lib/x86_64-linux-gnu/libpangoft2-1.0.so.0: undefined reference to pango_font_get_hb_font' /snap/gnome-3-34-1804-sdk/current/usr/lib/x86_64-linux-gnu/libgio-2.0.so.0: undefined reference to g_utf8_validate_len’
/snap/gnome-3-34-1804-sdk/current/usr/lib/x86_64-linux-gnu/libgio-2.0.so.0: undefined reference to g_source_set_dispose_function' /snap/gnome-3-34-1804-sdk/current/usr/lib/x86_64-linux-gnu/libpangoft2-1.0.so.0: undefined reference to pango_coverage_get_type’
/snap/gnome-3-34-1804-sdk/current/usr/lib/x86_64-linux-gnu/libpangoft2-1.0.so.0: undefined reference to pango_font_description_get_variations' /snap/gnome-3-34-1804-sdk/current/usr/lib/x86_64-linux-gnu/libgio-2.0.so.0: undefined reference to g_ref_count_init’
/snap/gnome-3-34-1804-sdk/current/usr/lib/x86_64-linux-gnu/libgio-2.0.so.0: undefined reference to g_ref_count_inc' /snap/gnome-3-34-1804-sdk/current/usr/lib/x86_64-linux-gnu/libpangoft2-1.0.so.0: undefined reference to pango_font_description_set_variations’
/snap/gnome-3-34-1804-sdk/current/usr/lib/x86_64-linux-gnu/libgio-2.0.so.0: undefined reference to g_spawn_async_with_fds' /snap/gnome-3-34-1804-sdk/current/usr/lib/x86_64-linux-gnu/libgio-2.0.so.0: undefined reference to g_ref_count_dec’
/snap/gnome-3-34-1804-sdk/current/usr/lib/x86_64-linux-gnu/libgio-2.0.so.0: undefined reference to g_unix_get_passwd_entry' /snap/gnome-3-34-1804-sdk/current/usr/lib/x86_64-linux-gnu/libgio-2.0.so.0: undefined reference to g_canonicalize_filename’
/snap/gnome-3-34-1804-sdk/current/usr/lib/x86_64-linux-gnu/libgio-2.0.so.0: undefined reference to g_atomic_ref_count_init' /snap/gnome-3-34-1804-sdk/current/usr/lib/x86_64-linux-gnu/libgio-2.0.so.0: undefined reference to g_atomic_ref_count_dec’
/snap/gnome-3-34-1804-sdk/current/usr/lib/x86_64-linux-gnu/libgio-2.0.so.0: undefined reference to `g_atomic_ref_count_inc’

I also tried gnome-3-28 and it compiled correctly but during run it reports error so no luch with this as well.

(2048:23724): GLib-GObject-WARNING **: 20:21:38.303: specified class size for type ‘IBusIMContext’ is smaller than the parent type’s ‘GtkIMContext’ class size
malloc(): memory corruption
Neúspěšně ukončen (SIGABRT)

I may be one of first developers which are trying to package Lazarus app with snap. I have create a new wiki tutorial here https://wiki.freepascal.org/Create_snap_package for this purpose. But so far it was kind of paintful and time consuming experience. There are still so many issues which need to be solved so the snap application can run with same user experience as with native deb package. At least publishing snap package is much easier than dealing with Debian rules for inclusion deb packages in the distribution.

Hi I have essentially the same problem. Did you find a solution yet. What I was able to determine is that libsox3 is not found (ldd /usr/bin/sox shows that one missing) although the files are there, symlinks seem to be ok. I can see that if I finish the build with an --shell-after. If I run apt install libsox3 in that same shell it seems now ldd will return all found.
I am not completely sure what this all means as this is my first snap but it seems something funky with the linking of the library dependencies for sox. I suspect that may extent to the libsox-fmt-base and libsox-fmt-alsa which seem to hold the code required for working with the different (basic) audio file formats.
Do post if you find a solution as I am starting to bang my head against the wall on this one.
Thanks

It sets the PULSE_SERVER variable properly and also adjusts your LD_LIBRARY_PATH so your app can find libpulse … if you do not want to use the extension, there are also the snapcraft desktop helpers instead to help you with setting the right default environment for a desktop app:

Hi, I didn’t find a solution yet. From my last finding and using command snap run --shell myapp, I can that libraries from my snap are placed in $SNAP/usr/lib/…/sox and not in /usr/lib/…/sox. This is a problem because in that case sox can’t find its extensions which include pulseaudio driver and mp3 format. I tried to find some way how to point out to correct directory but without success. LD_LIBRARY_PATH didn’t help. Variable was set but play was still not able to find its plugin libraries.

environment:
LD_LIBRARY_PATH: $SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/pulseaudio:$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/sox

I still don’t understand how snaps work and how existing Ubuntu libraries can be used. I think only way how to get sox working is to recompile it during snapcraft build phase and change its sox directory to point to correct location with $SNAP root. But this is just my guess. And also I don’t want to use such approach to compile other libraries during build of my app. There should be some easier official way how to get sound command working. sox isn’t only package which can play mp3 files to pulseaudio. The goal here is to keep our snaps small with size close to native deb package. To achieve this we probably need to use gnome extension. But unfortunately it doesn’t work with build system used by my applications. That would probably need hundreds of hours to resolve that. I am not sure if I can devote so much time to this.

add this to your snapcraft.yaml toplevel

layout:
  /usr/lib/sox:
    bind: $SNAP/usr/lib/sox

Thanks for this suggestion. I’ve already seen those layouts but somehow I didn’t know for which kind of use cases they are handy. https://snapcraft.io/docs/snap-layouts

So to get sox running, multiple things need to be sorted out. But finally I was able to play sound from my snapped game.

The final solution should be:

apps:
  myapp:
    plugs:
      - audio-playback

parts:
  myapp:
    stage-packages:    
    - sox
    - libsox-fmt-mp3
    - libsox-fmt-pulse
    - libpulse0 

environment:
  LD_LIBRARY_PATH: $SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/pulseaudio
  PULSE_SERVER: unix:/run/user/1000/pulse/native

layout:
  /usr/lib/$SNAPCRAFT_ARCH_TRIPLET/sox:
    bind: $SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/sox

This is also documented here: https://wiki.freepascal.org/Create_snap_package#Sound_support
Real example: https://svn.zdechov.net/c-evo/trunk/Install/snap/snapcraft.yaml (https://snapcraft.io/c-evo)

Sound is working now but one more thing needs to be resolved. PULSE_SERVER should be set to PULSE_SERVER: unix:$XDG_RUNTIME_DIR/…/pulse/native but $XDG_RUNTIME_DIR is not created by default so that path will fail if used. Also there is no way how to specify that path directly without “…” part. I’ve used fixed path to point to /run/user/1000 directory but that is probably not ok. Also I can’t use something like /run/user/$UID as that is not known at the time of snap build.

I saw that this can be solved by some desktop-launch script which can create that directory if it doesn’t exist. But I don’t want to use such script. I would prefer to run application executable directly. Is there some other way?