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.players.base
 10  * @class The vimeo media player.
 11  *
 12  * @param {object} context The jQuery context.
 13  * @param {object} options This components options.
 14  * @param {object} queue The event queue to pass events around.
 15  */
 16 minplayer.players.vimeo = function(context, options, queue) {
 17 
 18   // Derive from players base.
 19   minplayer.players.base.call(this, context, options, queue);
 20 };
 21 
 22 /** Derive from minplayer.players.base. */
 23 minplayer.players.vimeo.prototype = new minplayer.players.base();
 24 
 25 /** Reset the constructor. */
 26 minplayer.players.vimeo.prototype.constructor = minplayer.players.vimeo;
 27 
 28 /**
 29  * @see minplayer.plugin.construct
 30  * @this minplayer.players.vimeo
 31  */
 32 minplayer.players.vimeo.prototype.construct = function() {
 33 
 34   // Call the players.flash constructor.
 35   minplayer.players.base.prototype.construct.call(this);
 36 
 37   // Set the plugin name within the options.
 38   this.options.pluginName = 'vimeo';
 39 };
 40 
 41 /**
 42  * @see minplayer.players.base#getPriority
 43  * @param {object} file A {@link minplayer.file} object.
 44  * @return {number} The priority of this media player.
 45  */
 46 minplayer.players.vimeo.getPriority = function(file) {
 47   return 10;
 48 };
 49 
 50 /**
 51  * @see minplayer.players.base#canPlay
 52  * @return {boolean} If this player can play this media type.
 53  */
 54 minplayer.players.vimeo.canPlay = function(file) {
 55 
 56   // Check for the mimetype for vimeo.
 57   if (file.mimetype === 'video/vimeo') {
 58     return true;
 59   }
 60 
 61   // If the path is a vimeo path, then return true.
 62   return (file.path.search(/^http(s)?\:\/\/(www\.)?vimeo\.com/i) === 0);
 63 };
 64 
 65 /**
 66  * Determines if the player should show the playloader.
 67  *
 68  * @param {string} preview The preview image.
 69  * @return {bool} If this player implements its own playLoader.
 70  */
 71 minplayer.players.vimeo.prototype.hasPlayLoader = function(preview) {
 72   return minplayer.hasTouch || !preview;
 73 };
 74 
 75 /**
 76  * Determines if the player should show the playloader.
 77  *
 78  * @return {bool} If this player implements its own playLoader.
 79  */
 80 minplayer.players.vimeo.prototype.hasController = function() {
 81   return minplayer.hasTouch;
 82 };
 83 
 84 /**
 85  * Return the ID for a provided media file.
 86  *
 87  * @param {object} file A {@link minplayer.file} object.
 88  * @return {string} The ID for the provided media.
 89  */
 90 minplayer.players.vimeo.getMediaId = function(file) {
 91   var regex = /^http[s]?\:\/\/(www\.)?vimeo\.com\/(\?v\=)?([0-9]+)/i;
 92   if (file.path.search(regex) === 0) {
 93     return file.path.match(regex)[3];
 94   }
 95   else {
 96     return file.path;
 97   }
 98 };
 99 
100 /**
101  * Returns a preview image for this media player.
102  *
103  * @param {object} file A {@link minplayer.file} object.
104  * @param {string} type The type of image.
105  * @param {function} callback Called when the image is retrieved.
106  */
107 minplayer.players.vimeo.getImage = function(file, type, callback) {
108   jQuery.ajax({
109     url: 'http://vimeo.com/api/v2/video/' + file.id + '.json',
110     dataType: 'jsonp',
111     success: function(data) {
112       callback(data[0].thumbnail_large);
113     }
114   });
115 };
116 
117 /**
118  * @see minplayer.players.base#reset
119  */
120 minplayer.players.vimeo.prototype.reset = function() {
121 
122   // Reset the flash variables..
123   minplayer.players.base.prototype.reset.call(this);
124 };
125 
126 /**
127  * @see minplayer.players.base#create
128  * @return {object} The media player entity.
129  */
130 minplayer.players.vimeo.prototype.create = function() {
131   minplayer.players.base.prototype.create.call(this);
132 
133   // Insert the Vimeo Froogaloop player.
134   var tag = document.createElement('script');
135   tag.src = 'http://a.vimeocdn.com/js/froogaloop2.min.js';
136   var firstScriptTag = document.getElementsByTagName('script')[0];
137   firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
138 
139   // Create the iframe for this player.
140   var iframe = document.createElement('iframe');
141   iframe.setAttribute('id', this.options.id + '-player');
142   iframe.setAttribute('type', 'text/html');
143   iframe.setAttribute('width', '100%');
144   iframe.setAttribute('height', '100%');
145   iframe.setAttribute('frameborder', '0');
146   jQuery(iframe).addClass('vimeo-player');
147 
148   // Get the source.
149   var src = 'http://player.vimeo.com/video/';
150   src += this.mediaFile.id + '?';
151 
152   // Add the parameters to the src.
153   src += jQuery.param({
154     'wmode': 'opaque',
155     'api': 1,
156     'player_id': this.options.id + '-player',
157     'title': 0,
158     'byline': 0,
159     'portrait': 0,
160     'autoplay': this.options.autoplay,
161     'loop': this.options.loop
162   });
163 
164   // Set the source of the iframe.
165   iframe.setAttribute('src', src);
166 
167   // Now register this player when the froogaloop code is loaded.
168   this.poll((function(player) {
169     return function() {
170       if (window.Froogaloop) {
171         player.player = window.Froogaloop(iframe);
172         var playerTimeout = 0;
173         player.player.addEvent('ready', function() {
174           clearTimeout(playerTimeout);
175           player.onReady();
176           player.onError('');
177         });
178         playerTimeout = setTimeout(function() {
179           player.onReady();
180           player.onError('Unable to play video.');
181         }, 2000);
182       }
183       return !window.Froogaloop;
184     };
185   })(this), 200);
186 
187   // Trigger that the load has started.
188   this.trigger('loadstart');
189 
190   // Return the player.
191   return iframe;
192 };
193 
194 /**
195  * @see minplayer.players.base#onReady
196  */
197 minplayer.players.vimeo.prototype.onReady = function(player_id) {
198 
199   // Add the other listeners.
200   this.player.addEvent('loadProgress', (function(player) {
201     return function(progress) {
202       player.duration.set(parseFloat(progress.duration));
203       player.bytesLoaded.set(progress.bytesLoaded);
204       player.bytesTotal.set(progress.bytesTotal);
205     };
206   })(this));
207 
208   this.player.addEvent('playProgress', (function(player) {
209     return function(progress) {
210       player.duration.set(parseFloat(progress.duration));
211       player.currentTime.set(parseFloat(progress.seconds));
212     };
213   })(this));
214 
215   this.player.addEvent('play', (function(player) {
216     return function() {
217       player.onPlaying();
218     };
219   })(this));
220 
221   this.player.addEvent('pause', (function(player) {
222     return function() {
223       player.onPaused();
224     };
225   })(this));
226 
227   this.player.addEvent('finish', (function(player) {
228     return function() {
229       player.onComplete();
230     };
231   })(this));
232 
233   minplayer.players.base.prototype.onReady.call(this);
234   this.onLoaded();
235 };
236 
237 /**
238  * Clears the media player.
239  */
240 minplayer.players.vimeo.prototype.clear = function() {
241   if (this.player) {
242     this.player.api('unload');
243   }
244   minplayer.players.base.prototype.clear.call(this);
245 };
246 
247 /**
248  * @see minplayer.players.base#load
249  * @return {boolean} If this action was performed.
250  */
251 minplayer.players.vimeo.prototype.load = function(file) {
252   if (minplayer.players.base.prototype.load.call(this, file)) {
253     this.construct();
254     return true;
255   }
256   return false;
257 };
258 
259 /**
260  * @see minplayer.players.base#play
261  * @return {boolean} If this action was performed.
262  */
263 minplayer.players.vimeo.prototype.play = function() {
264   if (minplayer.players.base.prototype.play.call(this)) {
265     this.player.api('play');
266     return true;
267   }
268 
269   return false;
270 };
271 
272 /**
273  * @see minplayer.players.base#pause
274  * @return {boolean} If this action was performed.
275  */
276 minplayer.players.vimeo.prototype.pause = function() {
277   if (minplayer.players.base.prototype.pause.call(this)) {
278     this.player.api('pause');
279     return true;
280   }
281 
282   return false;
283 };
284 
285 /**
286  * @see minplayer.players.base#stop
287  * @return {boolean} If this action was performed.
288  */
289 minplayer.players.vimeo.prototype.stop = function() {
290   if (minplayer.players.base.prototype.stop.call(this)) {
291     this.player.api('unload');
292     return true;
293   }
294 
295   return false;
296 };
297 
298 /**
299  * @see minplayer.players.base#seek
300  * @return {boolean} If this action was performed.
301  */
302 minplayer.players.vimeo.prototype.seek = function(pos) {
303   if (minplayer.players.base.prototype.seek.call(this, pos)) {
304     this.player.api('seekTo', pos);
305     return true;
306   }
307 
308   return false;
309 };
310 
311 /**
312  * @see minplayer.players.base#setVolume
313  * @return {boolean} If this action was performed.
314  */
315 minplayer.players.vimeo.prototype.setVolume = function(vol) {
316   if (minplayer.players.base.prototype.setVolume.call(this, vol)) {
317     this.volume.set(vol);
318     this.player.api('setVolume', vol);
319     return true;
320   }
321 
322   return false;
323 };
324 
325 /**
326  * @see minplayer.players.base#getVolume
327  */
328 minplayer.players.vimeo.prototype.getVolume = function(callback) {
329   this.player.api('getVolume', function(vol) {
330     callback(vol);
331   });
332 };
333 
334 /**
335  * @see minplayer.players.base#getDuration.
336  */
337 minplayer.players.vimeo.prototype.getDuration = function(callback) {
338   if (this.isReady()) {
339     if (this.duration.value) {
340       callback(this.duration.value);
341     }
342     else {
343       this.player.api('getDuration', function(duration) {
344         callback(duration);
345       });
346     }
347   }
348 };
349