在 Raspberry Pi 上使用 crontab 无法播放音频 [英] Audio doesn't play with crontab on Raspberry Pi
问题描述
我正在尝试让当前连接到蓝牙扬声器的 Raspberry Pi 每天按计划播放音频文件,其中我的 cron
作业正在更新 @daily
获得新的时间(基本上是祈祷的召唤)
I'm trying to get my Raspberry Pi which is currently connect to a bluetooth speaker to play an audio file daily on a schedule basis where my cron
job is being update @daily
to get new timing (It's basically a call to prayer)
crontab -l
@daily /home/pi/athan/update_prayers.sh
39 05 27 2 * /home/pi/athan/call_prayer.sh >/dev/null 2>&1 #fajr
31 12 27 2 * /home/pi/athan/call_prayer.sh >/dev/null 2>&1 #dhuhr
34 15 27 2 * /home/pi/athan/call_prayer.sh >/dev/null 2>&1 #asr
05 18 27 2 * /home/pi/athan/call_prayer.sh >/dev/null 2>&1 #maghrib
24 19 27 2 * /home/pi/athan/call_prayer.sh >/dev/null 2>&1 #isha
所以,以上就是我现在所拥有的,这是/home/pi/athan/update_prayers.sh
So, the above is what I have right now and this is the content of /home/pi/athan/update_prayers.sh
#!/bin/bash
/home/pi/.nvm/versions/node/v7.5.0/bin/node /home/pi/athan/set_prayer.js
基本上我的 set_prayer.js
只是使用这个 https://www.npmjs.com/package/crontab 模块来设置 crontab,我能够每天更新它,这里没有问题.现在我正在使用 Mplayer
播放音频,这是我使用的命令:
Basically my set_prayer.js
is just using this https://www.npmjs.com/package/crontab module to set crontab and I was able to get it update daily no problem here. Right now I'm using Mplayer
to play the audio and this is the command I use:
/home/pi/athan/call_prayer.sh
#!/bin/bash
/usr/bin/mplayer /home/pi/athan/athan.mp3
我的问题是,在运行 cron 作业时没有声音,或者我什至不知道作业是否正在运行,但是当我手动执行时,我可以听到正在播放的音频没有问题.我还尝试使用 /usr/bin/omxplayer -o alsa/home/pi/athan/athan.mp3
直接在此处运行它,而不是运行 bash 脚本,但它似乎不起作用使用 cron 但当我直接运行命令时工作正常.
My problem here is that when it's time for the cron job to run there's no sound or I don't even know if the job is being run, but when I do it manually I can hear the audio being played no problem. I've also tried to run it directly here using /usr/bin/omxplayer -o alsa /home/pi/athan/athan.mp3
instead of running bash script and it doesn't seem to work with cron but works fine when I run the command directly.
推荐答案
我遇到了类似的问题(如果从 crontab 运行命令,则蓝牙扬声器上没有音频.
I was struggling with a similar problem (no audio on Bluetooth speaker if the command is run from crontab.
问题在于混合pulseaudio/crontab和环境设置.
The problem was the mix pulseaudio/crontab and the environment setup.
The following solution worked for me: https://wiki.archlinux.org/index.php/PulseAudio#Play_sound_from_a_non-interactive_shell_.28systemd_service.2C_cron.29
所以基本上只需执行 crontab -e
并添加以下内容:
so basically just do crontab -e
and add the following:
XDG_RUNTIME_DIR=/run/user/user_id
可以通过以下命令找到用户ID:
the user id can be found out by the following command:
id [user_name]
这篇关于在 Raspberry Pi 上使用 crontab 无法播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!