1

I can't seem to figure out how to open an mp3 File on my Webserver. If I just href the Song (Code below) it opens it on the Client I am accessing it from. I want the Song to play on the Webserver even if I access the html page from another Device.

<!DOCTYPE html>
<html>
<body>
<a href="Song.mp3">Song</a>

</body>
</html>

How do I solve this? I am using a Raspberry Pie, Apache, VLC (to play mp3) Thanks for your support :D

Brad
  • 159,648
  • 54
  • 349
  • 530
zeveos
  • 25
  • 4
  • https://stackoverflow.com/a/18826567/7351855 – Matej Feb 02 '19 at 15:55
  • This has nothing to do with my question. My mp3 file opens fine when I click on the link. I just can't figure out how to always execute the mp3 file on the server (Raspberry) instead of it opening on the client device as I click on the html link. – zeveos Feb 02 '19 at 16:13
  • 1
    So you don't want to use your browser just via terminal, right? – Matej Feb 02 '19 at 16:14
  • Yes I'd like this command to be executed on the Server when clicking the link: vlc Song.mp3 – zeveos Feb 02 '19 at 16:18
  • So something totally different. Is this what you want?: start http web server on rpi, sends html to client, when client clicks to the button play audio? – Matej Feb 02 '19 at 16:20
  • 1
    Oh, I see, you have apache server, so use php system() function http://php.net/manual/en/function.system.php – Matej Feb 02 '19 at 16:21
  • I changed this in the html: Song And created a php file but it is still not working. If I execute the php File on the server itself it works but it doesn't if I click the link. Here the php code: – zeveos Feb 02 '19 at 16:41
  • I posted answer instead of comment, is it possible to remove answer? xd – Matej Feb 02 '19 at 16:42

2 Answers2

1

Try this:

index.html:

<a href="/script.php">Song</a>

script.php:

<?php exec('export DISPLAY=:0; cvlc songs/Song1.mp3'); ?>


Edit:

Solution with inotifywait which can be CPU consumption friendly:

Php:

<?php 
    shell_exec('cat Song1.txt');
 ?>

Bash:

touch Song1.txt  
while  [ -f run.txt ]
do
    inotifywait --event=close_nowrite Song1.txt
    cvlc --play-and-exit songs/song.mp3
done
Matej
  • 782
  • 2
  • 6
  • 19
1

Because I couldn't execute the command directly I had to create a script which reacts to created files.

<?php 
    shell_exec('touch Song1.txt');
    ?>

Song.sh :

while  [ -f run.txt ]
do
if [ -f Song1.txt ];
then  
        cvlc --play-and-exit songs/song.mp3
        rm -f Song1.txt
fi
done
zeveos
  • 25
  • 4
  • That's also solution but CPU consumption is high, right? Add some sleep, for example `sleep 0.1` under the `fi` or use `inotify` – Matej Feb 03 '19 at 11:11
  • I edited my answer with `inotify`, it can use less CPU. – Matej Feb 03 '19 at 11:51