|
28
|
1 |
/** |
|
|
2 |
* StreamPlayer 0.12 |
|
|
3 |
* Romain Vuillemot |
|
|
4 |
*/ |
|
|
5 |
var StreamPlayer = function(player, options) { |
|
|
6 |
if(typeof(player) == 'string') |
|
|
7 |
player = document.getElementById(player); |
|
|
8 |
|
|
|
9 |
if(!player) |
|
|
10 |
return; |
|
|
11 |
|
|
|
12 |
var handle = player.getElementsByTagName('div')[0]; |
|
|
13 |
this.init(player, handle, options || {}); |
|
|
14 |
this.setup(); |
|
|
15 |
}; |
|
|
16 |
|
|
|
17 |
StreamPlayer.prototype.init = function(player, handle, options) { |
|
|
18 |
this.callback = options.callback || null; |
|
|
19 |
this.displayUpdate = options.displayUpdate || null; |
|
|
20 |
this.playCallback = options.playCallback || null; |
|
|
21 |
this.pauseCallback = options.pauseCallback || null; |
|
|
22 |
this.stopCallback = options.stopCallback || null; |
|
|
23 |
this.updateCallback = options.updateCallback || null; |
|
|
24 |
this.nextCallback = options.nextCallback || null; |
|
|
25 |
this.previousCallback = options.previousCallback || null; |
|
|
26 |
this.ffCallback = options.ffCallback || null; |
|
|
27 |
this.fbCallback = options.fbCallback || null; |
|
|
28 |
this.soundCallback = options.soundCallback || null; |
|
|
29 |
|
|
|
30 |
this.refreshCallback = options.refreshCallback || this.refreshCallback; |
|
|
31 |
this.current_time = options.current_time || null; |
|
|
32 |
|
|
|
33 |
this.play_mode = options.play_mode || "DATA_BY_DATA"; // "DATA_BY_TIME_INTERVAL" |
|
|
34 |
this.read_frequency = options.read_frequency || 1000; // "DATA_BY_TIME_INTERVAL" |
|
|
35 |
this.data_interval = options.data_interval || 1000; // "DATA_BY_TIME_INTERVAL" |
|
|
36 |
|
|
|
37 |
this.player = player; |
|
|
38 |
this.handle = handle; |
|
|
39 |
this.options = options; |
|
|
40 |
|
|
|
41 |
this.refreshIntervalId = null; |
|
|
42 |
this.buffer_count = 0; |
|
|
43 |
this.buffered_data = []; |
|
|
44 |
this.current_value = null; |
|
|
45 |
|
|
|
46 |
this.auto_start = this.getOption('auto_start', true); |
|
|
47 |
this.is_playing = this.auto_start; |
|
|
48 |
|
|
|
49 |
this.is_buffering = this.getOption('is_buffering', false); |
|
|
50 |
this.current_speed = this.getOption('current_speed', 1000); |
|
|
51 |
this.buffered_pause = true; |
|
|
52 |
|
|
|
53 |
this.addListeners(); |
|
|
54 |
|
|
|
55 |
this.min_speed = options.min_speed || 4000; |
|
|
56 |
this.max_speed = options.max_speed || 100; |
|
|
57 |
|
|
|
58 |
this.min_time = options.min_time || 0; |
|
|
59 |
this.max_time = options.max_time || -1; |
|
|
60 |
|
|
|
61 |
self = this; |
|
|
62 |
} |
|
|
63 |
|
|
|
64 |
StreamPlayer.prototype.addListeners = function() { |
|
|
65 |
|
|
|
66 |
this.player.onclick = function(e) { |
|
|
67 |
if(e.target.className.search(/(^|\s)stop(\s|$)/) != -1 && typeof(self.stopCallback) == 'function') { |
|
|
68 |
self.stopCallback(self); |
|
|
69 |
self.update(); |
|
|
70 |
} else if(e.target.className.search(/(^|\s)play(\s|$)/) != -1 && typeof(self.playCallback) == 'function') { |
|
|
71 |
self.playCallback(self); |
|
|
72 |
e.target.className = e.target.className.replace(/\s?play/g, ''); |
|
|
73 |
e.target.className += " pause"; |
|
|
74 |
self.is_buffering = false; |
|
|
75 |
self.is_playing = !self.is_playing; |
|
|
76 |
self.refreshIntervalId = setInterval(function () { |
|
|
77 |
self.updateCallback(self) |
|
|
78 |
self.update(); |
|
|
79 |
}, self.current_speed); |
|
|
80 |
} else if(e.target.className.search(/(^|\s)pause(\s|$)/) != -1 && typeof(self.pauseCallback) == 'function') { |
|
|
81 |
self.pauseCallback(); |
|
|
82 |
e.target.className = e.target.className.replace(/\s?pause/g, ''); |
|
|
83 |
e.target.className += " play"; |
|
|
84 |
clearInterval(self.refreshIntervalId); |
|
|
85 |
self.is_playing = !self.is_playing; |
|
|
86 |
self.update(); |
|
|
87 |
} else if(e.target.className.search(/(^|\s)next(\s|$)/) != -1 && typeof(self.nextCallback) == 'function') { |
|
|
88 |
self.nextCallback(self); |
|
|
89 |
self.update(); |
|
|
90 |
} else if(e.target.className.search(/(^|\s)prev(\s|$)/) != -1 && typeof(self.previousCallback) == 'function') { |
|
|
91 |
self.previousCallback(self); |
|
|
92 |
self.update(); |
|
|
93 |
} else if(e.target.className.search(/(^|\s)ff(\s|$)/) != -1 && typeof(self.ffCallback) == 'function') { |
|
|
94 |
if(self.current_speed>self.max_speed) { |
|
|
95 |
|
|
|
96 |
if(self.play_mode=="DATA_BY_DATA") |
|
|
97 |
self.current_speed = self.current_speed/2 > self.max_speed ? self.current_speed/2 : self.max_speed; |
|
|
98 |
|
|
|
99 |
if(self.is_playing) { |
|
|
100 |
clearInterval(self.refreshIntervalId); |
|
|
101 |
self.refreshIntervalId = setInterval(function () { |
|
|
102 |
self.updateCallback(self); |
|
|
103 |
self.update(); |
|
|
104 |
}, self.current_speed); |
|
|
105 |
} |
|
|
106 |
|
|
|
107 |
e.target.className.replace(/\s?disabled/g, ''); |
|
|
108 |
self.ffCallback(); |
|
|
109 |
document.getElementsByClassName("fb")[0].className = document.getElementsByClassName("fb")[0].className.replace(/\s?disabled/g, ''); |
|
|
110 |
document.getElementsByClassName("fb")[0].disabled = false; |
|
|
111 |
} |
|
|
112 |
if(self.current_speed<=self.max_speed) { |
|
|
113 |
if(e.target.className.search(/(^|\s)disabled(\s|$)/) == -1) { |
|
|
114 |
e.target.className += " disabled"; |
|
|
115 |
e.target.disabled = true; |
|
|
116 |
} |
|
|
117 |
} |
|
|
118 |
} else if(e.target.className.search(/(^|\s)fb(\s|$)/) != -1 && typeof(self.fbCallback) == 'function') { |
|
|
119 |
|
|
|
120 |
if(self.current_speed<self.min_speed) { |
|
|
121 |
|
|
|
122 |
self.current_speed = self.current_speed*2 < self.min_speed ? self.current_speed*2 : self.min_speed; |
|
|
123 |
|
|
|
124 |
if(self.is_playing) { |
|
|
125 |
clearInterval(self.refreshIntervalId); |
|
|
126 |
self.refreshIntervalId = setInterval(function () { |
|
|
127 |
self.updateCallback(self); |
|
|
128 |
self.update(); |
|
|
129 |
return ; |
|
|
130 |
}, self.current_speed); |
|
|
131 |
} |
|
|
132 |
e.target.className.replace(/\s?disabled/g, ''); |
|
|
133 |
self.fbCallback(); |
|
|
134 |
document.getElementsByClassName("ff")[0].className = document.getElementsByClassName("ff")[0].className.replace(/\s?disabled/g, ''); |
|
|
135 |
document.getElementsByClassName("ff")[0].disabled = false; |
|
|
136 |
} |
|
|
137 |
|
|
|
138 |
if(self.current_speed==self.min_speed) { |
|
|
139 |
if(e.target.className.search(/(^|\s)disabled(\s|$)/) == -1) { |
|
|
140 |
e.target.className += " disabled"; |
|
|
141 |
e.target.disabled = true; |
|
|
142 |
} |
|
|
143 |
} |
|
|
144 |
} else if(e.target.className.search(/(^|\s)soundon(\s|$)/) != -1 && typeof(self.soundCallback) == 'function') { |
|
|
145 |
e.target.className = e.target.className.replace(/\s?soundon/g, ''); |
|
|
146 |
e.target.className += " soundoff"; |
|
|
147 |
self.soundCallback(false); |
|
|
148 |
} else if(e.target.className.search(/(^|\s)soundoff(\s|$)/) != -1 && typeof(self.pauseCallback) == 'function') { |
|
|
149 |
e.target.className = e.target.className.replace(/\s?soundoff/g, ''); |
|
|
150 |
e.target.className += " soundon"; |
|
|
151 |
self.soundCallback(true); |
|
|
152 |
self.update(); |
|
|
153 |
} |
|
|
154 |
self.refreshCallback(self); |
|
|
155 |
} |
|
|
156 |
} |
|
|
157 |
|
|
|
158 |
StreamPlayer.prototype.setup = function() { |
|
|
159 |
this.addListeners(); |
|
|
160 |
this.update(); |
|
|
161 |
if((self.current_speed==self.min_speed) && document.getElementsByClassName("fb").length > 1 && document.getElementsByClassName("fb")[0].className.search(/(^|\s)disabled(\s|$)/) == -1) { |
|
|
162 |
document.getElementsByClassName("fb")[0].className += " disabled"; |
|
|
163 |
document.getElementsByClassName("fb")[0].disabled = false; |
|
|
164 |
} |
|
|
165 |
if((self.current_speed==self.min_speed) && document.getElementsByClassName("fb").length > 1 && document.getElementsByClassName("fb")[0].className.search(/(^|\s)disabled(\s|$)/) == -1) { |
|
|
166 |
document.getElementsByClassName("fb")[0].className += " disabled"; |
|
|
167 |
document.getElementsByClassName("fb")[0].disabled = false; |
|
|
168 |
} |
|
|
169 |
if(typeof(self.current_time) == 'function' && self.current_time()==0) { |
|
|
170 |
if(document.getElementsByClassName("prev").length>0 && document.getElementsByClassName("prev")[0].className.search(/(^|\s)disabled(\s|$)/) == -1) { |
|
|
171 |
document.getElementsByClassName("prev")[0].className += " disabled"; |
|
|
172 |
document.getElementsByClassName("prev")[0].disabled = true; |
|
|
173 |
} |
|
|
174 |
} else { |
|
|
175 |
if(document.getElementsByClassName("prev").length>0 && document.getElementsByClassName("prev")[0].className.search(/(^|\s)disabled(\s|$)/) == 1) { |
|
|
176 |
document.getElementsByClassName("prev")[0].className.replace(/\s?disabled/g, ''); |
|
|
177 |
document.getElementsByClassName("prev")[0].disabled = false; |
|
|
178 |
} |
|
|
179 |
} |
|
|
180 |
} |
|
|
181 |
|
|
|
182 |
StreamPlayer.prototype.getOption = function(name, defaultValue) { |
|
|
183 |
return this.options[name] !== undefined ? this.options[name] : defaultValue; |
|
|
184 |
} |
|
|
185 |
|
|
|
186 |
StreamPlayer.prototype.update = function() { |
|
|
187 |
if(self.auto_start && typeof(self.updateCallback) == 'function' && self.refreshIntervalId==null) { |
|
|
188 |
self.refreshIntervalId = setInterval(function () { |
|
|
189 |
self.updateCallback(self); |
|
|
190 |
self.update(); |
|
|
191 |
return ; |
|
|
192 |
}, self.current_speed); |
|
|
193 |
} |
|
|
194 |
if(typeof(self.current_time) == 'function' && self.current_time()==self.min_time) { |
|
|
195 |
if(document.getElementsByClassName("prev").length>0 && document.getElementsByClassName("prev")[0].className.search(/(^|\s)disabled(\s|$)/) == -1) { |
|
|
196 |
document.getElementsByClassName("prev")[0].className += " disabled"; |
|
|
197 |
document.getElementsByClassName("prev")[0].disabled = true; |
|
|
198 |
} |
|
|
199 |
} else { |
|
|
200 |
if(document.getElementsByClassName("prev").length>0 && document.getElementsByClassName("prev")[0].className.search(/(^|\s)disabled(\s|$)/)>0) { |
|
|
201 |
document.getElementsByClassName("prev")[0].className = document.getElementsByClassName("prev")[0].className.replace(/\s?disabled/g, ''); |
|
|
202 |
document.getElementsByClassName("prev")[0].disabled = false; |
|
|
203 |
} |
|
|
204 |
} |
|
|
205 |
if(typeof(self.current_time) == 'function' && self.current_time()==self.max_time) { |
|
|
206 |
if(document.getElementsByClassName("next").length>0 && document.getElementsByClassName("next")[0].className.search(/(^|\s)disabled(\s|$)/) == -1) { |
|
|
207 |
document.getElementsByClassName("next")[0].className += " disabled"; |
|
|
208 |
document.getElementsByClassName("next")[0].disabled = true; |
|
|
209 |
} |
|
|
210 |
if(self.is_playing) { |
|
|
211 |
self.pauseCallback(); |
|
|
212 |
var el = document.getElementsByClassName("pause")[0]; |
|
|
213 |
el.className = el.className.replace(/\s?pause/g, ''); |
|
|
214 |
el.className += " play"; |
|
|
215 |
clearInterval(self.refreshIntervalId); |
|
|
216 |
self.is_playing = !self.is_playing; |
|
|
217 |
self.update(); |
|
|
218 |
} |
|
|
219 |
} else { |
|
|
220 |
if(document.getElementsByClassName("next").length>0 && document.getElementsByClassName("next")[0].className.search(/(^|\s)disabled(\s|$)/)>0) { |
|
|
221 |
document.getElementsByClassName("next")[0].className = document.getElementsByClassName("next")[0].className.replace(/\s?disabled/g, ''); |
|
|
222 |
document.getElementsByClassName("next")[0].disabled = false; |
|
|
223 |
} |
|
|
224 |
} |
|
|
225 |
self.refreshCallback(self); |
|
|
226 |
} |
|
|
227 |
|
|
|
228 |
StreamPlayer.prototype.refreshCallback = function() { |
|
|
229 |
return; |
|
|
230 |
} |