I created a web MP3 player, It works fine, it's just that I have to edit my script every time I add new songs on my webserver. I want the list to update automatically as I add more songs. My script basically looks like this:
<body>
<audio id="audio" preload="auto" tabindex="0" controls="" type="audio/mpeg">
</audio>
<ul id="playlist">
<li class="active"><a href="usb/music1.mp3">Music 1</a></li>
<li><a href="usb/music2.mp3">Music 2</a></li>
<li><a href="usb/music3.mp3">Music 3</a></li>
<li><a href="usb/music4.mp3">Music 4</a></li>
</ul>
</body>
My music files are all located at /www/usb
I tried something like this:
<?php
...............
echo '
<body>
<audio id="audio" preload="auto" tabindex="0" controls="" type="audio/mpeg">
</audio>
<ul id="playlist">';
exec('find /www/usb/ -name "*.mp3"',$list);
$x=0;
while($x<count($list)){
if($list[$x]==$h[0]){
echo '<li class="active"><a href="$list[$x]">$list[$x]</a></li>';
$x++;
}
}
echo '
</ul>
</body>
</html>';
?>
But it gives me the following error: Fatal error: Maximum execution time of 30 seconds exceeded in /www/test2.php on line 64
My complete script:
<?php
echo '
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#playlist,audio{background:#666;width:400px;padding:20px;}
.active a{color:#5DB0E6;text-decoration:none;}
li a{color:#eeeedd;background:#333;padding:5px;display:block;}
li a:hover{text-decoration:none;}
</style>
<script type="text/javascript" src="jquery-1.11.2.min.js"></script>
<script type="text/javascript">
var audio;
var playlist;
var tracks;
var current;
$(document).ready(function() {
init();
});
function init(){
current = 0;
audio = $("audio");
playlist = $("#playlist");
tracks = playlist.find("li a");
len = tracks.length - 1;
audio[0].volume = .10;
audio[0].play();
playlist.find("a").click(function(e){
e.preventDefault();
link = $(this);
current = link.parent().index();
run(link, audio[0]);
});
audio[0].addEventListener("ended",function(e){
current++;
if(current == len){
current = 0;
link = playlist.find("a")[0];
}else{
link = playlist.find("a")[current];
}
run($(link),audio[0]);
});
}
function run(link, player){
player.src = link.attr("href");
par = link.parent();
par.addClass("active").siblings().removeClass("active");
audio[0].load();
audio[0].play();
}
</script>
</head>
<body>
<audio id="audio" preload="auto" tabindex="0" controls="" type="audio/mpeg">
</audio>
<ul id="playlist">';
exec('find /www/usb/ -name "*.mp3"',$list);
$x=0;
while($x<count($list)){
if($list[$x]==$h[0]){
echo '<li class="active"><a href="$list[$x]">$list[$x]</a></li>';
$x++;
}
}
echo '
</ul>
</body>
</html>';
?>
How do I solve that? Please note that I also want to be able to search in sub directories. Thank you.
UPDATE 1 I tried @RhapX suggestion but instead the shown result is like this, and whenever I click the music1.mp3
or music2.mp3
, it doesn't play because the url goes to http://192.168.1.1/music1.mp3
and http://192.168.1.1/music2.mp3
where the actual location is at http://192.168.1.1/usb/music1.mp3