Popcorn.addInstance = function( instance ) {
var instanceLen = Popcorn.instances.length,
- instanceId = instance.video.id && instance.video.id;
+ instanceId = instance.media.id && instance.media.id;
- // If the video element has its own `id` use it, otherwise provide one
+ // If the media 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 ||
// Check if entity is a valid string id
matches = rIdExp.exec( entity );
- // Get video element by id or object reference
- this.video = matches && matches.length && matches[ 2 ] ?
+ // Get media element by id or object reference
+ this.media = matches && matches.length && matches[ 2 ] ?
document.getElementById( matches[ 2 ] ) :
entity;
+ // Create an audio or video element property reference
+ this[ this.media.tagName.toLowerCase() ] = this.media;
+
+
// Register new instance
Popcorn.addInstance( this );
// Wrap true ready check
var isReady = function( that ) {
- if ( that.video.readyState >= 2 ) {
+ if ( that.media.readyState >= 2 ) {
// Adding padding to the front and end of the arrays
// this is so we do not fall off either end
- var duration = that.video.duration;
+ var duration = that.media.duration;
// Check for no duration info (NaN)
var videoDurationPlus = duration != duration ? Number.MAX_VALUE : duration + 1;
end: videoDurationPlus
});
- that.video.addEventListener( "timeupdate", function( event ) {
+ that.media.addEventListener( "timeupdate", function( event ) {
var currentTime = this.currentTime,
previousTime = that.data.trackEvents.previousUpdateTime,
ret[ name ] = function( arg ) {
- if ( typeof this.video[name] === "function" ) {
- this.video[ name ]();
+ if ( typeof this.media[name] === "function" ) {
+ this.media[ name ]();
return this;
}
if ( arg !== false && arg !== null && typeof arg !== "undefined" ) {
- this.video[ name ] = arg;
+ this.media[ name ] = arg;
return this;
}
- return this.video[ name ];
+ return this.media[ name ];
};
});
// Rounded currentTime
roundTime: function () {
- return -~this.video.currentTime;
+ return -~this.media.currentTime;
},
// Attach an event to a single point in time
var evt = document.createEvent( eventInterface );
evt.initEvent(type, true, true, global, 1);
- this.video.dispatchEvent(evt);
+ this.media.dispatchEvent(evt);
return this;
}
// only attach one event of any type
if ( !hasEvents && Popcorn.events.all.indexOf( type ) > -1 ) {
- this.video.addEventListener( type, function( event ) {
+ this.media.addEventListener( type, function( event ) {
Popcorn.forEach( self.data.events[type], function ( obj, key ) {
if ( typeof obj === "function" ) {
document.addEventListener( "DOMContentLoaded", function () {
- var videos = document.getElementsByTagName( "video" );
+ var medias = document.querySelectorAll( "video,audio" );
- Popcorn.forEach( videos, function ( iter, key ) {
+
+ Popcorn.forEach( medias, function( idx, key ) {
- var video = videos[ key ],
+ var media = medias[ key ],
hasDataSources = false,
- dataSources, data, popcornVideo;
+ dataSources, data, popcornMedia;
// Ensure that the DOM has an id
- if ( !video.id ) {
+ if ( !media.id ) {
- video.id = Popcorn.guid( "__popcorn" );
+ media.id = Popcorn.guid( "__popcorn" );
}
// Ensure we're looking at a dom node
- if ( video.nodeType && video.nodeType === 1 ) {
+ if ( media.nodeType && media.nodeType === 1 ) {
- popcornVideo = Popcorn( "#" + video.id );
+ popcornMedia = Popcorn( "#" + media.id );
- dataSources = ( video.getAttribute( "data-timeline-sources" ) || "" ).split(",");
+ dataSources = ( media.getAttribute( "data-timeline-sources" ) || "" ).split(",");
+
+
if ( dataSources[ 0 ] ) {
- Popcorn.forEach( dataSources, function ( source ) {
+ Popcorn.forEach( dataSources, function( source ) {
// split the parser and data as parser:file
data = source.split( ":" );
}
- // If the video has data sources and the correct parser is registered, continue to load
- if ( dataSources[ 0 ] && popcornVideo[ data[ 0 ] ] ) {
+ // If the media has data sources and the correct parser is registered, continue to load
+ if ( dataSources[ 0 ] && popcornMedia[ data[ 0 ] ] ) {
- // Set up the video and load in the datasources
- popcornVideo[ data[ 0 ] ]( data[ 1 ] );
+ // Set up the media and load in the datasources
+ popcornMedia[ data[ 0 ] ]( data[ 1 ] );
}
});
}
- // Only play the video if it was specified to do so
- if ( !!popcornVideo.autoplay ) {
- popcornVideo.play();
+ // Only play the media if it was specified to do so
+ if ( !!popcornMedia.autoplay ) {
+ popcornMedia.play();
}
}
ok( instance instanceof Popcorn, "Instance instanceof Popcorn" );
plus();
- equal( Popcorn.instances.length, 1, "There are the correct number of Popcorn instances" );
+ equal( Popcorn.instances.length, 2, "There are the correct number of Popcorn instances" );
plus();
// Create another instance
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" );
+ equal( Popcorn.removeInstanceById( remove.id ).length, 2, "Removing an instance by id: 1 instance remains" );
plus();
});
});
+module("Audio");
+test( "Basic Audio Support", function () {
+
+ var popped = Popcorn( "#audio" ),
+ popObj = Popcorn( document.getElementById( "audio" ) ),
+ methods = "load play pause currentTime mute volume roundTime exec removePlugin",
+ count = 0,
+ expects = 30;
+
+ expect( expects );
+
+ function plus() {
+
+ if ( ++count === expects ) {
+
+ start();
+ }
+ }
+
+ stop( 10000 );
+
+ // testing element passed by id
+ ok( "audio" in popped, "instance by id has `media` property" );
+ plus();
+ equal( Object.prototype.toString.call( popped.media ), "[object HTMLAudioElement]", "instance by id media property is a HTMLAudioElement" );
+ 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 ) {
+
+ ok( k in popped, "instance by id has method: " + k );
+ plus();
+ });
+
+ // testing element passed by reference
+ ok( "media" in popObj, "instance by reference has `media` property" );
+ plus();
+ equal( Object.prototype.toString.call( popObj.media ), "[object HTMLAudioElement]", "instance by reference media property is a HTMLAudioElement" );
+ 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();
+ });
+});
+
+test("Parser Support", function () {
+
+ var expects = 3,
+ count = 0,
+ timeOut = 0,
+ interval,
+ audiocorn = Popcorn( "#audio" );
+
+ function plus() {
+ if ( ++count === expects ) {
+ start();
+
+ Popcorn.removePlugin( "testAudioParser" );
+ }
+ }
+
+ expect(expects);
+ stop(5000);
+
+
+ Popcorn.plugin("testAudioParser", {
+
+ start: function() {
+ ok(true, "testAudioParser started: " + Math.round(this.currentTime()) );
+ plus();
+ },
+ end: function() {
+ ok(true, "testAudioParser ended: " + Math.round(this.currentTime()) );
+ plus();
+ }
+ });
+
+ Popcorn.parser( "parseAudio", function( data ){
+ ok( typeof data.json === "object", "data.json exists");
+ plus();
+ return data.json;
+ });
+
+ audiocorn.parseAudio("data/parserAudio.json", function() {
+
+ audiocorn.currentTime(4).play();
+
+ });
+});
+
+
module("Popcorn Test Runner End");
test("Last Check", function () {