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 Flash media player class to control the flash fallback.
 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.minplayer = function(context, options, queue) {
 17 
 18   // Derive from players flash.
 19   minplayer.players.flash.call(this, context, options, queue);
 20 };
 21 
 22 /** Derive from minplayer.players.flash. */
 23 minplayer.players.minplayer.prototype = new minplayer.players.flash();
 24 
 25 /** Reset the constructor. */
 26 minplayer.players.minplayer.prototype.constructor = minplayer.players.minplayer;
 27 
 28 /**
 29  * @see minplayer.plugin.construct
 30  * @this minplayer.players.minplayer
 31  */
 32 minplayer.players.minplayer.prototype.construct = function() {
 33 
 34   // Call the players.flash constructor.
 35   minplayer.players.flash.prototype.construct.call(this);
 36 
 37   // Set the plugin name within the options.
 38   this.options.pluginName = 'minplayer';
 39 };
 40 
 41 /**
 42  * Called when the Flash player is ready.
 43  *
 44  * @param {string} id The media player ID.
 45  */
 46 window.onFlashPlayerReady = function(id) {
 47   var media = minplayer.get(id, 'media');
 48   var i = media.length;
 49   while (i--) {
 50     media[i].onReady();
 51   }
 52 };
 53 
 54 /**
 55  * Called when the Flash player updates.
 56  *
 57  * @param {string} id The media player ID.
 58  * @param {string} eventType The event type that was triggered.
 59  */
 60 window.onFlashPlayerUpdate = function(id, eventType) {
 61   var media = minplayer.get(id, 'media');
 62   var i = media.length;
 63   while (i--) {
 64     media[i].onMediaUpdate(eventType);
 65   }
 66 };
 67 
 68 /**
 69  * Used to debug from the Flash player to the browser console.
 70  *
 71  * @param {string} debug The debug string.
 72  */
 73 window.onFlashPlayerDebug = function(debug) {
 74   if (console && console.log) {
 75     console.log(debug);
 76   }
 77 };
 78 
 79 /**
 80  * @see minplayer.players.base#getPriority
 81  * @param {object} file A {@link minplayer.file} object.
 82  * @return {number} The priority of this media player.
 83  */
 84 minplayer.players.minplayer.getPriority = function(file) {
 85   // Force this player if the stream is set.
 86   return file.stream ? 100 : 1;
 87 };
 88 
 89 /**
 90  * @see minplayer.players.base#canPlay
 91  * @return {boolean} If this player can play this media type.
 92  */
 93 minplayer.players.minplayer.canPlay = function(file) {
 94 
 95   // If this has a stream, then the minplayer must play it.
 96   if (file.stream) {
 97     return true;
 98   }
 99 
100   var isWEBM = jQuery.inArray(file.mimetype, [
101     'video/x-webm',
102     'video/webm',
103     'application/octet-stream'
104   ]) >= 0;
105   return !isWEBM && (file.type == 'video' || file.type == 'audio');
106 };
107 
108 /**
109  * @see minplayer.players.base#create
110  * @return {object} The media player entity.
111  */
112 minplayer.players.minplayer.prototype.create = function() {
113 
114   // Make sure we provide default options...
115   this.options = jQuery.extend({
116     swfplayer: 'flash/minplayer.swf'
117   }, this.options);
118 
119   minplayer.players.flash.prototype.create.call(this);
120 
121   // The flash variables for this flash player.
122   var flashVars = {
123     'id': this.options.id,
124     'debug': this.options.debug,
125     'config': 'nocontrols',
126     'file': this.mediaFile.path,
127     'stream': this.mediaFile.stream,
128     'autostart': this.options.autoplay,
129     'autoload': this.options.autoload
130   };
131 
132   // Return a flash media player object.
133   return this.getFlash({
134     swf: this.options.swfplayer,
135     id: this.options.id + '_player',
136     width: '100%',
137     height: '100%',
138     flashvars: flashVars,
139     wmode: this.options.wmode
140   });
141 };
142 
143 /**
144  * Called when the Flash player has an update.
145  *
146  * @param {string} eventType The event that was triggered in the player.
147  */
148 minplayer.players.minplayer.prototype.onMediaUpdate = function(eventType) {
149   switch (eventType) {
150     case 'mediaMeta':
151       this.onLoaded();
152       break;
153     case 'mediaPlaying':
154       if (this.minplayerloaded) {
155         this.onPlaying();
156       }
157       break;
158     case 'mediaPaused':
159       this.minplayerloaded = true;
160       this.onPaused();
161       break;
162     case 'mediaComplete':
163       this.onComplete();
164       break;
165   }
166 };
167 
168 /**
169  * Resets all variables.
170  */
171 minplayer.players.minplayer.prototype.clear = function() {
172   minplayer.players.flash.prototype.clear.call(this);
173   this.minplayerloaded = this.options.autoplay;
174 };
175 
176 /**
177  * @see minplayer.players.base#load
178  * @return {boolean} If this action was performed.
179  */
180 minplayer.players.minplayer.prototype.load = function(file) {
181   if (minplayer.players.flash.prototype.load.call(this, file)) {
182     this.player.loadMedia(file.path, file.stream);
183     return true;
184   }
185 
186   return false;
187 };
188 
189 /**
190  * @see minplayer.players.base#play
191  * @return {boolean} If this action was performed.
192  */
193 minplayer.players.minplayer.prototype.play = function() {
194   if (minplayer.players.flash.prototype.play.call(this)) {
195     this.player.playMedia();
196     return true;
197   }
198 
199   return false;
200 };
201 
202 /**
203  * @see minplayer.players.base#pause
204  * @return {boolean} If this action was performed.
205  */
206 minplayer.players.minplayer.prototype.pause = function() {
207   if (minplayer.players.flash.prototype.pause.call(this)) {
208     this.player.pauseMedia();
209     return true;
210   }
211 
212   return false;
213 };
214 
215 /**
216  * @see minplayer.players.base#stop
217  * @return {boolean} If this action was performed.
218  */
219 minplayer.players.minplayer.prototype.stop = function() {
220   if (minplayer.players.flash.prototype.stop.call(this)) {
221     this.player.stopMedia();
222     return true;
223   }
224 
225   return false;
226 };
227 
228 /**
229  * @see minplayer.players.base#seek
230  * @return {boolean} If this action was performed.
231  */
232 minplayer.players.minplayer.prototype.seek = function(pos) {
233   if (minplayer.players.flash.prototype.seek.call(this, pos)) {
234     this.player.seekMedia(pos);
235     return true;
236   }
237 
238   return false;
239 };
240 
241 /**
242  * @see minplayer.players.base#setVolume
243  * @return {boolean} If this action was performed.
244  */
245 minplayer.players.minplayer.prototype.setVolume = function(vol) {
246   if (minplayer.players.flash.prototype.setVolume.call(this, vol)) {
247     this.player.setVolume(vol);
248     return true;
249   }
250 
251   return false;
252 };
253 
254 /**
255  * @see minplayer.players.base#getVolume
256  */
257 minplayer.players.minplayer.prototype.getVolume = function(callback) {
258   if (this.isReady()) {
259     callback(this.player.getVolume());
260   }
261 };
262 
263 /**
264  * @see minplayer.players.flash#getDuration
265  */
266 minplayer.players.minplayer.prototype.getDuration = function(callback) {
267   if (this.isReady()) {
268 
269     // Check to see if it is immediately available.
270     var duration = this.player.getDuration();
271     if (duration) {
272       callback(duration);
273     }
274     else {
275 
276       // If not, then poll every second for the duration.
277       this.poll((function(player) {
278         return function() {
279           duration = player.player.getDuration();
280           if (duration) {
281             callback(duration);
282           }
283           return !duration;
284         };
285       })(this), 1000);
286     }
287   }
288 };
289 
290 /**
291  * @see minplayer.players.base#getCurrentTime
292  */
293 minplayer.players.minplayer.prototype.getCurrentTime = function(callback) {
294   if (this.isReady()) {
295     callback(this.player.getCurrentTime());
296   }
297 };
298 
299 /**
300  * @see minplayer.players.base#getBytesLoaded
301  */
302 minplayer.players.minplayer.prototype.getBytesLoaded = function(callback) {
303   if (this.isReady()) {
304     callback(this.player.getMediaBytesLoaded());
305   }
306 };
307 
308 /**
309  * @see minplayer.players.base#getBytesTotal.
310  */
311 minplayer.players.minplayer.prototype.getBytesTotal = function(callback) {
312   if (this.isReady()) {
313     callback(this.player.getMediaBytesTotal());
314   }
315 };
316