5

I have set up a script, which executes an mplayer command, in order to play audio in crontab. It will be executed in a special set time, but this script can not play audio when ncmpcpp (a front-end for mpd, aka. Music Player Daemon) is playing music. But this script can play audio when I execute it from terminal like this:

sh bin/alarm.sh   

I try to get output from either crontab or script, but to no avail. I guess some setting in ncmpcpp or mplayer is wrong, or mpd is using same daemon with crontab's daemon.

  • Anyway, I can not find out the solution. If you know, please help me.
v2r
  • 9,367
  • 11
  • 49
  • 52
stardiviner
  • 273
  • 4
  • 12

4 Answers4

3

The problem seems to be that mpd runs under user mpd per default (as a system daemon) and is thus unable to join your sessions' pulseaudio mixer, therefore it hogs the sound card output via alsa or even oss. In my answer, I assume that you are logged in and that the crontab is yours and not the system crontab.

MPD

Depending on your priorities, you have a lot of options that mostly circle around the idea teaching mpd to not hog the sound and use pulseaudio instead. See the mpd wiki.

mplayer

You can configure mplayer to default to pulseaudio by placing a line ao=pulse in ~/.mplayer/config but pulseaudio should pick it up nicely with its alsa plugin.

aquaherd
  • 6,160
  • 1
  • 29
  • 39
  • I have set `ao=pulse` in `~/.mplayer/config`. but set mpd on pulseaudio is so complex. But it this splution is good. – stardiviner Nov 21 '11 at 04:41
  • This doesn't work for me. `mplayer` just waits until `mpd` is paused, then plays the sound. – Exeleration-G Dec 28 '15 at 09:06
  • Thank you so much! This helped with a Qt5 app which was refusing to play while an `mplayer` instance was running - turns out `mplayer` hogged the sound device. Setting it to `pulse` allowed them to share it perfectly. – juod May 06 '21 at 05:02
0

When using e.g. play (from the sox package), the following information needs to be exported:

export DISPLAY=:0
export XAUTHORITY=/home/YOURUSERNAME/.Xauthority
blueyed
  • 8,855
  • 3
  • 29
  • 36
0

In your crontab, please add this to the command section

export DISPLAY=:0.0 && sh /bin/alarm.sh

The export DISPLAY=:0.0 is required to run Graphical apps.

Nitin Venkatesh
  • 21,993
  • 11
  • 68
  • 91
  • It does not help for sound though.. the error from cron for e.g. ogg123 is: `ALSA lib pcm_dmix.c:1018:(snd_pcm_dmix_open) unable to open slave ERROR: Cannot open device alsa.` – blueyed May 05 '12 at 18:27
0
XDG_RUNTIME_DIR=/run/user/$(id -u) <sound command>

Also have a look at the links below, as other vars might be needed:

https://wiki.archlinux.org/index.php/PulseAudio#Play_sound_from_a_non-interactive_shell_.28systemd_service.2C_cron.29

mplayer can't play audio from cron while ncmpcpp is playing simultaneously

Zanna
  • 69,223
  • 56
  • 216
  • 327
  • Please [edit] and explain where/how this variable should be passed or set, and if possible why it is needed – Zanna Dec 20 '17 at 16:05