I want to play music via PHP on my Raspberry Pi with Mocp (music on console). This command
mocp -S
works just fine in the console (it starts the server of mocp) but in PHP's exec function it doesn't work:
<?php
$ret = exec("mocp -S", $out, $err);
print_r($ret);
print_r($out);
print_r($err);
?>
It just returns:
Array ( ) 2
According to this list return_var code 2 means
Misuse of shell builtins (according to Bash documentation)
But what exactly does this mean? The command mocp --help
works in both console and PHP exec.
How can I run mocp with every parameter in PHP?
EDIT:
I just logged in as "www-data" (the apache user) and tried to run mocp commands. I got this return:
FATAL_ERROR: Can't create directory
/var/www/.moc
I copied the .moc
folder of Pi to www-data
(with user "pi"):
sudo cp /home/pi/.moc/ /var/www/.moc
Now my script from above gives me:
Running the server...Array ( [0] => Running the server... [1] => Running the server... ) 2
But it still does not work. In the console of www-data mocp -S
now gives me
FATAL_ERROR: Can't bind() to the socket
EDIT 2:
I changed the permsions on /var/www/.moc
. If I try mocp -S
, I get this
Running the server...
Trying JACK...
Trying ALSA...
ALSA lib confmisc.c:768:(parse_card) cannot find card '0'
ALSA lib conf.c:4241:(_snd_config_evaluate) function snd_func_card_driver returned error: Datei oder Verzeichnis nicht gefunden
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4241:(_snd_config_evaluate) function snd_func_concat returned error: Datei oder Verzeichnis nicht gefunden
ALSA lib confmisc.c:1251:(snd_func_refer) error evaluating name
ALSA lib conf.c:4241:(_snd_config_evaluate) function snd_func_refer returned error: Datei oder Verzeichnis nicht gefunden
ALSA lib conf.c:4720:(snd_config_expand) Evaluate error: Datei oder Verzeichnis nicht gefunden
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM default
Trying OSS...
FATAL_ERROR: No valid sound driver!
FATAL_ERROR: Server exited!
Seems like www-data user doesn't have right access permissions to use your audio device. I belive executing this should help:
sudo usermod -a -G audio www-data
My full solution of the problem:
I started with a new installation of raspbian and ran those comands:sudo raspi-config
->Expand Filesystem
->Finish, reboot
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install apache2 php5 libapache2-mod-php5
sudo groupadd www-data
sudo usermod -g www-data www-data
sudo apt-get install moc
sudo usermod -a -G audio www-data
mocp -S
sudo cp -R /home/pi/.moc /var/www/.moc
sudo chown www-data /var/www/.moc/
sudo chmod -R a+rwxst .moc/
At the end user "www-data" is able to run mocp.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With