I currently have an Apache web server running on my Pi that has this as the index.php
file:
<?php
if (isset($_GET['play'])) {
exec('sudo omxplayer yes.m4a');
}
?>
However, my code doesn't work! When I visit the page, nothing happens on my Pi. I know that exec does work, because when I use another command such as li
, I get an output. Is there any error(s) in my code?
Whenever someone visits the webpage with the url, say, 10.0.1.23/?play=1
, I want the Pi to run my audio file. I have been looking for what feels like forever, but to no avail.
If this is not possible for any reason (although I am almost sure it is), then what would be the easiest way to do this? I need to have my audio file get executed from another computer with minimal delay, as this is for my lab where, as long as it's consistent, a 1/4 of a second delay would be fine.
Edit: I have tried running the web server on my mac with the same code, only instead of sudo omxplayer yes.m4a
I used say test
and it seemed to have worked fine. Still not sure why my Pi isn't running my audio.
-
Does sound work correctly otherwise (without PHP)?Steve Robillard– Steve Robillard2014年12月20日 04:54:05 +00:00Commented Dec 20, 2014 at 4:54
-
@SteveRobillard Yes. Not sure why it's not working when running it via PHP.Andrew Dassonville– Andrew Dassonville2014年12月20日 04:56:25 +00:00Commented Dec 20, 2014 at 4:56
-
Where are you expecting the sound to play on the computer with the webpage or back to the client over the web?Steve Robillard– Steve Robillard2014年12月20日 05:16:28 +00:00Commented Dec 20, 2014 at 5:16
-
@SteveRobillard I am expecting the sound to play on my Pi.Andrew Dassonville– Andrew Dassonville2014年12月20日 05:21:21 +00:00Commented Dec 20, 2014 at 5:21
-
I think the problem is the sudo. First the sudo is going to try and run as the webserver user which may not be sudo enabled. running exec can be a security problem and this adds to that insecurity. One way around this may be to create a bash script to run the audio file and call that instead of the omxplayer command. The bash script will need to have the owner/group permissions set so the webserver user can call it. Another alternative is to add an entry to the sudoers file (make sure to edit this file with sudo visudo this checks for syntax errors which could stop your system from booting).Steve Robillard– Steve Robillard2014年12月20日 08:21:10 +00:00Commented Dec 20, 2014 at 8:21
1 Answer 1
you have to specify the full path to the executable and/or data files:
exec('/usr/bin/sudo /check/the/path/omxplayer /your/home/directory/or/whatever/yes.m4a');
you may find the path to omxplayer
using command which omxplayer
and path to your data file using your linux command line experience.
-
Thanks for the help. I just tried using
exec('/usr/bin/sudo /usr/bin/omxplayer /var/www/yes.m4a');
and it still doesn't work in PHP. However, it does work in command line, so could this have anything to do with where the audio gets output from when being run from PHP? Sort of a long shot, but I'm very new to Raspberry Pi programming. I also now know thatexec
its self does work, because I tried making the page runmkdir
and it successfully created the directory.Andrew Dassonville– Andrew Dassonville2014年12月20日 04:44:22 +00:00Commented Dec 20, 2014 at 4:44 -
@AndrewDassonville when you run
sudo
you might want to provide your password in some form, useman sudo
to seek for your options. and I hope your web server runs under your name, because for userwww
orwww-data
there might be no access tosudo
or the password is totally different.lenik– lenik2014年12月20日 12:13:45 +00:00Commented Dec 20, 2014 at 12:13