1 /** The minplayer namespace. */
  2 var minplayer = minplayer || {};
  3 
  4 /** All the media player implementations */
  5 minplayer.players = minplayer.players || {};
  6 
  7 /**
  8  * @constructor
  9  * @extends minplayer.display
 10  * @class The base media player class where all media players derive from.
 11  *
 12  * @param {object} context The jQuery context.
 13  * @param {object} options This components options.
 14  * @param {function} ready The callback function when the player is ready.
 15  */
 16 minplayer.players.base = function(context, options, ready) {
 17 
 18   /** The ready pointer to be called when the player is ready. */
 19   this.readyCallback = ready;
 20 
 21   // Derive from display
 22   minplayer.display.call(this, context, options);
 23 };
 24 
 25 /** Derive from minplayer.display. */
 26 minplayer.players.base.prototype = new minplayer.display();
 27 
 28 /** Reset the constructor. */
 29 minplayer.players.base.prototype.constructor = minplayer.players.base;
 30 
 31 /**
 32  * Get the priority of this media player.
 33  *
 34  * @return {number} The priority of this media player.
 35  */
 36 minplayer.players.base.getPriority = function() {
 37   return 0;
 38 };
 39 
 40 /**
 41  * Returns the ID for the media being played.
 42  *
 43  * @param {object} file A {@link minplayer.file} object.
 44  * @return {string} The ID for the provided media.
 45  */
 46 minplayer.players.base.getMediaId = function(file) {
 47   return '';
 48 };
 49 
 50 /**
 51  * Determine if we can play the media file.
 52  *
 53  * @param {object} file A {@link minplayer.file} object.
 54  * @return {boolean} If this player can play this media type.
 55  */
 56 minplayer.players.base.canPlay = function(file) {
 57   return false;
 58 };
 59 
 60 /**
 61  * @see minplayer.plugin.construct
 62  * @this minplayer.players.base
 63  */
 64 minplayer.players.base.prototype.construct = function() {
 65 
 66   // Set the name of this plugin.
 67   this.options.name = 'media';
 68 
 69   // Call the media display constructor.
 70   minplayer.display.prototype.construct.call(this);
 71 
 72   // Reset the variables to initial state.
 73   this.reset();
 74 
 75   /** The currently loaded media file. */
 76   this.mediaFile = this.options.file;
 77 
 78   // Get the player display object.
 79   if (!this.playerFound()) {
 80 
 81     // Cleanup some events and code.
 82     this.display.unbind();
 83 
 84     // Remove the media element if found
 85     if (this.elements.media) {
 86       this.elements.media.remove();
 87     }
 88 
 89     // Create a new media player element.
 90     this.display.html(this.create());
 91   }
 92 
 93   // Get the player object...
 94   this.media = this.getMedia();
 95 };
 96 
 97 /**
 98  * Clears all the intervals.
 99  */
100 minplayer.players.base.prototype.clearIntervals = function() {
101   // Stop the intervals.
102   if (this.playInterval) {
103     clearInterval(this.playInterval);
104   }
105 
106   if (this.progressInterval) {
107     clearInterval(this.progressInterval);
108   }
109 };
110 
111 /**
112  * Resets all variables.
113  */
114 minplayer.players.base.prototype.reset = function() {
115 
116   // Reset the ready flag.
117   this.ready = false;
118 
119   // The duration of the player.
120   this.duration = new minplayer.async();
121 
122   // The current play time of the player.
123   this.currentTime = new minplayer.async();
124 
125   // The amount of bytes loaded in the player.
126   this.bytesLoaded = new minplayer.async();
127 
128   // The total amount of bytes for the media.
129   this.bytesTotal = new minplayer.async();
130 
131   // The bytes that the download started with.
132   this.bytesStart = new minplayer.async();
133 
134   // The current volume of the player.
135   this.volume = new minplayer.async();
136 
137   // Stop the intervals.
138   this.clearIntervals();
139 
140   // Set the intervals to zero.
141   this.playInterval = 0;
142   this.progressInterval = 0;
143 };
144 
145 /**
146  * Called when the player is ready to recieve events and commands.
147  */
148 minplayer.players.base.prototype.onReady = function() {
149   // Store the this pointer.
150   var _this = this;
151 
152   // Set the ready flag.
153   this.ready = true;
154 
155   // Set the volume to the default.
156   this.setVolume(this.options.volume / 100);
157 
158   // Create a progress interval to keep track of the bytes loaded.
159   this.progressInterval = setInterval(function() {
160 
161     // Get the bytes loaded asynchronously.
162     _this.getBytesLoaded(function(bytesLoaded) {
163 
164       // Get the bytes total asynchronously.
165       _this.getBytesTotal(function(bytesTotal) {
166 
167         // Trigger an event about the progress.
168         if (bytesLoaded || bytesTotal) {
169 
170           // Get the bytes start, but don't require it.
171           var bytesStart = 0;
172           _this.getBytesStart(function(val) {
173             bytesStart = val;
174           });
175 
176           // Trigger a progress event.
177           _this.trigger('progress', {
178             loaded: bytesLoaded,
179             total: bytesTotal,
180             start: bytesStart
181           });
182         }
183       });
184 
185     });
186   }, 1000);
187 
188   // Call the callback to let this person know we are ready.
189   if (this.readyCallback) {
190     this.readyCallback(this);
191   }
192 
193   // Trigger that the load has started.
194   this.trigger('loadstart');
195 };
196 
197 /**
198  * Should be called when the media is playing.
199  */
200 minplayer.players.base.prototype.onPlaying = function() {
201   // Store the this pointer.
202   var _this = this;
203 
204   // Trigger an event that we are playing.
205   this.trigger('playing');
206 
207   // Create a progress interval to keep track of the bytes loaded.
208   this.playInterval = setInterval(function() {
209 
210     // Get the current time asyncrhonously.
211     _this.getCurrentTime(function(currentTime) {
212 
213       // Get the duration asynchronously.
214       _this.getDuration(function(duration) {
215 
216         // Convert these to floats.
217         currentTime = parseFloat(currentTime);
218         duration = parseFloat(duration);
219 
220         // Trigger an event about the progress.
221         if (currentTime || duration) {
222 
223           // Trigger an update event.
224           _this.trigger('timeupdate', {
225             currentTime: currentTime,
226             duration: duration
227           });
228         }
229       });
230     });
231   }, 1000);
232 };
233 
234 /**
235  * Should be called when the media is paused.
236  */
237 minplayer.players.base.prototype.onPaused = function() {
238 
239   // Trigger an event that we are paused.
240   this.trigger('pause');
241 
242   // Stop the play interval.
243   clearInterval(this.playInterval);
244 };
245 
246 /**
247  * Should be called when the media is complete.
248  */
249 minplayer.players.base.prototype.onComplete = function() {
250   // Stop the intervals.
251   this.clearIntervals();
252   this.trigger('ended');
253 };
254 
255 /**
256  * Should be called when the media is done loading.
257  */
258 minplayer.players.base.prototype.onLoaded = function() {
259   this.trigger('loadeddata');
260 };
261 
262 /**
263  * Should be called when the player is waiting.
264  */
265 minplayer.players.base.prototype.onWaiting = function() {
266   this.trigger('waiting');
267 };
268 
269 /**
270  * @see minplayer.players.base#isReady
271  * @return {boolean} Checks to see if the Flash is ready.
272  */
273 minplayer.players.base.prototype.isReady = function() {
274 
275   // Return that the player is set and the ready flag is good.
276   return (this.media && this.ready);
277 };
278 
279 /**
280  * Determines if the player should show the playloader.
281  *
282  * @return {bool} If this player implements its own playLoader.
283  */
284 minplayer.players.base.prototype.hasPlayLoader = function() {
285   return false;
286 };
287 
288 /**
289  * Returns if the media player is already within the DOM.
290  *
291  * @return {boolean} TRUE - if the player is in the DOM, FALSE otherwise.
292  */
293 minplayer.players.base.prototype.playerFound = function() {
294   return false;
295 };
296 
297 /**
298  * Creates the media player and inserts it in the DOM.
299  *
300  * @return {object} The media player entity.
301  */
302 minplayer.players.base.prototype.create = function() {
303   this.reset();
304   return null;
305 };
306 
307 /**
308  * Returns the media player object.
309  *
310  * @return {object} The media player object.
311  */
312 minplayer.players.base.prototype.getMedia = function() {
313   return this.media;
314 };
315 
316 /**
317  * Loads a new media player.
318  *
319  * @param {object} file A {@link minplayer.file} object.
320  */
321 minplayer.players.base.prototype.load = function(file) {
322 
323   // Store the media file for future lookup.
324   if (file) {
325     this.reset();
326     this.mediaFile = file;
327   }
328 };
329 
330 /**
331  * Play the loaded media file.
332  */
333 minplayer.players.base.prototype.play = function() {
334 };
335 
336 /**
337  * Pause the loaded media file.
338  */
339 minplayer.players.base.prototype.pause = function() {
340 };
341 
342 /**
343  * Stop the loaded media file.
344  */
345 minplayer.players.base.prototype.stop = function() {
346   // Stop the intervals.
347   this.clearIntervals();
348 };
349 
350 /**
351  * Seek the loaded media.
352  *
353  * @param {number} pos The position to seek the minplayer. 0 to 1.
354  */
355 minplayer.players.base.prototype.seek = function(pos) {
356 
357 };
358 
359 /**
360  * Set the volume of the loaded minplayer.
361  *
362  * @param {number} vol The volume to set the media. 0 to 1.
363  */
364 minplayer.players.base.prototype.setVolume = function(vol) {
365   this.trigger('volumeupdate', vol);
366 };
367 
368 /**
369  * Get the volume from the loaded media.
370  *
371  * @param {function} callback Called when the volume is determined.
372  * @return {number} The volume of the media; 0 to 1.
373  */
374 minplayer.players.base.prototype.getVolume = function(callback) {
375   return this.volume.get(callback);
376 };
377 
378 /**
379  * Get the current time for the media being played.
380  *
381  * @param {function} callback Called when the time is determined.
382  * @return {number} The volume of the media; 0 to 1.
383  */
384 minplayer.players.base.prototype.getCurrentTime = function(callback) {
385   return this.currentTime.get(callback);
386 };
387 
388 /**
389  * Return the duration of the loaded media.
390  *
391  * @param {function} callback Called when the duration is determined.
392  * @return {number} The duration of the loaded media.
393  */
394 minplayer.players.base.prototype.getDuration = function(callback) {
395   return this.duration.get(callback);
396 };
397 
398 /**
399  * Return the start bytes for the loaded media.
400  *
401  * @param {function} callback Called when the start bytes is determined.
402  * @return {int} The bytes that were started.
403  */
404 minplayer.players.base.prototype.getBytesStart = function(callback) {
405   return this.bytesStart.get(callback);
406 };
407 
408 /**
409  * Return the bytes of media loaded.
410  *
411  * @param {function} callback Called when the bytes loaded is determined.
412  * @return {int} The amount of bytes loaded.
413  */
414 minplayer.players.base.prototype.getBytesLoaded = function(callback) {
415   return this.bytesLoaded.get(callback);
416 };
417 
418 /**
419  * Return the total amount of bytes.
420  *
421  * @param {function} callback Called when the bytes total is determined.
422  * @return {int} The total amount of bytes for this media.
423  */
424 minplayer.players.base.prototype.getBytesTotal = function(callback) {
425   return this.bytesTotal.get(callback);
426 };
427 
428