Last active 1440820623

Produce list of MP3 files in current and sub directories, playable by clicking

Revision 57952301ae4c0585929864580367d22022cb06a5

mp3list.php Raw
1<?php
2$arr = array();
3foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.', FilesystemIterator::SKIP_DOTS)) as $item => $file) {
4 $arr[] = $item;
5}
6sort($arr);
7?>
8<html>
9<head>
10<style>
11div.filelist {
12 overflow-y: scroll;
13 height: 90%;
14}
15</style>
16<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
17<script>
18var audio = document.createElement('audio');
19
20$(window).load(function() {
21 audio.setAttribute('src', '');
22 audio.setAttribute('controls', 'controls');
23 audio.setAttribute('id', 'audio');
24 $('body').prepend(audio);
25 audio.load();
26 audio.addEventListener("ended", function () {
27 if (document.getElementById('autoplay').checked) {
28 playRandom();
29 }
30 });
31 if (window.location.hash != "" || window.location.hash != "#") {
32 set_src(window.location.hash.substring(1));
33 }
34});
35
36function playRandom() {
37 try {
38 var list = $('ul').find('li');
39 set_src($(list[Math.floor(Math.random()*list.length)]).find('a')[0].innerHTML);
40 } catch (err) {
41 playRandom();
42 }
43}
44
45function set_src(filename) {
46 audio.setAttribute('src', filename);
47 audio.load();
48 audio.play();
49 window.location.hash = filename;
50 return false;
51}
52</script>
53</head>
54<body>
55<a href="#" onclick="playRandom(); return false;">Random</a><form><input type="checkbox" name="autoplay" id="autoplay" checked>Autoplay
56<div class="filelist">
57<ul>
58<?php
59foreach($arr as $item) {
60 if (mime_content_type($item) == "audio/mpeg") {
61 $item = substr($item, 2); ?>
62 <li><a href="#<?php echo $item; ?>" onClick="return set_src('<?php echo $item; ?>');"><?php echo $item; ?></a></li>
63<?php } }
64?>
65</ul>
66</div>
67</body>
68</html>
69