mobiletechtracker / NetCoreAudio

Allows playback of audio files on .NET Core with minimal dependencies
MIT License
124 stars 20 forks source link

Playing sound while running as systemd #20

Open thetoeb opened 6 months ago

thetoeb commented 6 months ago

I figured out that starting the mpg123 while being an systemd service don't work (also amixer and i think aplay also), because you need to specify the device you want to use. I workaround it by adding the package "Microsoft.Extensions.Hosting.Systemd" and if OS is Linux it checks with SystemdHelpers.IsSystemdService() to instantiate a different _internalPlayer, which uses the commands -a hw1,0 (for example) on mpg123 and -c 2 on amixer to ensure the correct device is used (Im currently using it on my Rasperry PI 4 B so this is the headphone output).

Maybe you could add that? I didn't figured out a good way to generically implement that to specify the correct device in that case, because i just needed that quick fix for my project.

weidmanngabriel commented 4 months ago

@thetoeb Could you provide some more details about your fix? Maybe even the relevant code parts? I got the same problem and was trying to find a solution for hours

thetoeb commented 4 months ago

Sure. All i did was to create my own implementation of the "UnixPlayerBase" to adjust the bash commands, so that they know which device to use. This worked for me, but you might need to identify the hardware-index of your device for your system: Pastebin

The relevant places are "-a hw1,0", "--device=="plughw:2,0" and "-c 2". On amixer you might also need to know which control is used (on my system its called "PCM", it might be called different on yours). To find the correct IDs and names please consider the documentation of amixer / aplay.

weidmanngabriel commented 4 months ago

@thetoeb Great, it finally works 👍 Fortunately I had to change nothing (I'm a linux newby, even if ChatGPT is helpful a lot).

Maybe I will check out if there is a more general way, I don't really like to hardcode output ports.