-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
113 lines (91 loc) · 2.96 KB
/
plugin.js
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
// key handling - space - pause
(function($) {
$.fn.html5pl = function() {
var audioTag = $('<audio>').attr({'id':'html5p'}).append($('<source>'));
$('body').append(audioTag);
var player = $('audio').get(0);
player.songs = [];
player.state = -1;
$('a').each(function(){
var href = $(this).attr('href') || '';
if(href.match(/mp3|m4a/)){
var song = {
title: this.innerText,
url: this.href
}
player.songs.push(song);
}
});
$('html5p source').attr('src',player.songs[0].url);
(function(){
holder = $('<div>').css({
'position':'fixed',
'top':'0',
'right':'0',
'height': '20px',
'background':'yellow',
'color':'black',
'padding':'1em',
'font-family': 'Helvetica, sans'
}).addClass('player');
// controls
pause = $("<span class='play'>play</span>").css({'cursor':'pointer'});
prev = $("<span class='prev'> « </span>").css({'cursor':'pointer'});
next = $("<span class='next'> » </span>").css({'cursor':'pointer'});
holder.append(pause);
holder.append(prev);
holder.append(next);
$('body').append(holder);
})();
var playFilelist = function(pl){
$(player).find('source').attr('src',pl[0].url);
}
// play
$('.pause').live('click',function(){
if(!player.ended) {
player.pause();
}
$(this).html('play').addClass('play').removeClass('pause');
});
// pause
$('.play').live('click',function(){
player.play();
if(player.state=-1)
player.state++
$(this).addClass('pause').html('pause').removeClass('play');
});
// next
$('.next').live('click',function(e){
e.preventDefault();
player.pause();
player.state++;
if (player.state == player.songs.length)
player.state = 0
$(player).find('source').attr('src', player.songs[player.state].url);
loadAndPlay();
});
// prev
$('.prev').live('click',function(e){
e.preventDefault();
player.pause();
if (player.state > 0)
player.state--;
if (player.state == player.songs.length)
player.state = 0
$(player).find('source').attr('src', player.songs[player.state].url);
loadAndPlay();
});
var loadAndPlay = function(){
$('.play').trigger('click');
player.load();
player.play();
document.title = 'Playing: ' + player.songs[player.state].title;
}
// ended event was not fired correctly
player.addEventListener('timeupdate', function(evt) {
if(player.currentTime == player.duration)
$('.next').trigger('click');
});
playFilelist(player.songs);
};
})(jQuery);