简体   繁体   中英

How to run mplayer with audio speaker output from PHP web script on linux?

I am logged in to linux feora 15 distro with username: stackoverflow. My browser execute in the local system a PHP script to play a music using PHP system("mplayer /tmp/stackoverflow.wav"), passthru("mplayer /tmp/stackoverflow.wav") command. Such as linux command.

As a user stackoverflow i dont hear any audio. But i can see that mplayer /tmp/stackoverflow.wav is running which has duration of 8minutes audio.

My question is: how can i use PHP system("mplayer /tmp/stackoverflow.wav"); to switch user and run the same command, so that i can hear my music with my PHP?

<?php
// for example
switchuser("su stackoverflow password");
system("mplayer /tmp/itworks.wav");
?>

The direct answer to your question would be to use sudo , as in

system("sudo -u <username> mplayer /tmp/itworks.wav")

But, I'm not sure this will solve your problem. Firstly, where do you want the sound outputted? The server or the client/browser? The above technique will work on the server. For the browser, you'd need to get the client to load the.wav file as an object or some such and deal with it, presumably via a plugin.

You mention that you can see a running mplayer process though? If you are at the server (or the server and client are on the same physical machine) and you can't hear any output, then there's something else going on. It's possible that the web server process user doesn't have permission to use the audio card, but then I doubt the mplayer process would continue for so long without terminating with "permission denied" error. To figure this out, we'd need more info I guess

I would like to do the same. When I try to run mplayer just via shell_ecex(); it does not select pulseaudio for output. Even when I include into the command -ao pulse it doesn't work. When I try to run the mplayer command with sudo (I added www-data to the sudoers file), mplayer apparently starts playing (according to ressource usage) but there is no output. When I kill the mplayer instance manually the whole sound output is blocked, even when I launch mplayer via the console. Do you have any tips for me how to run mplayer via PHP?

Edit: I have a solution: My command looks like this:

shell_exec('sudo -u [username] nohup  mplayer -slave [mplayer attributes go here] 2> /dev/null > /dev/null &'));

This also stops the php script from waiting for a result from mplayer, so it starts mplayer and continues operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM