-1

I just built a mp3 player from playlist.me the script looks like this:

<!-- Song Player http://playlist.me -->
<script
    type="text/javascript"
    src="http://playlist.me/w/script.js"
    data-config="{'skin':'skins/black/skin.css','volume':50,'autoplay':false,'shuffle':false,'repeat':1,'placement':'bottom','showplaylist':false,'playlist':[{'title':'Song TItle','url':'http://sourcemp3.com/music.mp3'}]}" >
</script>
<noscript>
    <a href="http://playlist.me/w/i.php">SCM music player</a>
</noscript>
<!-- playlist.me script end -->

The problem is that I have to manually edit the script to add more MP3 files, how do I add files without manually edit the script? My MP3 files are located in /mnt/usb. I can do find with bash to list all the music files but how do I put them in the script above? My goal is that I want to be able to update my MP3 files by just copying and pasting my files to /mnt/usb/ and they'll show in my web player automatically.

UPDATE as suggested, I edited my script to be like this:

<?php
$glob = glob('/mnt/usb/*.mp3');
$url  = '/mnt/usb/';

$playlist = array();
foreach ($glob as $mp3) {
    $playlist[] = array('title' => $mp3, 'url' => $url . $mp3);
}
echo '
<html>
<head>
<title> MP3 Player</title>
</head>
<body>
<script
    type="text/javascript"
    src="http://playlist.me/w/script.js"
    data-config="{\'skin\':\'skins/black/skin.css\',\'volume\':50,\'autoplay\':false,\'shuffle\':false,\'repeat\':1,\'placement\':\'bottom\',\'showplaylist\':false,\'playlist\':[{\'title\':\'Song TItle\',\'url\':\'http://sourcemp3.com/music.mp3\'}]}" >
</script>
<noscript>
    <a href="http://playlist.me/w/i.php">SCM music player</a>
</noscript>
</body>
</html>';
echo sprintf("'playlist':%s", json_encode($playlist));
?>

But I get the following error: Fatal error: Call to undefined function json_encode() in /www/music.php on line 25 and the music won't load either. Please help

UPDATE AGAIN I got that json part sorted out, now the problem is like this: enter image description here

My script:

<?php
$glob = glob('/mnt/usb/*.mp3');
$url  = '/mnt/usb/';

$playlist = array();
foreach ($glob as $mp3) {
    $playlist[] = array('title' => $mp3, 'url' => $url . $mp3);
}
echo sprintf("'playlist':%s", json_encode($playlist));
echo '
<html>
<head>
<script
    type="text/javascript"
    src="http://playlist.me/w/script.js"
    data-config="{"skin":"skins/black/skin.css","volume":50,"autoplay":false,"shuffle":false,"repeat":1,"placement":"bottom","showplaylist":false,"playlist":" >
</script>
<title> MP3 Player</title>
</head>
<body>
<noscript>
    <a href="http://playlist.me/w/i.php">SCM music player</a>
</noscript>
</body>
</html>';
echo sprintf("'playlist':%s", json_encode($playlist));
?>

How do I solve this?

Don
  • 235
  • 2
  • 4
  • 16
  • You could try and recursively build a – Halfpint Jan 21 '15 at 14:17
  • 1
    You need to pick a programming language (you have three tagged in the question, although I wouldn't recommend bash). Pick one, then figure out how to integrate it with your webserver (JS will be "use Node instead", PHP is mod_php or fastcgi, bash is typically CGI I think, most other options have fastcgi as an option). – Quentin Jan 21 '15 at 14:19

1 Answers1

0

Can't be done in pure clientside Javascript because it can't read the servers filesystem. Need at least 1 Serverside language:

Embed this PHP into your Javascript, to fill the playlist variable.

<?php
$glob = glob('/mnt/usb/*.mp3');
$url  = 'http://localhost/usb/';

$playlist = array();
foreach ($glob as $mp3) {
    $playlist[] = array('title' => $mp3, 'url' => $url . basename($mp3));
}


echo sprintf("'playlist':%s", json_encode($playlist));

Symlink (type in console):

ln -s /mnt/usb /www/usb
Daniel W.
  • 31,164
  • 13
  • 93
  • 151