forked from DarshanMaradiya/OnlineMusic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudioPlayer.js
168 lines (153 loc) · 4.97 KB
/
audioPlayer.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
157
158
159
160
161
162
163
164
165
166
167
/*
Default constructor configuration:
autoplay: false,
shuffle: false,
loop: false,
playerId: "audioPlayer",
playlistId: "playlist",
currentClass: "current-song"
Methods:
setLoop
setShuffle
toggleShuffle
toggleLoop
prevTrack
nextTrack
Can access player by .player variable
example playlist.player.pause();
*/
class AudioPlaylist{
randomizeOrder(){
for (var i = this.trackOrder.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = this.trackOrder[i];
this.trackOrder[i] = this.trackOrder[j];
this.trackOrder[j] = temp;
}
return this.trackOrder;
}
setTrack(arrayPos){
var liPos = this.trackOrder[arrayPos]; // convert array index to html index
this.player.src = $("#"+this.playlistId+ " li a").eq(liPos).attr("href");
$("."+this.currentClass).removeClass(this.currentClass);
$("#"+this.playlistId+ " li").eq(liPos).addClass(this.currentClass);
this.trackPos = arrayPos; // update based on array index position
}
prevTrack(){
if(this.trackPos == 0)
this.setTrack(this.length-1);
else
this.setTrack(this.trackPos - 1);
this.player.play();
}
nextTrack(){
// if track isn't the last track in array of tracks, go to next track
if(this.trackPos < this.length - 1)
this.setTrack(this.trackPos+1);
else{
if(this.shuffle)
this.randomizeOrder();
this.setTrack(0);
}
this.player.play();
}
setLoop(val){
if(val === true)
this.loop = true;
else
this.loop = false;
return this.loop;
}
setShuffle(val){
if(val == this.shuffle) // if no change
return val;
else{
if(val === true){
this.randomizeOrder();
this.shuffle = true;
}
else{
this.shuffle = false;
// empty track array, fill array with indexs in order
this.trackOrder = [];
for(var i = 0; i < this.length; i++){
this.trackOrder.push(i);
}
// jump array to track position of currently playing track
this.trackPos = this.trackOrder.indexOf($("."+this.currentClass).index());
}
return this.shuffle;
}
}
toggleShuffle(){
if(this.shuffle === true)
this.setShuffle(false);
else
this.setShuffle(true);
return this.shuffle;
}
toggleLoop(){
if(this.loop === true)
this.setLoop(false);
else
this.setLoop(true);
return this.loop;
}
constructor(config = {} ){
/***
*
* setting defaults, and initialzing player
*
*/
var classObj = this; // store scope for event listeners
this.shuffle = (config.shuffle === true) ? true : false;
this.playerId = (config.playerId) ? config.playerId : "audioPlayer";
this.playlistId = (config.playlistId) ? config.playlistId : "playlist";
this.currentClass = (config.currentClass) ? config.currentClass : "current-song"
this.length = $("#"+this.playlistId+" li").length;
this.player = $("#"+this.playerId)[0];
this.autoplay = (config.autoplay === true || this.player.autoplay) ? true : false;
this.loop = (config.loop === true) ? true : false;
this.trackPos = 0;
this.trackOrder = [];
for(var i = 0; i < this.length; i++){
this.trackOrder.push(i);
}
if(this.shuffle)
this.randomizeOrder();
this.setTrack(this.trackPos);
if(this.autoplay)
this.player.play();
/***
*
* handle link clicks
*
*/
$("#"+this.playlistId+" li a ").click(function(e){
e.preventDefault();
// set track based on index of
classObj.setTrack(classObj.trackOrder.indexOf($(this).parent().index()));
classObj.player.play();
});
/***
*
* handle end of track
*
*/
this.player.addEventListener("ended", function(){
// if last track ended
if(classObj.trackPos < classObj.length - 1){
classObj.setTrack(classObj.trackPos+1);
classObj.player.play();
}
else{
if(classObj.loop){
if(classObj.shuffle)
classObj.randomizeOrder();
classObj.setTrack(0);
classObj.player.play();
}
}
});
}
}