miaomiao
Posts: 22
Joined: Fri Jun 30, 2017 4:07 am

Omxplayer fail to play sound file when Raspberry Pi auto run

Tue Sep 12, 2017 9:51 am

First time Raspberry Pi user.

We are using Geany on RP2 and play audio sound is one of our function. " Omxplayer " is used. This is the code we use in geany to play sound file

Code: Select all

status=system("omxplayer  -g  -o local sound.mp3")
When I run it in the RP terminal it can work properly,but only problem is when I tried to auto run the program by sudo nano /etc/rc.local ,everything can work except the play sound function.

This is what inside the /etc/rc.local

Code: Select all

#!/bin/sh -e
#
#rc.local
#print the IP address
IP=s(hostname -I)|| true
if [ "$_IP" ]; then
printf"My IP address is %s\n" "$_IP"
fi
sudo  /home/pi/urg_library/get_distance
exit 0
i have is that i can not figure out how to read sound file when Raspberry Pi auto run itself. It only work when I remove sudo /home/pi/urg_library/get_distance from /etc/rc.local.

What is the problem? Any one know how to solve it? Thanks in advance.

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Omxplayer fail to play sound file when Raspberry Pi auto run

Tue Sep 12, 2017 10:15 am

Firstly you do not need to use "sudo" when running commands in /etc/rc.local. Everything in that file runs as "root" user already.

Code: Select all

#!/bin/sh -e
#
#rc.local
#print the IP address
IP=s(hostname -I)|| true
if [ "$_IP" ]; then
printf"My IP address is %s\n" "$_IP"
fi
/home/pi/urg_library/get_distance
exit 0

Secondly, you'll need to put the full path to your sound.mp3 file, otherwise omxplayer won't find it. Assuming you normally run the program as the "pi" user, that would need

Code: Select all

status=system("omxplayer  -g  -o local /home/pi/sound.mp3")
(I am assuming your program begins with #!/usr/bin/python or something similar, otherwise you may need to add /usr/bin/python to the start of the line in rc.local)

Return to “Troubleshooting”