▶이동
  • 상구너 닷컴 블로그를 방문하신 여러분을 환영합니다 :)
  • 스팸 정책에 의해 일부 덧글·방명록차단될 수 있습니다.

PHP html5 css3 음악 플레이어.

Develop 상구너(sanguneo) 2014. 10. 18. 22:07

php로 음악파일 목록을 불러와 playlist 로 만들고 자동재생되도록 만들어보았습니다.

1차적으론 개인 파일서버에있는 음악들을 들으려고 만들었는데, 

이정도면 배포해도 괜찮겠지 싶어서 일단은 배포합니다.

파일은 <name>.php 로 음악이 있는 폴더에 저장하면 됩니다. 파일을 직접 올리지는 않겠습니다.

php 에서 같은 디렉토리 내의 파일목록과 디렉토리목록을 리스트업해서 재생해줍니다.

우분투 환경에서 테스트하였고, 윈도우의 경우는 php가 한글을 인식하는게 문제가 있어서 안될듯..


재배포와 수정은 자유이지만 처음 주석부분은 수정하지 않았으면 좋겠다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
/**
 *
 * 구너플레이어 14.10
 * 제이쿼리플러그인을 따로 두고싶으면 부분 수정해도 괜찮습니다.
 *
**/
function endsWith($haystack, $needle)
{
    return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
}
 
function humanFileSize($size)
{
    if ($size >= 1073741824) {
        $fileSize = round($size / 1024 / 1024 / 1024, 1) . 'GB';
    } elseif ($size >= 1048576) {
        $fileSize = round($size / 1024 / 1024, 1) . 'MB';
    } elseif ($size >= 1024) {
        $fileSize = round($size / 1024, 1) . 'KB';
    } else {
        $fileSize = $size . ' bytes';
    }
    return $fileSize;
}
 
function dirlist()
{
    $default_dir = ".";
    if (!$dh = @opendir($default_dir)) {
        return false;
    }
    $dirlist[] = "..";
    $filelist2 = array();
    while (($file = readdir($dh)) !== false) {
        if ($file == "." ||$file == ".." || !is_dir($file))
            continue; // . 과 .. 디렉토리는 무시
        $filelist2[] = $file;
    }
    sort($filelist2);
    $dirlist = array_merge($dirlist,$filelist2);
    foreach ($dirlist as $dirname) {
        echo "<ul class=\"listentity\" data=\"" . $dirname . "\">\n<li class=\"mtitle2\">\n" . $dirname . "</li>\n\n</ul>\n";
         
    }
    closedir($df);
}
 
function filelist()
{
    $default_dir = ".";
    if (!$dh = @opendir($default_dir)) {
        return false;
    }
     
    while (($file = readdir($dh)) !== false) {
        if ($file == "." || $file == ".." || !endsWith($file, "mp3"))
            continue; // . 과 .. 디렉토리는 무시
        $filelist[] = $file . "|" . humanFileSize(filesize($file));
    }
    sort($filelist);
    $filefirst = explode("|",$filelist[1])[0];
    foreach ($filelist as $filename) {
        echo "\t<ul class=\"listentity\" data=\"".explode("|",$filename)[0]."\">\n\t\t<li class=\"mtitle\">".explode("|",$filename)[0]."</li>\n\t\t<li class=\"mcurrent\">▶</li>\n\t\t<li class=\"msize\"><a href=\"".explode("|",$filename)[0]."\">".explode("|",$filename)[1]."</a></li>\n\t</ul>\n";
         
    }
    $filefirst = explode("|",$filelist[0])[0];
    closedir($df);
}
 
?>
 
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
    content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, target-densitydpi=medium-dpi" />
<title>#music_title</title>
<style>
    body {
        margin: 0;
        padding: 0;
    }
 
    .headC {
        position: relative;
        height: 100px;
        width: 100%;
        top: 0;
        left: 0
    }
 
    #headI {
        position: fixed;
        background-color: white;
        border-bottom: gray 1px solid;
        z-index: 100;
    }
 
    .space {
        width: 100%;
        height: 10%
    }
 
    #headI #audio audio {
        width: 100%;
        height: 100%;
    }
 
    #musictitle {
        cursor: pointer;
        height: 30%;
        width: 100%;
        text-align: center;
        white-space: nowrap;
        text-overflow: ellipsis;
        -o-text-overflow: ellipsis;
        overflow: hidden;
        -moz-binding: url('ellipsis.xml#ellipsis');
    }
 
    audio {
        width: 100%;
    }
 
    .tailC {
        position: relative;
        height: 50px;
        width: 100%;
        bottom: 0;
        left: 0
    }
 
    #tailI {
        position: fixed;
        background-color: white;
        text-align: center;
        z-index: 100;
    }
 
    #tailI input {
        width: 15%;
        height: 100%;
        font-size: 20pt;
        -webkit-appearance: button;
        -webkit-border-radius: 0;
    }
 
    #dirlist {
        padding-top: 20px;
    }
 
    #dirlist .listentity:last-child {
        border-bottom: 0
    }
 
    #playlist {
        padding-bottom: 20px;
    }
 
    .listentity {
        -webkit-margin-before: 0;
        -webkit-margin-after: 0;
        -webkit-padding-start: 10px;
        -webkit-padding-end: 10px;
        width: calc(100% - 20px);
        height: 30px;
        margin: 0 auto;
        border-top: 1px gray dashed;
        position: relative;
        clear: left;
        list-style: none;
        cursor: pointer
    }
 
    .listentity:hover {
        background-color: #CCCCCC
    }
 
    .listentity:last-child {
        border-bottom: 1px gray dashed;
    }
 
    .listentity li {
        line-height: 30px;
        vertical-align: middle;
        font-size: 10pt;
        float: left
    }
 
    .mtitle {
        width: 77%;
        white-space: nowrap;
        text-overflow: ellipsis;
        -o-text-overflow: ellipsis;
        overflow: hidden;
        -moz-binding: url('ellipsis.xml#ellipsis');
    }
 
    .mtitle2 {
        width: 100%;
        white-space: nowrap;
        text-overflow: ellipsis;
        -o-text-overflow: ellipsis;
        overflow: hidden;
        -moz-binding: url('ellipsis.xml#ellipsis');
    }
 
    .mcurrent {
        width: 5%;
        color: transparent
    }
 
    .msize {
        text-align: right;
        width: 8%;
        padding-left: 5%;
    }
 
    .active {
        font-weight: bold;
    }
 
    .active .mcurrent {
        color: black
    }
 
    a, a:hover, a:link, a:visited, a:active {
        color: gray;
        text-decoration: none;
    }
</style>
</head>
<body>
    <div id="headI" class="headC">
        <div class="space"></div>
        <div id="musictitle" title="클릭하시면 노래제목 전체가 출력됩니다">#music title</div>
        <audio id="audio" autoplay controls src="<?php echo $filefirst;?>"></audio>
        <div class="space"></div>
    </div>
    <div id="fakehead" class="headC"></div>
    <!--플레이리스트 시작-->
    <div id="dirlist">
<?php echo dirlist();?>
    </div>
    <div id="playlist">
<?php echo filelist();?>
    </div>
    <!--플레이리스트 종료-->
    <div id="tailI" class="tailC">
        <input id="prev" type="button" value="<<" />
        <input id="play" type="button" value="||"/>
        <input id="next" type="button" value=">>"/>
        <input id="loop" type="button" value="@"/>
    </div>
    <div id="faketail" class="tailC"></div>
    <script language="javascript" type="text/javascript">
    var audio;
    var playlist;
    var tracks;
    var current;
    var loop = false;
     
    init();
     
    function init() {
        current = 0;
        audio = $('#audio');
        playlist = $('#playlist');
        tracks = playlist.find('ul');
        len = tracks.length - 1;
        audio[0].play();
        playlist.find('ul').click(function(e) {
            link = $(this);
            current = link.index();
            run(link, audio[0]);
        });
        audio[0].addEventListener('ended', function(e) {
            if(!loop){
                current++;
                if (current == len + 1) {
                    current = 0;
                    link = playlist.find('ul')[0];
                } else {
                    link = playlist.find('ul')[current];
                }
            }
            run($(link), audio[0]);
        });
        audio[0].addEventListener("pause", function() {
            $('#play').attr("value", ">");
        });
        audio[0].addEventListener("play", function() {
            $('#play').attr("value", "||");
        });
    }
     
    function run(link, player) {
        player.src = link.attr('data');
        par = link;
        $('#musictitle').html(link.attr('data'));
        document.title = link.attr('data');
        par.addClass('active').siblings().removeClass('active');
        audio[0].load();
        audio[0].play();
        $('#play').attr("value", ">");
    }
    $('#prev').click(function() {
        current--;
        if (current == -1) {
            current = len;
        }
        link = playlist.find('ul')[current];
        run($(link), audio[0]);
    });
    $('#play').click(function() {
        if (audio[0].paused == false) {
            audio[0].pause();
            $('#play').attr("value", ">");
        } else {
            audio[0].play();
            $('#play').attr("value", "||");
        }
    });
    $('#next').click(function() {
        current++;
        if (current == len + 1) {
            current = 0;
            link = playlist.find('ul')[0];
        } else {
            link = playlist.find('ul')[current];
        }
        run($(link), audio[0]);
    });
    $('#loop').click(function() {
        if(!loop){
            loop = true;
            $('#loop').attr("value", "!@");
        }else{
            loop = false;
            $('#loop').attr("value", "@");
        }
    });
     
    $('#dirlist').find('ul').click(function(e) {
        location.href = $(this).attr("data");
    });
     
    $('#musictitle').click(function() {
        alert($('#musictitle').html());
    });
     
    $(document).ready(function() {
        var event = document.createEvent("HTMLEvents");
        event.initEvent("click", true, false);
        playlist.find('ul')[0].dispatchEvent(event);
        var browserCheckText = new Array('iPhone', 'iPod', 'BlackBerry', 'Android', 'Windows CE', 'LG', 'MOT', 'SAMSUNG', 'SonyEricsson', 'Symbian', 'Windows Phone', 'webOS', 'Opera Mini', 'Opera Mobi', 'POLARIS', 'IEMobile', 'nokia');
        for (var word in browserCheckText) {
            if (navigator.userAgent.toUpperCase().match(browserCheckText[word].toUpperCase()) != null) {
                $('#play').attr("value", ">");
                audio[0].pause();
                break;
            }else{
                $('#play').attr("value", "||");
            }
        }
    });
</script>
</body>
</html>


One file php html5 css3 jquery music player.


이름도 한번 지어볼까..? 구너플?


'Develop' 카테고리의 다른 글

구너갤러리 15.11.2  (0) 2015.11.16
구너갤러리 15.11  (0) 2015.11.04
구너 플레이어 15.11  (1) 2015.11.04
베지에 곡선 (Bezier Curve)  (1) 2015.09.04
[구너 플레이어] 15.8 단일파일 php 음악 플레이어.  (4) 2015.08.06
1