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  * @return {number} The priority of this media player.
 82  */
 83 minplayer.players.minplayer.getPriority = function() {
 84   return 1;
 85 };
 86 
 87 /**
 88  * @see minplayer.players.base#canPlay
 89  * @return {boolean} If this player can play this media type.
 90  */
 91 minplayer.players.minplayer.canPlay = function(file) {
 92   var isWEBM = jQuery.inArray(file.mimetype, ['video/x-webm',
 93     'video/webm',
 94     'application/octet-stream'
 95   ]) >= 0;
 96   return !isWEBM && (file.type == 'video' || file.type == 'audio');
 97 };
 98 
 99 /**
100  * @see minplayer.players.base#create
101  * @return {object} The media player entity.
102  */
103 minplayer.players.minplayer.prototype.create = function() {
104 
105   // Make sure we provide default options...
106   this.options = jQuery.extend({
107     swfplayer: 'flash/minplayer.swf'
108   }, this.options);
109 
110   minplayer.players.flash.prototype.create.call(this);
111 
112   // The flash variables for this flash player.
113   var flashVars = {
114     'id': this.options.id,
115     'debug': this.options.debug,
116     'config': 'nocontrols',
117     'file': this.mediaFile.path,
118     'autostart': this.options.autoplay,
119     'autoload': this.options.autoload
120   };
121 
122   // Return a flash media player object.
123   return this.getFlash({
124     swf: this.options.swfplayer,
125     id: this.options.id + '_player',
126     width: '100%',
127     height: '100%',
128     flashvars: flashVars,
129     wmode: this.options.wmode
130   });
131 };
132 
133 /**
134  * Called when the Flash player has an update.
135  *
136  * @param {string} eventType The event that was triggered in the player.
137  */
138 minplayer.players.minplayer.prototype.onMediaUpdate = function(eventType) {
139   switch (eventType) {
140     case 'mediaMeta':
141       this.onLoaded();
142       break;
143     case 'mediaPlaying':
144       if (this.minplayerloaded) {
145         this.onPlaying();
146       }
147       break;
148     case 'mediaPaused':
149       this.minplayerloaded = true;
150       this.onPaused();
151       break;
152     case 'mediaComplete':
153       this.onComplete();
154       break;
155   }
156 };
157 
158 /**
159  * Resets all variables.
160  */
161 minplayer.players.minplayer.prototype.clear = function() {
162   minplayer.players.flash.prototype.clear.call(this);
163   this.minplayerloaded = this.options.autoplay;
164 };
165 
166 /**
167  * @see minplayer.players.base#load
168  * @return {boolean} If this action was performed.
169  */
170 minplayer.players.minplayer.prototype.load = function(file) {
171   if (minplayer.players.flash.prototype.load.call(this, file)) {
172     this.player.loadMedia(file.path, file.stream);
173     return true;
174   }
175 
176   return false;
177 };
178 
179 /**
180  * @see minplayer.players.base#play
181  * @return {boolean} If this action was performed.
182  */
183 minplayer.players.minplayer.prototype.play = function() {
184   if (minplayer.players.flash.prototype.play.call(this)) {
185     this.player.playMedia();
186     return true;
187   }
188 
189   return false;
190 };
191 
192 /**
193  * @see minplayer.players.base#pause
194  * @return {boolean} If this action was performed.
195  */
196 minplayer.players.minplayer.prototype.pause = function() {
197   if (minplayer.players.flash.prototype.pause.call(this)) {
198     this.player.pauseMedia();
199     return true;
200   }
201 
202   return false;
203 };
204 
205 /**
206  * @see minplayer.players.base#stop
207  * @return {boolean} If this action was performed.
208  */
209 minplayer.players.minplayer.prototype.stop = function() {
210   if (minplayer.players.flash.prototype.stop.call(this)) {
211     this.player.stopMedia();
212     return true;
213   }
214 
215   return false;
216 };
217 
218 /**
219  * @see minplayer.players.base#seek
220  * @return {boolean} If this action was performed.
221  */
222 minplayer.players.minplayer.prototype.seek = function(pos) {
223   if (minplayer.players.flash.prototype.seek.call(this, pos)) {
224     this.player.seekMedia(pos);
225     return true;
226   }
227 
228   return false;
229 };
230 
231 /**
232  * @see minplayer.players.base#setVolume
233  * @return {boolean} If this action was performed.
234  */
235 minplayer.players.minplayer.prototype.setVolume = function(vol) {
236   if (minplayer.players.flash.prototype.setVolume.call(this, vol)) {
237     this.player.setVolume(vol);
238     return true;
239   }
240 
241   return false;
242 };
243 
244 /**
245  * @see minplayer.players.base#getVolume
246  */
247 minplayer.players.minplayer.prototype.getVolume = function(callback) {
248   if (this.isReady()) {
249     callback(this.player.getVolume());
250   }
251 };
252 
253 /**
254  * @see minplayer.players.flash#getDuration
255  */
256 minplayer.players.minplayer.prototype.getDuration = function(callback) {
257   if (this.isReady()) {
258 
259     // Check to see if it is immediately available.
260     var duration = this.player.getDuration();
261     if (duration) {
262       callback(duration);
263     }
264     else {
265 
266       // If not, then poll every second for the duration.
267       this.poll((function(player) {
268         return function() {
269           duration = player.player.getDuration();
270           if (duration) {
271             callback(duration);
272           }
273           return !duration;
274         };
275       })(this), 1000);
276     }
277   }
278 };
279 
280 /**
281  * @see minplayer.players.base#getCurrentTime
282  */
283 minplayer.players.minplayer.prototype.getCurrentTime = function(callback) {
284   if (this.isReady()) {
285     callback(this.player.getCurrentTime());
286   }
287 };
288 
289 /**
290  * @see minplayer.players.base#getBytesLoaded
291  */
292 minplayer.players.minplayer.prototype.getBytesLoaded = function(callback) {
293   if (this.isReady()) {
294     callback(this.player.getMediaBytesLoaded());
295   }
296 };
297 
298 /**
299  * @see minplayer.players.base#getBytesTotal.
300  */
301 minplayer.players.minplayer.prototype.getBytesTotal = function(callback) {
302   if (this.isReady()) {
303     callback(this.player.getMediaBytesTotal());
304   }
305 };
306