0

I am trying to achieve the following : I want to start a selected song on the rpi from a web interface.

I can play the song through exec

exec("mpg123 /var/www/html/Sounds/Zelda.mp3",$status);

and I can create a list dynamically through /var/www/html/Sounds

<?php
$arr_files = scandir("/var/www/html/Sounds/");
echo "<select name =\"songlist\" size=\"5\">";

foreach ($arr_files as $value){
echo "<option value=\"$value\">$value</option>";
}   
echo "</select>"
?>

How would I go about in order to take the selected song from the html select 'songlist' and then play it using the exec command by clicking a button?

So far, the only thing I am finding is how to play a song on the client, not on the server side.

Hopefully this is clear enough. Please let me know if more information is required.

Thanks.

edit : I would like to say that I am a total newbie with PHP/HTML, so explanations would be welcomed as well.

1

You can do it all in one PHP page. I personally would not I would have the processing done in a separate PHP file, but this will get you started

<?php
if(isset($_POST['songlist'])){
    $selected = $_POST['songlist'];
    //exec("mpg123 /var/www/html/Sounds/".$selected);
    echo $selected;
}

echo "<form method='post' name='songlist'>";
$arr_files = scandir("/var/www/html/Sounds/");
echo "<select name =\"songlist\" size=\"5\">";

foreach ($arr_files as $value){ 
    echo "<option value=\"$value\">$value</option>";
}   
echo "</select>";
echo "<input type='submit' name='submit'>";
echo "</form>";
?>
  • Thank you for your help. It does get me unstuck. My question is this then : can I use action = "script.php" in the input form to then call a script with the if isset section and then play the song? Is that what you meant when you said to do it in a separate file? – Jeph Gagnon Oct 4 '17 at 12:22
  • yes that is exactly what I mean :D that way when you start styling your form it gets less confusing. – Dr.Rabbit Oct 4 '17 at 12:45

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.