// Loop
document.getElementById( "btnLoop" ).addEventListener( "click", function() {
loop = loop === "loop" ? "" : "loop";
- player.loop( loop );
+ player.video.setLoop( loop );
}, false);
// Volume
//On document ready
document.addEventListener( "DOMContentLoaded", function() {
- var player = Popcorn.vimeo( "player_1", "http://vimeo.com/11127501" )
+ var player = Popcorn( Popcorn.vimeo( "player_1", "http://vimeo.com/11127501" ) )
.footnote({
start: 5, // seconds
end: 40, // seconds
</div>
</div>
<iframe id="player_1" width="500" height="281" frameborder="0"></iframe>
+ <br />
+ <br />
<button class="simple" id="btnPlay">Play</button>
<button class="simple" id="btnPause">Pause</button>
<button class="seek" id="btnSeek">Seek to 30</button>
};
};
+ var timeupdateInterval = 33,
+ timeCheckInterval = 0.75;
+
Popcorn.vimeo = function( mediaId, list ) {
return new Popcorn.vimeo.init( mediaId, list );
};
var matches = url.match( rWebUrl );
return matches ? matches[0].substr(10) : "";
};
-
+
// If container id is not supplied, assumed to be same as player id
var ctor = function ( containerId, videoUrl ) {
if ( !containerId ) {
var vidId,
that = this;
- this.swfObj = document.getElementById( containerId ),
- this.addEventFn,
- this.evtHolder,
- this.popped,
- this.autoplay,
- this.readyState = 0,
- this.objVars;
+ this.swfObj = document.getElementById( containerId );
+ this.addEventFn;
+ this.evtHolder;
+ this.autoplay;
+ this.paused = true;
+ this.duration = Number.MAX_VALUE;
+ this.ended = 0;
+ this.currentTime = 0;
+ this.volume = 1;
+ this.loop = 0;
+ this.initialTime = 0;
+ this.played = 0;
+ this.readyState = 0;
+
+ this.previousCurrentTime = this.currentTime;
+ this.previousVolume = this.volume;
if ( !this.swfObj ) {
throw "Invalid id, could not find it!";
this.evtHolder = new LikeADOM( this );
this.autoplay = this.swfObj.autoplay;
- // Tracking variables for the video player
- // Better to store them here than on the DOM
- this.objVars = {
- paused: true,
- duration: Number.NaN,
- ended: 0,
- currentTime: Number.NaN,
- volume: 1,
- loop: 0,
- initialTime: 0,
- played: 0,
- };
-
// Try and get a video id from a vimeo site url
// Try either from ctor param or from iframe itself
if( videoUrl ) {
var loadingFn;
that.swfObj.get( "api_getDuration", function( duration ) {
- that.objVars.duration = duration;
+ that.duration = parseFloat( duration );
that.evtHolder.dispatchEvent( "durationchange" );
});
// Done by Getting the Current Time while the video plays
that.swfObj.addEvent( "onProgress", function() {
that.swfObj.get( "api_getCurrentTime", function( time ) {
- that.objVars.currentTime = parseFloat( time );
+ that.currentTime = parseFloat( time );
that.evtHolder.dispatchEvent( "timeupdate" );
});
});
// Add pause listener to keep track of playing state
that.addEventListener( "pause", function() {
- that.objVars.paused = true;
+ that.paused = true;
});
// Add play listener to keep track of playing state
that.addEventListener( "playing", function() {
- that.objVars.paused = false;
- that.objVars.ended = 0;
+ that.paused = false;
+ that.ended = 0;
});
// Add ended listener to keep track of playing state
that.addEventListener( "ended", function() {
- if ( that.objVars.loop !== "loop" ) {
- that.objVars.paused = true;
- that.objVars.ended = 1;
+ if ( that.loop !== "loop" ) {
+ that.paused = true;
+ that.ended = 1;
}
});
that.evtHolder.removeEventListener( "progress", loadingFn );
});
});
-
- // 'this' is a video wrapper, so put into Popcorn and return the result
- this.popped = Popcorn( this );
- return this.popped;
}
return ctor;
})();
// Sequence object prototype
Popcorn.extend( Popcorn.vimeo.prototype, {
// Do everything as functions instead of get/set
- loop: function( val ) {
+ setLoop: function( val ) {
if ( !val ) {
- return this.objVars.loop;
+ return;
}
- this.objVars.loop = val;
+ this.loop = val;
var isLoop = val === "loop" ? 1 : 0;
// HTML convention says to loop if value is 'loop'
this.swfObj.api('api_setLoop', isLoop );
},
// Set the volume as a value between 0 and 1
- volume: function( val ) {
+ setVolume: function( val ) {
if ( !val && val !== 0 ) {
- return this.objVars.volume;
+ return;
}
// Normalize in case outside range of expected values
}
// HTML video expects to be 0.0 -> 1.0, Vimeo expects 0-100
- this.objVars.volume = val;
+ this.volume = this.previousVolume = val;
this.swfObj.api( "api_setVolume", val*100 );
this.evtHolder.dispatchEvent( "volumechange" );
},
// Seeks the video
- currentTime: function ( time ) {
+ setCurrentTime: function ( time ) {
if ( !time && time !== 0 ) {
- return this.objVars.currentTime;
+ return;
}
- this.objVars.currentTime = time;
- this.objVars.ended = time < this.objVars.duration;
+ this.currentTime = this.previousCurrentTime = time;
+ this.ended = time >= this.duration;
this.swfObj.api( "api_seekTo", time );
// Fire events for seeking and time change
},
// Play the video
play: function() {
- if ( !this.objVars.played ) {
- this.objVars.played = 1;
+ if ( !this.played ) {
+ this.played = 1;
+ this.startTimeUpdater();
this.evtHolder.dispatchEvent( "loadstart" );
}
pause: function() {
this.swfObj.api( "api_pause" );
},
- duration: function() {
- return this.objVars.duration;
- },
mute: function() {
if ( !this.muted() ) {
- this.objVars.oldVol = this.objVars.volume;
- this.volume( 0 );
+ this.oldVol = this.volume;
+
+ if ( this.paused ) {
+ this.setVolume( 0 );
+ } else {
+ this.volume = 0;
+ }
} else {
- this.volume( this.objVars.oldVol );
+ if ( this.paused ) {
+ this.setVolume( this.oldVol );
+ } else {
+ this.volume = this.oldVol;
+ }
}
},
muted: function() {
- return this.objVars.volume === 0;
+ return this.volume === 0;
},
// Force loading by playing the player. Pause afterwards
load: function() {
},
dispatchEvent: function( evtName ) {
return this.evtHolder.dispatchEvent( evtName );
- }
+ },
+ startTimeUpdater: function() {
+ var self = this,
+ seeked = 0;
+
+ if ( Math.abs( this.currentTime - this.previousCurrentTime ) > timeCheckInterval ) {
+ // Has programatically set the currentTime
+ this.setCurrentTime( this.currentTime );
+ seeked = 1;
+ } else {
+ this.previousCurrentTime = this.currentTime;
+ }
+
+ if ( this.volume !== this.previousVolume ) {
+ this.setVolume( this.volume );
+ }
+
+ if ( !self.paused || seeked ) {
+ this.dispatchEvent( 'timeupdate' );
+ }
+
+ if( !self.ended ) {
+ setTimeout( function() {
+ self.startTimeUpdater.call(self);
+ }, timeupdateInterval);
+ }
+ },
});
})( Popcorn );
\ No newline at end of file
if ( timesCalled === 1 ) {
ok( true, "Pause event works" );
plus();
- ok( popped.video.objVars.paused, "Pause attribute set" );
+ ok( popped.video.paused, "Pause attribute set" );
plus();
popped.play();
}
ok( true, "Video autoplay attribute works" );
plus();
- ok( !popped.video.objVars.paused, "Player paused attribute not set" );
+ ok( !popped.video.paused, "Player paused attribute not set" );
plus();
popped.exec( 5, function() {
ok( "vimeo" in Popcorn, "Vimeo is a method of Popcorn" );
plus();
- equals( popped.video.objVars.initialTime, 0, "Player initial time is 0" );
+ equals( popped.video.initialTime, 0, "Player initial time is 0" );
plus();
Popcorn.plugin( "timingTest", {
return new Popcorn.p.init( entity );
};
+ // Instance caching
+ Popcorn.instances = [];
+ Popcorn.instanceIds = {};
+
+ Popcorn.removeInstance = function( instance ) {
+ // If called prior to any instances being created
+ // Return early to avoid splicing on nothing
+ if ( !Popcorn.instances.length ) {
+ return;
+ }
+
+ // Remove instance from Popcorn.instances
+ Popcorn.instances.splice( Popcorn.instanceIds[ instance.id ], 1 );
+
+ // Delete the instance id key
+ delete Popcorn.instanceIds[ instance.id ];
+
+ // Return current modified instances
+ return Popcorn.instances;
+ };
+
+ // Addes a Popcorn instance to the Popcorn instance array
+ Popcorn.addInstance = function( instance ) {
+
+ var instanceLen = Popcorn.instances.length,
+ instanceId = instance.video.id && instance.video.id;
+
+ // If the video element has its own `id` use it, otherwise provide one
+ // Ensure that instances have unique ids and unique entries
+ // Uses `in` operator to avoid false positives on 0
+ instance.id = !( instanceId in Popcorn.instanceIds ) && instanceId ||
+ "__popcorn" + instanceLen;
+
+ // Create a reference entry for this instance
+ Popcorn.instanceIds[ instance.id ] = instanceLen;
+
+ // Add this instance to the cache
+ Popcorn.instances.push( instance );
+
+ // Return the current modified instances
+ return Popcorn.instances;
+ };
+
+ // Request Popcorn object instance by id
+ Popcorn.getInstanceById = function( id ) {
+ return Popcorn.instances[ Popcorn.instanceIds[ id ] ];
+ };
+
+ // Remove Popcorn object instance by id
+ Popcorn.removeInstanceById = function( id ) {
+ return Popcorn.removeInstance( Popcorn.instances[ Popcorn.instanceIds[ id ] ] );
+ };
+
// Declare a shortcut (Popcorn.p) to and a definition of
// the new prototype for our Popcorn constructor
Popcorn.p = Popcorn.prototype = {
init: function( entity ) {
- var elem, matches;
+ var matches;
// Supports Popcorn(function () { /../ })
// Originally proposed by Daniel Brooks
document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false);
}
-
-
return;
}
+ // check if entity is a valid string id
+ matches = rIdExp.exec( entity );
- if ( (typeof entity) == "string" ) {
- matches = rIdExp.exec( entity );
+ // get video element by id or reference
+ this.video = matches && matches.length && matches[ 2 ] ?
+ document.getElementById( matches[ 2 ] ) :
+ entity;
- if ( matches.length && matches[2] ) {
- elem = document.getElementById(matches[2]);
- }
-
- this.video = elem ? elem : null;
- } else if ( entity instanceof Object ) {
- this.video = entity;
- }
+ Popcorn.addInstance( this );
this.data = {
history: [],
// this is so we do not fall off either end
var duration = that.video.duration;
-
- if( typeof that.video.duration === "function") {
- duration = that.video.duration();
- }
-
// Check for no duration info (NaN)
var videoDurationPlus = duration != duration ? Number.MAX_VALUE : duration + 1;
tracksByEnd = tracks.byEnd,
tracksByStart = tracks.byStart;
-
- if (typeof this.currentTime === "function") {
- currentTime = this.currentTime();
- }
-
// Playbar advancing
if ( previousTime < currentTime ) {
}
};
- if ( this.video ) {
- isReady( this );
- }
+ isReady( this );
return this;
}
// todo: play, pause, mute should toggle
var methods = "load play pause currentTime playbackRate mute volume duration",
- noArgMethods = /load|play|pause|mute/,
ret = {};
ret[ name ] = function( arg ) {
- var isFunc = typeof this.video[name] === "function";
-
- if ( noArgMethods.test( name ) || ( arg !== false && arg !== null && typeof arg !== "undefined" ) ) {
-
- if ( isFunc ) {
- this.video[ name ]( arg );
- } else {
- this.video[ name ] = arg;
- }
+ if ( typeof this.video[name] === "function" ) {
+ this.video[ name ]();
return this;
}
- if ( isFunc ) {
- return this.video[ name ]();
+
+ if ( arg !== false && arg !== null && typeof arg !== "undefined" ) {
+
+ this.video[ name ] = arg;
+
+ return this;
}
return this.video[ name ];
_running: false,
_natives: {
start: fn || Popcorn.nop,
+ end: Popcorn.nop,
type: "exec"
}
});
// Push track event ids into the history
obj.data.history.push( track._id );
+
+ track._natives.start = track._natives.start || Popcorn.nop;
+ track._natives.end = track._natives.end || Popcorn.nop;
}
// Store this definition in an array sorted by times
}, false );
})(window, window.document);
-
});
+test("Instances", function() {
+ var expects = 11,
+ count = 0,
+ instance;
+
+ expect(expects);
+
+ function plus(){
+ if ( ++count == expects ) {
+ start();
+ }
+ }
+
+ stop();
+
+ ok( typeof Popcorn.addInstance === "function" , "Popcorn.addInstance is a provided utility function");
+ plus();
+
+ ok( typeof Popcorn.removeInstance === "function" , "Popcorn.removeInstance is a provided utility function");
+ plus();
+
+ ok( typeof Popcorn.getInstanceById === "function" , "Popcorn.getInstanceById is a provided utility function");
+ plus();
+
+ ok( typeof Popcorn.removeInstanceById === "function" , "Popcorn.removeInstanceById is a provided utility function");
+ plus();
+
+ ok( typeof Popcorn.instanceIds === "object" , "Popcorn.instanceIds is a provided cache object");
+ plus();
+
+ ok( "length" in Popcorn.instances && "join" in Popcorn.instances, "Popcorn.instances is a provided cache array");
+ plus();
+
+ instance = Popcorn.getInstanceById("video");
+
+ ok( instance.video, "Stored instance as a `video` property" );
+ plus();
+
+ ok( instance.data, "Stored instance as a `data` property" );
+ plus();
+
+ ok( instance instanceof Popcorn, "Instance instanceof Popcorn" );
+ plus();
+
+ equal( Popcorn.instances.length, 1, "There are the correct number of Popcorn instances" );
+ plus();
+
+ // Create another instance
+ Popcorn("#video");
+
+ // Get a reference to remove
+ var remove = Popcorn.instances[1];
+
+ // Remove and check the length of the currently cached instances
+ equal( Popcorn.removeInstanceById( remove.id ).length, 1, "Removing an instance by id: 1 instance remains" );
+ plus();
+});
test("guid", function () {
-test("Object", function () {
+test( "Object", function () {
+ var popped = Popcorn( "#video" ),
+ popObj = Popcorn( document.getElementById( "video" ) ),
+ methods = "load play pause currentTime mute volume roundTime exec removePlugin",
+ count = 0,
+ expects = 30;
- var popped = Popcorn("#video"),
- methods = "load play pause currentTime mute volume roundTime exec removePlugin";
-
-
+ expect( expects );
- ok( "video" in popped, "instance has `video` property" );
- ok( Object.prototype.toString.call(popped.video) === "[object HTMLVideoElement]", "video property is a HTMLVideoElement" );
+ function plus() {
- ok( "data" in popped, "instance has `data` property" );
- ok( Object.prototype.toString.call(popped.data) === "[object Object]", "data property is an object" );
+ if ( ++count === expects ) {
+
+ start();
+ }
+ }
- ok( "trackEvents" in popped.data, "instance has `trackEvents` property" );
- ok( Object.prototype.toString.call(popped.data.trackEvents) === "[object Object]", "trackEvents property is an object" )
+ stop( 10000 );
+
+ // testing element passed by id
+ ok( "video" in popped, "instance by id has `video` property" );
+ plus();
+ equal( Object.prototype.toString.call( popped.video ), "[object HTMLVideoElement]", "instance by id video property is a HTMLVideoElement" );
+ plus();
+
+ ok( "data" in popped, "instance by id has `data` property" );
+ plus();
+ equal( Object.prototype.toString.call( popped.data ), "[object Object]", "instance by id data property is an object" );
+ plus();
+
+ ok( "trackEvents" in popped.data, "instance by id has `trackEvents` property" );
+ plus();
+ equal( Object.prototype.toString.call( popped.data.trackEvents ), "[object Object]", "instance by id trackEvents property is an object" );
+ plus();
-
popped.play();
+ methods.split( /\s+/g ).forEach(function ( k,v ) {
- methods.split(/\s+/g).forEach(function (k,v) {
+ ok( k in popped, "instance by id has method: " + k );
+ plus();
+ });
- ok( k in popped, "instance has method: " + k );
-
+ // testing element passed by reference
+ ok( "video" in popObj, "instance by reference has `video` property" );
+ plus();
+ equal( Object.prototype.toString.call( popObj.video ), "[object HTMLVideoElement]", "instance by reference video property is a HTMLVideoElement" );
+ plus();
+
+ ok( "data" in popObj, "instance by reference has `data` property" );
+ plus();
+ equal( Object.prototype.toString.call( popObj.data ), "[object Object]", "instance by reference data property is an object" );
+ plus();
+
+ ok( "trackEvents" in popObj.data, "instance by reference has `trackEvents` property" );
+ plus();
+ equal( Object.prototype.toString.call( popObj.data.trackEvents ), "[object Object]", "instance by reference trackEvents property is an object" );
+ plus();
+
+ popObj.play();
+
+ methods.split( /\s+/g ).forEach(function ( k,v ) {
+
+ ok( k in popObj, "instance by reference has method: " + k );
+ plus();
});
-
-
});
module("Popcorn Methods");
});
-test("Passed an object", function () {
-
- QUnit.reset();
-
- var evtMethods = "addEventListener dispatchEvent".split(/\s+/),
- methods = "load play pause currentTime playbackRate mute volume duration".split(/\s+/),
- attributes = "readyState autoplay".split(/\s+/),
- obj = {},
- p,
- playEvent,
- count = 0,
- wants;
-
- function plus(){
-
- if ( ++count === wants ) {
- start();
- }
- }
-
- Popcorn.forEach( methods, function( method ) {
- obj[method] = (function() {
- var calledWithArgs = 0,
- calledWithoutArgs = 0;
-
- return function( arg ) {
- if ( arg && !calledWithArgs ) {
- calledWithArgs = 1;
- ok(true, method+" called with argument");
- plus();
- } else if ( !arg && !calledWithoutArgs ) {
- calledWithoutArgs = 1;
- ok(true, method+" called without argument");
- plus();
- }
- }
- })();
- });
-
- Popcorn.forEach( evtMethods, function( method ) {
- obj[method] = (function() {
- var hasCalled = 0;
-
- return function() {
- if ( hasCalled ) {
- return;
- }
-
- hasCalled = 1;
- ok(true, method+" called");
- plus();
- };
- })()
- });
-
- Popcorn.forEach( attributes, function( attr ) {
- obj[attr] = 3;
- });
-
- p = Popcorn( obj );
- wants = methods.length*2 + evtMethods.length;
-
- expect( wants );
- stop( 10000 );
-
- p.listen( "play", Popcorn.nop );
-
- Popcorn.forEach( methods, function( method ) {
- p[method]();
- p[method]( 1 );
- });
-
- p.trigger( "play" );
-});
-
-
test("Simulated", function () {
QUnit.reset();
test("Plugin Factory", function () {
QUnit.reset();
-
- // needs expectation
var popped = Popcorn("#video"),
methods = "load play pause currentTime mute volume roundTime exec removePlugin",
- expects = 48,
+ expects = 34,
count = 0;
-
- //expect(expects);
-
+
function plus() {
if ( ++count == expects ) {
- Popcorn.removePlugin("breaker");
Popcorn.removePlugin("executor");
Popcorn.removePlugin("complicator");
- Popcorn.removePlugin("closure");
- start();
+ start();
}
}
+ expect( expects );
stop( 10000 );
- expect(expects);
Popcorn.plugin("executor", function () {
ok( "executor" in popped, "executor plugin is now available to instance" );
plus();
- ok( Popcorn.registry.length === 1, "One item in the registry");
+ equals( Popcorn.registry.length, 1, "One item in the registry");
plus();
start: 1,
end: 2
});
-
-
+
Popcorn.plugin("complicator", {
start: function ( event ) {
timeupdate: function () {
}
});
-
-
+
ok( "complicator" in popped, "complicator plugin is now available to instance" );
plus();
- ok( Popcorn.registry.length === 2, "Two items in the registry");
+ equals( Popcorn.registry.length, 2, "Two items in the registry");
plus();
popped.complicator({
start: 3,
end: 4
- });
+ });
+
+ popped.currentTime(0).play();
+
+});
+
+test("Plugin Breaker", function () {
+
+ QUnit.reset();
+
+ var popped = Popcorn("#video"),
+ expects = 6,
+ count = 0;
+
+ function plus() {
+ if ( ++count == expects ) {
+ Popcorn.removePlugin("breaker");
+ start();
+ }
+ }
+ expect( expects );
+ stop( 10000 );
var breaker = {
ok( "breaker" in popped, "breaker plugin is now available to instance" );
plus();
- ok( Popcorn.registry.length === 3, "Three items in the registry");
+ equals( Popcorn.registry.length, 1, "Three items in the registry");
plus();
popped.breaker({
end: 2
});
+ popped.currentTime(0).play();
+
+});
+
+test("Plugin Empty", function () {
+
+ QUnit.reset();
+
+ var popped = Popcorn("#video"),
+ expects = 2,
+ testObj = {},
+ count = 0;
+
+ function plus() {
+ if ( ++count == expects ) {
+ Popcorn.removePlugin("empty");
+ start();
+ }
+ }
+
+ expect( expects );
+ stop( 10000 );
+
+ Popcorn.plugin("empty", testObj);
+
+ popped.empty({});
+
+ ok( testObj.start, "default start function is generated" );
+ plus();
+ ok( testObj.end, "default end function is generated" );
+ plus();
+
+ popped.currentTime(0).play();
+
+});
+
+test("Plugin Closure", function () {
+
+ QUnit.reset();
+
+ var popped = Popcorn("#video"),
+ methods = "load play pause currentTime mute volume roundTime exec removePlugin",
+ expects = 8,
+ count = 0;
+
+ function plus() {
+ if ( ++count == expects ) {
+ Popcorn.removePlugin("closure");
+ start();
+ }
+ }
+
+ expect( expects );
+ stop( 10000 );
+
Popcorn.plugin("closure", function() {
var startCount = 0;
nick: "second closure track"
});
- popped.currentTime(0).play();
+ popped.currentTime(5).play();
});
});
-
-
-
module("Popcorn Test Runner End");
test("Last Check", function () {