From: stevenaw Date: Tue, 8 Mar 2011 02:57:15 +0000 (-0500) Subject: [#280] Wrapper coded, unit tests written X-Git-Tag: v0.4~6^2~13 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=ac7935ddb563ab10159aad71d58831e54aeb4961;p=popcorn-js.git [#280] Wrapper coded, unit tests written --- diff --git a/players/vimeo/froogaloop.min.js b/players/vimeo/froogaloop.min.js new file mode 100644 index 0000000..f911b71 --- /dev/null +++ b/players/vimeo/froogaloop.min.js @@ -0,0 +1,5 @@ +var Froogaloop=function(){var e={hasWindowEvent:false,PLAYER_DOMAIN:"",eventCallbacks:{},iframe_pattern:/player\.(([a-zA-Z0-9_\.]+)\.)?vimeo(ws)?\.com\/video\/([0-9]+)/i},j=function(b){b||(b=document.getElementsByTagName("iframe"));for(var a,c=0,f=b.length,g;c0?a.apply(null,b):a.call()},d={r20:/%20/g,isArray:function(b){return Object.prototype.toString.call(b)=== +"[object Array]"},isFunction:function(b){return Object.prototype.toString.call(b)==="[object Function]"},unserialize:function(b){if(!b)return false;var a={};b=b.split("&");for(var c,f,g=0;g-1)f=d.unserialize(f);a[c]=f}return a},s:false,serialize:function(b){d.s=[];for(var a in b)d.buildParams(a,b[a]);return d.s.join("&").replace(d.r20,"+")},buildParams:function(b,a){var c=0;if(d.isArray(a)){for(;c + + + Popcorn Vimeo Player Example + + + + + + + + + + +
+
+

Description

+

+ This demo will showcase how a Vimeo flash video can be integrated into Popcorn. This is done by making the flash video masquerade as HTML 5 video.
+ Due to the Flash security model, this demo must be run from a web server
+ From 5 to 40 seconds, the footnote 'The video is "RiP: A Remix Manifesto", by Brett Gaylor' will appear below 'Footnote Area'. +


+ Vimeo does not support their player to be chromeless (hidden controls), but custsom controls have been developed and tied into their player.
+ Clicking play/pause or seeking in either the video or via custsom controls will cause the other to update.
+ Clicking on the embed option in the Vimeo player while the video is playing will cause the video to pause and custom controls to update.
+ The vimeo video can be specified in the HTML source by giving the vimeo web site url (http://vimeo.com/VIDEOID) to either the VimeoEngine or as the iframe src attribute.
+

+
+
+
+
+ Footnote Area
+
+
+ + + + + + + +
+ Current Time (s): +
Video Duration (s): +
Volume (0-1): +
Load Status: Not Started +
+
+ + \ No newline at end of file diff --git a/players/vimeo/popcorn.vimeo.js b/players/vimeo/popcorn.vimeo.js new file mode 100644 index 0000000..2ac1b29 --- /dev/null +++ b/players/vimeo/popcorn.vimeo.js @@ -0,0 +1,478 @@ +// Popcorn Vimeo Player Wrapper +( function( Popcorn ) { + /** + * Vimeo wrapper for Popcorn. + * This player adds enables Popcorn.js to handle Vimeo videos. It does so by masking an embedded Vimeo video iframe + * as a video and implementing the HTML5 Media Element interface. + * + * To use this plug-in, include the Vimeo JavaScript API Froogaloop in the parent HTML page as well as the embedded Vimeo iframe: + * + * + * + * You can specify the video in three ways: + * 1. Use the embed code supplied by Vimeo, and pass the iframe id into a new Popcorn.VimeoEngine object + * + * + * + * + * 2. Use an empty iframe and give both the iframe id and the web url when creating a new Popcorn.VimeoEngine + * + * + * + * + * 3. Set the iframe's src attribute to the vimeo video's web url, and pass the id when creating a new Popcorn.VimeoEngine + * + * + * + * + * Due to Vimeo's API, certain events must be subscribed to at different times, and some not at all. + * These events are completely custom-implemented and may be subscribed to at any time: + * durationchange + * load + * loadstart + * play + * readystatechange + * volumechange + * + * These events are related to player functionality and must be subscribed to during or after the load event: + * abort + * emptied + * ended + * pause + * playing + * progress + * seeked + * timeupdate + * + * These events are not supported: + * canplay + * canplaythrough + * error + * loadeddata + * loadedmetadata + * ratechange + * seeking + * stalled + * suspend + * waiting + * + * Due to Vimeo's API, some attributes are be supported while others are not. + * Supported media attributes: + * autoplay ( via Popcorn ) + * currentTime ( get only, set by calling setCurrentTime() ) + * duration ( get only ) + * ended ( get only ) + * initialTime ( get only, always 0 ) + * loop ( get only, set by calling setLoop() ) + * muted ( get only, set by calling setVolume(0) ) + * paused ( get only ) + * readyState ( get only ) + * src ( get only ) + * volume ( get only, set by calling setVolume() ) + * + * load() function + * + * Unsupported media attributes: + * buffered + * defaultPlaybackRate + * networkState + * playbackRate + * played + * preload + * seekable + * seeking + * startOffsetTime + */ + + // base object for DOM-related behaviour like events + var LikeADOM = function ( owner ) { + var evts = {}; + var makeHandler = function( evtName ) { + if ( !evts[evtName] ) { + evts[evtName] = []; + + // Create a wrapper function to all registered listeners + this["on"+evtName] = function() { + Popcorn.forEach( evts[evtName], function( fn ) { + if ( fn ) { + fn.call( owner ); + } + }); + } + } + }; + + return { + addEventListener: function( evtName, fn, doFire ) { + evtName = evtName.toLowerCase(); + + makeHandler.call( this, evtName ); + evts[evtName].push( fn ); + + if ( doFire ) { + dispatchEvent( evtName ); + } + + return fn; + }, + // Add many listeners for a single event + // Takes an event name and array of functions + addEventListeners: function( evtName, events ) { + evtName = evtName.toLowerCase(); + + makeHandler.call( this, evtName ); + evts[evtName] = evts[evtName].concat( events ); + }, + removeEventListener: function( evtName, fn ) { + var evtArray = this.getEventListeners( evtName ), + i, + l; + + // Find and remove from events array + for ( i = 0, l = evtArray.length; i < l; i++) { + if ( evtArray[i] === fn ) { + var removed = evtArray[i]; + evtArray[i] = 0; + return removed; + } + } + }, + getEventListeners: function( evtName ) { + if( evtName ) { + return evts[ evtName.toLowerCase() ] || []; + } else { + return evts; + } + }, + dispatchEvent: function( evt ) { + // If event object was passed in, toString will yield event type as string (timeupdate) + // If a string, toString() will return the string itself (timeupdate) + var evt = "on"+evt.toString().toLowerCase(); + this[evt] && this[evt](); + } + }; + }; + + Popcorn.vimeo = function( mediaId, list ) { + return new Popcorn.vimeo.init( mediaId, list ); + }; + + // A constructor, but we need to wrap it to allow for "static" functions + Popcorn.vimeo.init = (function() { + var rPlayerUri = /^http:\/\/player\.vimeo\.com\/video\/[\d]+/i, + rWebUrl = /vimeo\.com\/[\d]+/, + registry = [], + hasAPILoaded = false; + + // Source file is from https://github.com/vimeo/froogaloop/blob/master/froogaloop.min.js + // HTTPS seems to have some difficulties with getScript, so store locally + Popcorn.getScript( "./froogaloop.min.js", function() { + var i, + l = registry.length; + + hasAPILoaded = true; + for( i=0; i 1 ) { + val %= 1; + } + + // HTML video expects to be 0.0 -> 1.0, Vimeo expects 0-100 + this.objVars.volume = val; + this.swfObj.api( "api_setVolume", val*100 ); + this.evtHolder.dispatchEvent( "volumechange" ); + }, + // Seeks the video + currentTime: function ( time ) { + if ( !time && time !== 0 ) { + return this.objVars.currentTime; + } + + this.objVars.currentTime = time; + this.objVars.ended = time < this.objVars.duration; + this.swfObj.api( "api_seekTo", time ); + + // Fire events for seeking and time change + this.evtHolder.dispatchEvent( "seeked" ); + this.evtHolder.dispatchEvent( "timeupdate" ); + }, + // Play the video + play: function() { + if ( !this.objVars.played ) { + this.objVars.played = 1; + this.evtHolder.dispatchEvent( "loadstart" ); + } + + this.evtHolder.dispatchEvent( "play" ); + this.swfObj.api( "api_play" ); + }, + // Pauses the video + pause: function() { + this.swfObj.api( "api_pause" ); + }, + duration: function() { + return this.objVars.duration; + }, + muted: function() { + return this.objVars.volume === 0; + }, + // Force loading by playing the player. Pause afterwards + load: function() { + this.play(); + this.pause(); + }, + unload: function() { + this.pause(); + + this.swfObj.api( "api_unload" ); + this.evtHolder.dispatchEvent( "abort" ); + this.evtHolder.dispatchEvent( "emptied" ); + }, + // Hook an event listener for the player event into internal event system + // Stick to HTML conventions of add event listener and keep lowercase, without prependinng "on" + addEventListener: function( evt, fn ) { + var playerEvt, + that = this; + + evt = evt.toLowerCase(); + + // If it's an HTML media event supported by player, map + if ( evt === "seeked" ) { + playerEvt = "onSeek"; + } else if ( evt === "timeupdate" ) { + playerEvt = "onProgress"; + } else if ( evt === "progress" ) { + playerEvt = "onLoading"; + } else if ( evt === "ended" ) { + playerEvt = "onFinish"; + } else if ( evt === "playing" ) { + playerEvt = "onPlay"; + } else if ( evt === "pause" || evt === "load" ) { + // Direct mapping, CamelCase the event name as vimeo API expects + playerEvt = "on"+evt[0].toUpperCase() + evt.substr(1); + } + + // Vimeo only stores 1 callback per event + // Have vimeo call internal collection of callbacks + this.evtHolder.addEventListener( evt, fn, false ); + + // Link manual event structure with Vimeo's if not already + // Do not link for 'timeupdate' + if( evt !== "timeupdate" && playerEvt && this.evtHolder.getEventListeners( evt ).length === 1 ) { + this.swfObj.addEvent( playerEvt, function() { + that.evtHolder.dispatchEvent( evt ); + }); + } + }, + removeEventListener: function( evtName, fn ) { + return this.evtHolder.removeEventListener( evtName, fn ); + } + }); +})( Popcorn ); \ No newline at end of file diff --git a/players/vimeo/popcorn.vimeo.unit.html b/players/vimeo/popcorn.vimeo.unit.html new file mode 100644 index 0000000..7be1d61 --- /dev/null +++ b/players/vimeo/popcorn.vimeo.unit.html @@ -0,0 +1,28 @@ + + + + Popcorn API + + + + + + + + + + + +

Popcorn Vimeo Player Plugin

+

+
+

+
    +
    + + + + diff --git a/players/vimeo/popcorn.vimeo.unit.js b/players/vimeo/popcorn.vimeo.unit.js new file mode 100644 index 0000000..471e0fd --- /dev/null +++ b/players/vimeo/popcorn.vimeo.unit.js @@ -0,0 +1,149 @@ +test("Popcorn Vimeo Plugin", function () { + function makeHandler( evtName ) { + return( function() { + var count = 0; + return function() { + count++; + + if ( count === 1 ) { + ok( true, "Event '"+evtName+"' is supported" ); + plus(); + } + } + })(); + } + + function plus() { + if ( ++count === expects ) { + start(); + } + } + + var popped = Popcorn.vimeo( "player_1", "http://vimeo.com/11127501" ), + expects = 21, + playCount = 0, + pauseCount = 0, + count = 0, + events = { + "durationchange": makeHandler( "durationchange" ), + "play": makeHandler( "play" ), + "loadstart": function() { + (makeHandler( "loadstart" ))(); + this.play(); + }, + "readystatechange": makeHandler( "readystatechange" ), + "volumechange": (function() { + var timesCalled = 0; + + return function() { + timesCalled++; + + if (timesCalled === 1) { + ok( true, "Volume changed event works" ); + plus(); + + equals( popped.volume(), 0, "Volume correctly set" ); + plus(); + + ok( popped.video.muted(), "Muted set when player volume 0" ); + plus(); + + popped.volume( 1 ); + } else if ( timesCalled === 2 ) { + ok( !popped.video.muted(), "Unmuted when volume not 0" ); + plus(); + + popped.exec( 4, function() { + popped.video.unload(); + }); + } + }; + })(), + "emptied": makeHandler( "emptied" ), + "ended": 0, + "pause": (function() { + var timesCalled = 0; + + return function() { + timesCalled++; + + if ( timesCalled === 1 ) { + ok( true, "Pause event works" ); + plus(); + ok( popped.video.objVars.paused, "Pause attribute set" ); + plus(); + popped.play(); + } + }; + })(), + "playing": (function() { + var timesCalled = 0; + + return function() { + timesCalled++; + + if ( timesCalled === 1 ) { + ok( true, "Play event works" ); + plus(); + + ok( true, "Video autoplay attribute works" ); + plus(); + + ok( !popped.video.objVars.paused, "Player paused attribute not set" ); + plus(); + + popped.exec( 5, function() { + popped.currentTime( 0 ); + popped.pause(); + }); + } else if ( timesCalled === 2 ) { + ok( true, "Popcorn play function works" ); + plus(); + popped.volume( 0 ); + } + }; + })(), + "progress": makeHandler( "progress" ), + "seeked": makeHandler( "seeked" ), + "timeupdate": makeHandler( "timeupdate" ) + }; + + expect( expects ); + + stop( 20000 ); + + ok( "vimeo" in Popcorn, "Vimeo is a method of Popcorn" ); + plus(); + + equals( popped.video.objVars.initialTime, 0, "Player initial time is 0" ); + plus(); + + Popcorn.plugin( "timingTest", { + start: function ( event, options ) { + ok( true, "Popcorn plugin started" ); + plus(); + }, + end: function ( event, options ) { + ok( true, "Popcorn plugin stopped" ); + plus(); + } + }); + + popped.timingTest({ + start: 3, // seconds + end: 4, // seconds + }); + + popped.listen( "load", function() { + ok ( this.video.swfObj.id === popped.video.swfObj.id, "Correctly kept track of 'this'" ); + plus(); + + Popcorn.forEach( events, function( fn, evtName ) { + fn && popped.listen( evtName, fn ); + }); + + // Queue load + // Load event will play when beginning to load + popped.load(); + }); +}); diff --git a/popcorn.js b/popcorn.js index 17d20e3..4bb3a53 100644 --- a/popcorn.js +++ b/popcorn.js @@ -78,14 +78,17 @@ } - matches = rIdExp.exec( entity ); - - if ( matches.length && matches[2] ) { - elem = document.getElementById(matches[2]); - } + if ( (typeof entity) == "string" ) { + matches = rIdExp.exec( entity ); + if ( matches.length && matches[2] ) { + elem = document.getElementById(matches[2]); + } - this.video = elem ? elem : null; + this.video = elem ? elem : null; + } else if ( entity instanceof Object ) { + this.video = entity; + } this.data = { history: [], @@ -128,6 +131,11 @@ tracksByEnd = tracks.byEnd, tracksByStart = tracks.byStart; + + if (typeof currentTime === "function") { + currentTime = this.currentTime(); + } + // Playbar advancing if ( previousTime < currentTime ) { @@ -214,7 +222,9 @@ } }; - isReady( this ); + if ( this.video ) { + isReady( this ); + } return this; } @@ -296,20 +306,23 @@ ret[ name ] = function( arg ) { - if ( typeof this.video[name] === "function" ) { - this.video[ name ](); - - return this; - } - + var isFunc = typeof this.video[name] === "function"; if ( arg !== false && arg !== null && typeof arg !== "undefined" ) { - this.video[ name ] = arg; + if ( isFunc ) { + this.video[ name ](arg); + } else { + this.video[ name ] = arg; + } return this; } + if ( isFunc ) { + return this.video[ name ](); + } + return this.video[ name ]; }; }); @@ -902,7 +915,7 @@ settings.ajax.open( settings.type, settings.url, settings.async ); - settings.ajax.send( settings.data = null ? null : settings.data ); + settings.ajax.send( settings.data || null ); return Popcorn.xhr.httpData( settings ); } @@ -1102,3 +1115,4 @@ }, false ); })(window, window.document); +