Last active 1440820623

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

Revision febe42ed6e4e293441767b21850f97a727a9d53d

mp3list.php Raw
1<?php
2if (isset($_GET['print'])) {
3 highlight_file(__FILE__);
4 die();
5}
6function load_addon($filename) {
7 if (file_exists($filename)) {
8 include $filename;
9 }
10}
11$arr = array();
12foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.', FilesystemIterator::SKIP_DOTS)) as $item => $file) {
13 $arr[] = $item;
14}
15sort($arr);
16?>
17<html>
18<head>
19<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
20<style>
21div.filelist {
22 overflow-y: scroll;
23 height: 90%;
24}
25</style>
26<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
27<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
28<script>
29
30var audio = document.createElement('audio');
31
32function playRandom() {
33 try {
34 var list = $('ul').find('li');
35 set_src($(list[Math.floor(Math.random()*list.length)]).find('a')[0].innerHTML);
36 } catch (err) {
37 playRandom();
38 }
39}
40
41function set_src(filename) {
42 window.location.hash = filename.replace(/ /g, "%20");
43 document.title = filename;
44 try {
45 audio.setAttribute('src', filename);
46 audio.load();
47 audio.play();
48 $('#np').html(filename.slice(0, -4));
49 return false;
50 } catch(err) {
51 location.reload();
52 }
53}
54
55function setVolume(volume) {
56 audio.volume = volume;
57}
58
59$(window).load(function() {
60 $("#volume").slider({
61 min: 0,
62 max: 100,
63 value: (audio.volume) * 100,
64 range: "min",
65 animate: true,
66 slide: function(event, ui) {
67 setVolume((ui.value) / 100);
68 }
69 });
70 audio.setAttribute('src', '');
71 audio.setAttribute('controls', 'controls');
72 audio.setAttribute('id', 'audio');
73 $('form').prepend(audio);
74 audio.load();
75 audio.addEventListener("ended", function () {
76 if (document.getElementById('autoplay').checked) {
77 playRandom();
78 }
79 });
80 if (window.location.hash != "" || window.location.hash != "#") {
81 set_src(window.location.hash.substring(1));
82 } else {
83 set_src("<?php echo substr(current($arr), 2); ?>");
84 }
85});
86</script>
87</head>
88<body>
89<form onsubmit="return false;">
90 <input type="button" onclick="playRandom(); return false;" value="Random">
91 <label for="autoplay">Autoplay</label><input type="checkbox" name="autoplay" id="autoplay" checked>
92 <b><span id="np"></span></b>
93</form>
94<div id="volume" style="width: 25%"></div>
95<div class="filelist">
96<ul>
97<?php
98foreach($arr as $item) {
99 if (mime_content_type($item) == "audio/mpeg") {
100 $item = substr($item, 2); ?>
101 <li><a href="#<?php echo htmlentities($item); ?>" onClick='return set_src("<?php echo str_replace("&quot;", "&#92;&quot;", htmlentities($item, ENT_QUOTES)); ?>");'><?php echo $item; ?></a></li>
102<?php
103 }
104}
105?>
106</ul>
107</div>
108</body>
109</html>