PHP mplayer test won't play over internet

This issue may not be directly related to PHP but everyone here is certain to know more about the interaction of PHP and Apache than I do. I have a rudimentary PHP mplayer test:

<?php // Play a simple 1/f frequency sweep for test


exec(‘mplayer /home/pi/music/16Hz-20kHz-Lin-1f-10sec.mp3’);

The test works when I play it locally but if I execute it from a browser aimed at the device’s IP, the test doesn’t play.

I suspect file/directory permission and possibly ownership problems. I can’t fix it because the efforts I have made to learn what permissions/ownership should be haven’t gotten me anywhere.

The ~/music directory is:
drwxrw-rwx 2 pi pi 4096 Jul 30 20:39 music
and the specific sample is:
-rwxrw-rwx 1 pi pi 80942 Feb 1 2008 16Hz-20kHz-Exp-1f-10sec.mp3

/var/www/html/ is:
drwxrwxr-x 2 www-data www-data 4096 Jul 30 20:26 html
and the test file is:
-rw-r–r-- 1 root root 120 Jul 31 20:10 try_play.php

The pi user (me) is a member of the www-data group.

Can you help me make this right? Maybe I’m even on the wrong track. I’ll appreciate any help or suggestions.

Hi @rsb and a warm welcome to the forums.

Can you check the filenames?

One file has Lin and another file has Exp in the filenames,

Perhaps using PHP if( file_exists(…): and also applying a full path may help.

Thanks for the quick response. And your careful eye. I made a posting error. The correct file is actually in the directory:
-rwxrw-rwx 1 pi pi 80937 Feb 1 2008 16Hz-20kHz-Lin-1f-10sec.mp3
I just copied the wrong one when I posted.

And the php does work when I play it locally.

Can you supply a link to the online site?

Define what you mean by - “And the php does work when I play it locally.”

You do realize that this code is going to play the /home/pi/music/16Hz-20kHz-Lin-1f-10sec.mp3 file on the computer that is acting as the web server whenever a remote user accesses this script, don’t you?

If the machine - - is hosting this script. Then if the machine at accesses this script, that means is going to play this mp3 file. Is that what you are wanting to happen?

I would also probably encourage you to use the full path to the mplayer binary.

which mplayer

should give you the full path to mplayer. Whatever path it is in may not be in the path environment variable for your webserver.

The remote site is an r pi3 and its purpose is to play music and radio through a good DAC and physically attached speakers. “And the php does work when I play it locally.” means that when I ssh to the pi and execute php try_play.php it plays the test file through its attached speakers. But if I address it from a browser at http://raspberrypi/try_play.php it does not play the sample. http://raspberrypi does work as an address; I can get the Apache default page, PHP info, and mysql info that way.

I see why you asked about this. The headline is misleading. I’ll try to edit it to make it clearer.

What user is PHP executing as under the web server?

Is that user a member of the audio group on this device?

What does the PHP (or webserver) error log say?

How is PHP interfacing with the webserver?

The logged in user is ‘pi’. Pi is a member of audio and of www-data. Sorry, I’m a novice with networking stuff. Forgot there were logs. I’ll check. Not certain about the last question. try_play.php is located in /var/www/html. I can execute other php scripts there from my browser with no problems.

The purpose of this is to control my headless pi from my phone either through a browser client or a native android app. I’m much better with the client side than the server side unfortunately. Off to those logs now. Thanks.

PHP is probably executing as a different user. I don’t know what web server you are using, or how you are interfacing PHP with the web server.

I would suspect that whatever that user is, it doesn’t have access to whatever is controlling the audio on that device (pulse audio?)

Or it’s not finding the mplayer binary because the full path is not being specified.

There’s probably a wealth of information regarding this within the PHP error_log or web server error_log.

The pertinent part of the log is:

[Mon Jul 30 20:11:50.478865 2018] [core:notice] [pid 594] AH00094: Command line: ‘/usr/sbin/apache2’
Cannot find HOME directory.
AO: [pulse] Init failed: Connection refused
Failed to initialize audio driver ‘pulse’
[AO_ALSA] Playback open error: Permission denied
Failed to initialize audio driver ‘alsa’
[AO SDL] Unable to open audio: No available audio device
Failed to initialize audio driver ‘sdl:aalib’
Could not open/initialize audio device → no sound.
Cannot find HOME directory.
File not found: ‘/home/pi/Music/16Hz-20kHz-Lin-1f-10sec.mp3’
Failed to open /home/pi/Music/16Hz-20kHz-Lin-1f-10sec.mp3.
Cannot find HOME directory.
File not found: ‘/home/pi/Music/16Hz-20kHz-Lin-1f-10sec.mp3’
Failed to open /home/pi/Music/16Hz-20kHz-Lin-1f-10sec.mp3.

The two lines referring to [pulse] are normal. Pulse audio is disabled. Not being able to open ‘alsa’ is a killer from the beginning. Permission denied.
I don’t understand this error:
Cannot find HOME directory.
File not found: ‘/home/pi/Music/16Hz-20kHz-Lin-1f-10sec.mp3’

$HOME is defined now as ‘/home/pi’. Does the error mean I should change the path to the audio sample?

Are you using pulse audio or alsa?

Whatever it is, the user your web server is executing PHP as doesn’t have access to the audio group. You would need to remedy that, but I don’t know the exact steps to tell you how to do that.

I’d look at the output of

groups pi

and compare that to

groups %whatever_user_php_is_executing_as_under_the_webserver%

As for why it’s not able to access the file from pi’s home directory. This could be a permission issue or it could be a restriction such as open_basedir that is preventing you from loading it.

Thanks again. I really appreciate your continuing help. I’m using alsa; pulse is disabled. I’ll tackle the groups issue first. Then I’ll trial and error the second.

I’ll let you know what happens after some experimentation (and some dinner).

1 Like

Once again, thanks for your continuing help. I appear to have found the solution. The audio test mp3 files did not have the x permission set for ‘group.’ When I set the x permission the php worked immediately from a browser. There are no errors in the apache2 log. That permission change seems to have solved all the problems. Thanks everyone. I’m good now, although I imagine I’ll be back again soon enough.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.