-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_player.js
156 lines (147 loc) · 4.13 KB
/
audio_player.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
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
/*
Audio Folder Player
by Freakk
www.freakk.net
*/
jQuery(document).ready(function($) {
var index = 0;
var max_songs = $('#playlist').attr('value');
var mute = false;
var vol = .6; /* css #vol_bar width should also be set at 60% */
var vol_bak = 1;
var countdown = false;
var mins = 0;
var secs = 0;
var loaded = 0;
var duration = 0;
var posirion = 0;
$('#title').html($('#song'+(index+1)).attr('name'));
/* CONTROL FUNCTIONS */
var play = function(){
$('#playlist audio').get(index).play();
$('#play').hide();
$('#pause').show();
$('#title').html($('#song'+(index+1)).attr('name'));
};
var pause = function(){
$('#playlist audio').get(index).pause();
$('#pause').hide();
$('#play').show();
};
var stop = function(){
$('#playlist audio').get(index).pause();
$('#playlist audio').get(index).currentTime = 0;
$('#pause').hide();
$('#play').show();
};
var next = function(){
//stop current song
$('#playlist audio').get(index).pause();
$('#playlist audio').get(index).currentTime = 0;
index++;
if(index >= max_songs) index = 0;
$('#playlist audio').get(index).play();
$('#play').hide();
$('#pause').show();
$('#title').html($('#song'+(index+1)).attr('name'));
};
var prev = function(){
//stop current song
$('#playlist audio').get(index).pause();
$('#playlist audio').get(index).currentTime = 0;
index--;
if(index < 0) index = max_songs-1;
$('#playlist audio').get(index).play();
$('#play').hide();
$('#pause').show();
$('#title').html($('#song'+(index+1)).attr('name'));
};
var playSong = function(n){
stop();
index = n;
play();
};
/* AUTOPLAY */
if( $('#player').attr('data-autoplay')=="1") play();
/* CONTROL BUTTONS */
$('#play').click(function() { play(); });
$('#pause').click(function() { pause();});
$('#stop').click(function() { stop(); });
$('#ffwd').click(function() {
$('#playlist audio').get(index).currentTime += .5;
});
$('#fbwd').click(function() {
$('#playlist audio').get(index).currentTime -= .5;
});
$('#next').click(function() { next(); });
$('#prev').click(function() { prev(); });
$('#timer').click(function() {
if(countdown) countdown=false;
else countdown=true;
});
/* BIND PROCESS */
$("audio").bind('timeupdate', function(){
duration = $('#playlist audio').get(index).duration;
position = $('#playlist audio').get(index).currentTime;
loaded = $('#playlist audio').get(index).buffered.end($('#playlist audio').get(index).buffered.length-1);
if(position >= duration){ next(); };
if(countdown==false){
mins = parseInt(position / 60);
secs = parseInt(position - mins * 60);
} else {
eta = duration - position;
mins = parseInt( eta / 60);
secs = parseInt( eta - mins * 60);
}
// double digit
if(mins<10) mins = "0"+mins.toString();
if(secs<10) secs = "0"+secs.toString();
var widthOfBufferBar = Math.floor( 100 / duration * loaded);
var widthOfProgressBar = Math.floor( 100 / duration * position);
$('#progressbar').css({
'width':widthOfProgressBar+'%'
});
$('#progressbar_buffer').css({
'width':widthOfBufferBar+'%'
});
$('#timer').html( mins+":"+secs);
$('#playlist audio').get(index).volume = vol;
});
/* PROGRESS BAR */
$('#progressbar_container').click(function(e) {
var offset = $(this).offset();
pos =(100*(e.clientX - offset.left)/($('#progressbar_container').width() ));
$('#playlist audio').get(index).currentTime = $('#playlist audio').get(index).duration*pos/100;
});
/* MUTE */
$('#vol_on').click(function() {
$('#vol_off').show();
$(this).hide();
vol_bak = vol;
vol = 0;
mute = true;
});
$('#vol_off').click(function() {
$('#vol_on').show();
$(this).hide();
vol=vol_bak;
mute = false;
});
/* VOLUME CONTROL */
$('#vol_bar_container').click(function(e) {
var offset = $(this).offset();
pos = parseInt(100*(e.clientX - offset.left)/($('#vol_bar_container').width() ));
width = $('#vol_bar_container').attr('width');
$('#vol_bar').css({'width':pos+'%'});
if(mute){ vol_bak = pos/100; }
else {vol = pos/100 };
});
/* PLAYLIST */
$('#playlist_display_btn').click(function() {
$('#playlist_container').fadeToggle('fast');
});
$('.song_name').click(function() {
playSong($(this).attr('value')-1);
});
});