-<!DOCTYPE html>\r
-<html>\r
- <head>\r
- <title>Popcorn Youtube Player Example</title>\r
- <script src="../../popcorn.js"></script>\r
- <script type="text/javascript" src="popcorn.youtube.js"></script>\r
- <script>\r
- // <![CDATA[\r
-\r
- /* Some code on this page is taken from stevenaw's vimeo plug-in. */\r
-\r
- // Helper function to get elements\r
- function element(id) {\r
- return document.getElementById(id);\r
- }\r
-\r
- // Register YouTube\r
- function onYouTubePlayerReady() {\r
- element( 'player-status' ).innerHTML = 'Ready';\r
-\r
- var youcorn = Popcorn.youtube( 'video' );\r
- var popcorn = Popcorn(youcorn);\r
- element( 'player-duration' ).innerHTML = popcorn.duration();\r
-\r
- // Update time\r
- popcorn.listen( 'timeupdate', function() {\r
- element( 'player-time' ).innerHTML = popcorn.currentTime();\r
- });\r
-\r
- // Update button labels\r
- var paused = true;\r
- popcorn.listen( 'play' , function() {\r
- paused = false;\r
- element( 'btn-play-pause' ).innerHTML = 'Pause';\r
- });\r
- popcorn.listen('pause', function() {\r
- paused = true;\r
- element( 'btn-play-pause' ).innerHTML = 'Play';\r
- });\r
-\r
- // Single play/pause button\r
- element( 'btn-play-pause' ).addEventListener( 'click', function() {\r
- if (paused) {\r
- popcorn.play();\r
- } else {\r
- popcorn.pause();\r
- }\r
- }, false);\r
-\r
- // Seek\r
- element('btn-seek').addEventListener('click', function() {\r
- popcorn.currentTime(30);\r
- }, false);\r
-\r
- // Volume\r
- element('btn-volume').addEventListener('click', function() {\r
- var volume = (popcorn.volume() === 1) ? 0.5 : 1;\r
- popcorn.volume(volume);\r
- }, false);\r
- }\r
- // ]]>\r
- </script>\r
- </head>\r
- <body>\r
- <div>\r
- <object id="video" type="application/x-shockwave-flash"\r
- data="http://www.youtube.com/e/ac7KhViaVqc?enablejsapi=1&version=3"\r
- width="250" height="200">\r
- <param name="allowFullScreen" value="true" />\r
- <param name="allowscriptaccess" value="always" />\r
- </object>\r
- </div>\r
- <div>\r
- <button class="simple" id="btn-play-pause">Play</button>\r
- <button class="seek" id="btn-seek">Seek to 30</button>\r
- <button class="volume" id="btn-volume">Toggle Volume</button>\r
- <div>\r
- <ul>\r
- <li>Status: <span id="player-status">Not Ready</span></li>\r
- <li>Current Time (s): <span id="player-time"></span></li>\r
- <li>Video Duration (s): <span id="player-duration"></span></li>\r
- </ul>\r
- </div>\r
- <div id="footnotediv" width="50%" height="50%"></div>\r
- </div>\r
- </body>\r
-</html>\r
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Popcorn Youtube Player Example</title>
+ <script src="../../popcorn.js"></script>
+ <script type="text/javascript" src="popcorn.youtube.js"></script>
+ <script>
+ // <![CDATA[
+
+ /* Some code on this page is taken from stevenaw's vimeo plug-in. */
+
+ // Helper function to get elements
+ function element(id) {
+ return document.getElementById(id);
+ }
+
+ // Register YouTube
+ function onYouTubePlayerReady() {
+ element( 'player-status' ).innerHTML = 'Ready';
+
+ var youcorn = Popcorn.youtube( 'video' );
+ var popcorn = Popcorn(youcorn);
+ element( 'player-duration' ).innerHTML = popcorn.duration();
+
+ // Update time
+ popcorn.listen( 'timeupdate', function() {
+ element( 'player-time' ).innerHTML = popcorn.currentTime();
+ });
+
+ // Update button labels
+ var paused = true;
+ popcorn.listen( 'play' , function() {
+ paused = false;
+ element( 'btn-play-pause' ).innerHTML = 'Pause';
+ });
+ popcorn.listen('pause', function() {
+ paused = true;
+ element( 'btn-play-pause' ).innerHTML = 'Play';
+ });
+
+ // Single play/pause button
+ element( 'btn-play-pause' ).addEventListener( 'click', function() {
+ if (paused) {
+ popcorn.play();
+ } else {
+ popcorn.pause();
+ }
+ }, false);
+
+ // Seek
+ element('btn-seek').addEventListener('click', function() {
+ popcorn.currentTime(30);
+ }, false);
+
+ // Volume
+ element('btn-volume').addEventListener('click', function() {
+ var volume = (popcorn.volume() === 1) ? 0.5 : 1;
+ popcorn.volume(volume);
+ }, false);
+ }
+ // ]]>
+ </script>
+ </head>
+ <body>
+ <div>
+ <object id="video" type="application/x-shockwave-flash"
+ data="http://www.youtube.com/e/ac7KhViaVqc?enablejsapi=1&version=3"
+ width="250" height="200">
+ <param name="allowFullScreen" value="true" />
+ <param name="allowscriptaccess" value="always" />
+ </object>
+ </div>
+ <div>
+ <button class="simple" id="btn-play-pause">Play</button>
+ <button class="seek" id="btn-seek">Seek to 30</button>
+ <button class="volume" id="btn-volume">Toggle Volume</button>
+ <div>
+ <ul>
+ <li>Status: <span id="player-status">Not Ready</span></li>
+ <li>Current Time (s): <span id="player-time"></span></li>
+ <li>Video Duration (s): <span id="player-duration"></span></li>
+ </ul>
+ </div>
+ <div id="footnotediv" width="50%" height="50%"></div>
+ </div>
+ </body>
+</html>
+
-// Popcorn Youtube Player Warpper\r
-\r
-( function( Popcorn ) {\r
- /**\r
- * Youtube wrapper for popcorn.\r
- * This plug-in adds capability for Popcorn.js to deal with Youtube\r
- * videos. This plug-in also doesn't use Popcorn's plugin() API and\r
- * instead hacks directly into Popcorn's core.\r
- *\r
- * To use this plug-in, onYouTubePlayerReady() event handler needs to be\r
- * called by the Youtube video player, before videos can be registered.\r
- * Once videos are registered, calls to them can be made the same way as\r
- * regular Popcorn objects. Also note that enablejsapi=1 needs to be added\r
- * to the embed code, in order for Youtube's JavaScript API to work.\r
- *\r
- * Note that there are a few methods, properties and events that are not\r
- * supported. See the bottom of this plug-in for a complete list.\r
- */\r
-\r
- // Intended\r
- var undef;\r
-\r
- // Config parameters\r
- var debug = true;\r
- var timeupdateInterval = 33; // 33 is suitable for 30 fps\r
- var progressInterval = 250; // as specified by WHATWG\r
-\r
- // Ready State Constants\r
- var READY_STATE_HAVE_NOTHING = 0;\r
- var READY_STATE_HAVE_METADATA = 1;\r
- var READY_STATE_HAVE_CURRENT_DATA = 2;\r
- var READY_STATE_HAVE_FUTURE_DATA = 3;\r
- var READY_STATE_HAVE_ENOUGH_DATA = 4;\r
-\r
- // Youtube State Constants\r
- var YOUTUBE_STATE_UNSTARTED = -1;\r
- var YOUTUBE_STATE_ENDED = 0;\r
- var YOUTUBE_STATE_PLAYING = 1;\r
- var YOUTUBE_STATE_PAUSED = 2;\r
- var YOUTUBE_STATE_BUFFERING = 3;\r
- var YOUTUBE_STATE_CUED = 5;\r
-\r
- Popcorn.youtube = function( elementId ) {\r
- return new Popcorn.youtube.init( elementId );\r
- };\r
-\r
- Popcorn.youtube.init = function( elementId ) {\r
- if ( !elementId ) {\r
- throw "Element id is invalid.";\r
- }\r
-\r
- this.video = document.getElementById( elementId );\r
- this.readyState = READY_STATE_HAVE_NOTHING;\r
- this.duration = this.video.getDuration();\r
- this.eventListeners = {};\r
- this.loadStarted = false;\r
- this.loadedData = false;\r
- this.fullyLoaded = false;\r
- this.timeUpdater = null;\r
- this.progressUpdater = null;\r
- this.registerYoutubeEventHandlers();\r
- this.registerInternalEventHandlers();\r
- }; // end Popcorn.youtube.init\r
-\r
- Popcorn.extend( Popcorn.youtube.init.prototype, {\r
-\r
- // For internal use only.\r
- // Register handlers to YouTube events.\r
- registerYoutubeEventHandlers: function() {\r
- var youcorn = this,\r
- stateChangeHandler = 'Popcorn.youtube.stateChangeEventHandler',\r
- errorHandler = 'Popcorn.youtube.errorEventHandler';\r
- this.video.addEventListener( 'onStateChange', stateChangeHandler );\r
- this.video.addEventListener( 'onError', errorHandler );\r
-\r
- /**\r
- * Since Flash can only call named functions, they are declared\r
- * separately here.\r
- */\r
- Popcorn.youtube.stateChangeEventHandler = function( state ) {\r
- if ( debug ) {\r
- console.log( 'Youtube state updated: ' + state );\r
- }\r
- if ( state == YOUTUBE_STATE_UNSTARTED ) {\r
- youcorn.readyState = READY_STATE_HAVE_METADATA;\r
- youcorn.raiseEvent('loadedmetadata');\r
- } else if ( state == YOUTUBE_STATE_ENDED ) {\r
- youcorn.raiseEvent('ended');\r
- } else if ( state == YOUTUBE_STATE_PLAYING ) {\r
- // Being able to play means current data is loaded.\r
- if ( !this.loadedData ) {\r
- this.loadedData = true;\r
- youcorn.raiseEvent( 'loadeddata' );\r
- }\r
-\r
- youcorn.readyState = READY_STATE_HAVE_CURRENT_DATA;\r
- youcorn.raiseEvent( 'playing' );\r
- } else if ( state == YOUTUBE_STATE_PAUSED ) {\r
- youcorn.raiseEvent( 'pause' );\r
- } else if ( state == YOUTUBE_STATE_BUFFERING ) {\r
- youcorn.raiseEvent( 'waiting' );\r
- } else if ( state == YOUTUBE_STATE_CUED ) {\r
- // not handled\r
- }\r
- };\r
-\r
- Popcorn.youtube.errorEventHandler = function( state ) {\r
- if ( debug ) {\r
- console.log( 'Youtube error encountered: ' + error );\r
- }\r
- youcorn.raiseEvent( 'error' );\r
- };\r
- },\r
-\r
- // For internal use only.\r
- // Start current time and loading progress syncing intervals.\r
- registerInternalEventHandlers: function() {\r
- this.addEventListener( 'playing', function() {\r
- startTimeUpdater( this );\r
- });\r
- this.addEventListener( 'loadedmetadata', function() {\r
- startProgressUpdater( this );\r
- });\r
- },\r
-\r
- play: function() {\r
- this.raiseEvent( 'play' );\r
- this.video.playVideo();\r
- },\r
-\r
- pause: function() {\r
- this.video.pauseVideo();\r
- // pause event is raised by Youtube.\r
- },\r
-\r
- load: function() {\r
- this.video.playVideo();\r
- this.video.pauseVideo();\r
- },\r
-\r
- currentTime: function( time ) {\r
- if ( time === undef ) {\r
- return this.video.getCurrentTime();\r
- }\r
- var playing = this.video.getPlayerState() == YOUTUBE_STATE_PLAYING;\r
- this.video.seekTo( time, true );\r
-\r
- // Prevent Youtube's behaviour to start playing video after seeking.\r
- if ( !playing ) {\r
- this.video.pauseVideo();\r
- }\r
-\r
- // Data need to be loaded again.\r
- if ( !this.fullyLoaded ) {\r
- this.loadedData = false;\r
- }\r
-\r
- // Raise event.\r
- this.raiseEvent( 'seeked' );\r
- },\r
-\r
- duration: function() {\r
- return this.video.getDuration();\r
- },\r
-\r
- mute: function() {\r
- if ( this.video.getVolume() != 0 ) {\r
- this.video.mute();\r
- this.raiseEvent( 'volumechange' );\r
- }\r
- },\r
-\r
- volume: function( vol ) {\r
- if ( vol == undef ) {\r
- return this.video.getVolume() / 100;\r
- }\r
- if ( vol != this.volume() ) {\r
- this.video.setVolume( vol * 100 );\r
- this.raiseEvent( 'volumechange' );\r
- }\r
- },\r
-\r
- addEventListener: function( eventName, func ) {\r
- if ( !this.eventListeners[eventName] ) {\r
- this.eventListeners[eventName] = [];\r
- }\r
- this.eventListeners[eventName].push( func );\r
- },\r
-\r
- /**\r
- * Notify event listeners about an event.\r
- */\r
- raiseEvent: function( name ) {\r
- if ( debug ) {\r
- console.log( 'Raising event: ' + name );\r
- }\r
- if ( !this.eventListeners[name] ) {\r
- return;\r
- }\r
- for ( var i in this.eventListeners[name] ) {\r
- // @TODO should we mimic the event object sent by HTMLMediaElement?\r
- this.eventListeners[name][i].call( this, null );\r
- }\r
- },\r
-\r
- /* Unsupported methods. */\r
-\r
- defaultPlaybackRate: function( arg ) {\r
- },\r
-\r
- playbackRate: function( arg ) {\r
- }\r
-\r
- }); // end Popcorn.extend\r
-\r
- function startTimeUpdater( youcorn ) {\r
- youcorn.timeUpdater = setInterval(function() {\r
- var state = youcorn.video.getPlayerState();\r
- if ( (state == YOUTUBE_STATE_ENDED) || (state == YOUTUBE_STATE_PAUSED) ) {\r
- clearInterval(youcorn.timeUpdater);\r
- return;\r
- }\r
- youcorn.raiseEvent( 'timeupdate' );\r
- }, timeupdateInterval);\r
- }\r
-\r
- function startProgressUpdater( youcorn ) {\r
- youcorn.progressUpdater = setInterval(function() {\r
- var bytesLoaded = youcorn.video.getVideoBytesLoaded();\r
- var bytesToLoad = youcorn.video.getVideoBytesTotal();\r
- if ( debug ) {\r
- console.log( 'Bytes loaded: ' + bytesLoaded );\r
- console.log( 'Bytes to load: ' + bytesToLoad );\r
- }\r
-\r
- // do nothing if size is not yet determined\r
- if ( bytesToLoad == 0 ) {\r
- return;\r
- }\r
-\r
- // raise an event if load has just started\r
- if ( !youcorn.loadStarted ) {\r
- youcorn.loadStarted = true;\r
- youcorn.raiseEvent( 'loadstart' );\r
- }\r
-\r
- // fully loaded\r
- if ( bytesLoaded >= bytesToLoad ) {\r
- youcorn.fullyLoaded = true;\r
- youcorn.readyState = READY_STATE_HAVE_ENOUGH_DATA;\r
- youcorn.raiseEvent( 'canplaythrough' );\r
- clearInterval( youcorn.progressUpdater );\r
- return;\r
- }\r
-\r
- youcorn.raiseEvent( 'progress' );\r
- }, progressInterval);\r
- }\r
-\r
- /* Unsupported properties and events. */\r
-\r
- /**\r
- * Unsupported events are:\r
- * * suspend\r
- * * abort\r
- * * emptied\r
- * * stalled\r
- * * canplay\r
- * * seeking\r
- * * ratechange\r
- * * durationchange\r
- */\r
-\r
-})( Popcorn );\r
-\r
+// Popcorn Youtube Player Warpper
+
+( function( Popcorn ) {
+ /**
+ * Youtube wrapper for popcorn.
+ * This plug-in adds capability for Popcorn.js to deal with Youtube
+ * videos. This plug-in also doesn't use Popcorn's plugin() API and
+ * instead hacks directly into Popcorn's core.
+ *
+ * To use this plug-in, onYouTubePlayerReady() event handler needs to be
+ * called by the Youtube video player, before videos can be registered.
+ * Once videos are registered, calls to them can be made the same way as
+ * regular Popcorn objects. Also note that enablejsapi=1 needs to be added
+ * to the embed code, in order for Youtube's JavaScript API to work.
+ *
+ * Note that there are a few methods, properties and events that are not
+ * supported. See the bottom of this plug-in for a complete list.
+ */
+
+ // Intended
+ var undef;
+
+ // Config parameters
+ var debug = true;
+ var timeupdateInterval = 33; // 33 is suitable for 30 fps
+ var progressInterval = 250; // as specified by WHATWG
+
+ // Ready State Constants
+ var READY_STATE_HAVE_NOTHING = 0;
+ var READY_STATE_HAVE_METADATA = 1;
+ var READY_STATE_HAVE_CURRENT_DATA = 2;
+ var READY_STATE_HAVE_FUTURE_DATA = 3;
+ var READY_STATE_HAVE_ENOUGH_DATA = 4;
+
+ // Youtube State Constants
+ var YOUTUBE_STATE_UNSTARTED = -1;
+ var YOUTUBE_STATE_ENDED = 0;
+ var YOUTUBE_STATE_PLAYING = 1;
+ var YOUTUBE_STATE_PAUSED = 2;
+ var YOUTUBE_STATE_BUFFERING = 3;
+ var YOUTUBE_STATE_CUED = 5;
+
+ Popcorn.youtube = function( elementId ) {
+ return new Popcorn.youtube.init( elementId );
+ };
+
+ Popcorn.youtube.init = function( elementId ) {
+ if ( !elementId ) {
+ throw "Element id is invalid.";
+ }
+
+ this.video = document.getElementById( elementId );
+ this.readyState = READY_STATE_HAVE_NOTHING;
+ this.duration = this.video.getDuration();
+ this.eventListeners = {};
+ this.loadStarted = false;
+ this.loadedData = false;
+ this.fullyLoaded = false;
+ this.timeUpdater = null;
+ this.progressUpdater = null;
+ this.registerYoutubeEventHandlers();
+ this.registerInternalEventHandlers();
+ }; // end Popcorn.youtube.init
+
+ Popcorn.extend( Popcorn.youtube.init.prototype, {
+
+ // For internal use only.
+ // Register handlers to YouTube events.
+ registerYoutubeEventHandlers: function() {
+ var youcorn = this,
+ stateChangeHandler = 'Popcorn.youtube.stateChangeEventHandler',
+ errorHandler = 'Popcorn.youtube.errorEventHandler';
+ this.video.addEventListener( 'onStateChange', stateChangeHandler );
+ this.video.addEventListener( 'onError', errorHandler );
+
+ /**
+ * Since Flash can only call named functions, they are declared
+ * separately here.
+ */
+ Popcorn.youtube.stateChangeEventHandler = function( state ) {
+ if ( debug ) {
+ console.log( 'Youtube state updated: ' + state );
+ }
+ if ( state == YOUTUBE_STATE_UNSTARTED ) {
+ youcorn.readyState = READY_STATE_HAVE_METADATA;
+ youcorn.raiseEvent('loadedmetadata');
+ } else if ( state == YOUTUBE_STATE_ENDED ) {
+ youcorn.raiseEvent('ended');
+ } else if ( state == YOUTUBE_STATE_PLAYING ) {
+ // Being able to play means current data is loaded.
+ if ( !this.loadedData ) {
+ this.loadedData = true;
+ youcorn.raiseEvent( 'loadeddata' );
+ }
+
+ youcorn.readyState = READY_STATE_HAVE_CURRENT_DATA;
+ youcorn.raiseEvent( 'playing' );
+ } else if ( state == YOUTUBE_STATE_PAUSED ) {
+ youcorn.raiseEvent( 'pause' );
+ } else if ( state == YOUTUBE_STATE_BUFFERING ) {
+ youcorn.raiseEvent( 'waiting' );
+ } else if ( state == YOUTUBE_STATE_CUED ) {
+ // not handled
+ }
+ };
+
+ Popcorn.youtube.errorEventHandler = function( state ) {
+ if ( debug ) {
+ console.log( 'Youtube error encountered: ' + error );
+ }
+ youcorn.raiseEvent( 'error' );
+ };
+ },
+
+ // For internal use only.
+ // Start current time and loading progress syncing intervals.
+ registerInternalEventHandlers: function() {
+ this.addEventListener( 'playing', function() {
+ startTimeUpdater( this );
+ });
+ this.addEventListener( 'loadedmetadata', function() {
+ startProgressUpdater( this );
+ });
+ },
+
+ play: function() {
+ this.raiseEvent( 'play' );
+ this.video.playVideo();
+ },
+
+ pause: function() {
+ this.video.pauseVideo();
+ // pause event is raised by Youtube.
+ },
+
+ load: function() {
+ this.video.playVideo();
+ this.video.pauseVideo();
+ },
+
+ currentTime: function( time ) {
+ if ( time === undef ) {
+ return this.video.getCurrentTime();
+ }
+ var playing = this.video.getPlayerState() == YOUTUBE_STATE_PLAYING;
+ this.video.seekTo( time, true );
+
+ // Prevent Youtube's behaviour to start playing video after seeking.
+ if ( !playing ) {
+ this.video.pauseVideo();
+ }
+
+ // Data need to be loaded again.
+ if ( !this.fullyLoaded ) {
+ this.loadedData = false;
+ }
+
+ // Raise event.
+ this.raiseEvent( 'seeked' );
+ },
+
+ duration: function() {
+ return this.video.getDuration();
+ },
+
+ mute: function() {
+ if ( this.video.getVolume() != 0 ) {
+ this.video.mute();
+ this.raiseEvent( 'volumechange' );
+ }
+ },
+
+ volume: function( vol ) {
+ if ( vol == undef ) {
+ return this.video.getVolume() / 100;
+ }
+ if ( vol != this.volume() ) {
+ this.video.setVolume( vol * 100 );
+ this.raiseEvent( 'volumechange' );
+ }
+ },
+
+ addEventListener: function( eventName, func ) {
+ if ( !this.eventListeners[eventName] ) {
+ this.eventListeners[eventName] = [];
+ }
+ this.eventListeners[eventName].push( func );
+ },
+
+ /**
+ * Notify event listeners about an event.
+ */
+ raiseEvent: function( name ) {
+ if ( debug ) {
+ console.log( 'Raising event: ' + name );
+ }
+ if ( !this.eventListeners[name] ) {
+ return;
+ }
+ for ( var i in this.eventListeners[name] ) {
+ // @TODO should we mimic the event object sent by HTMLMediaElement?
+ this.eventListeners[name][i].call( this, null );
+ }
+ },
+
+ /* Unsupported methods. */
+
+ defaultPlaybackRate: function( arg ) {
+ },
+
+ playbackRate: function( arg ) {
+ }
+
+ }); // end Popcorn.extend
+
+ function startTimeUpdater( youcorn ) {
+ youcorn.timeUpdater = setInterval(function() {
+ var state = youcorn.video.getPlayerState();
+ if ( (state == YOUTUBE_STATE_ENDED) || (state == YOUTUBE_STATE_PAUSED) ) {
+ clearInterval(youcorn.timeUpdater);
+ return;
+ }
+ youcorn.raiseEvent( 'timeupdate' );
+ }, timeupdateInterval);
+ }
+
+ function startProgressUpdater( youcorn ) {
+ youcorn.progressUpdater = setInterval(function() {
+ var bytesLoaded = youcorn.video.getVideoBytesLoaded();
+ var bytesToLoad = youcorn.video.getVideoBytesTotal();
+ if ( debug ) {
+ console.log( 'Bytes loaded: ' + bytesLoaded );
+ console.log( 'Bytes to load: ' + bytesToLoad );
+ }
+
+ // do nothing if size is not yet determined
+ if ( bytesToLoad == 0 ) {
+ return;
+ }
+
+ // raise an event if load has just started
+ if ( !youcorn.loadStarted ) {
+ youcorn.loadStarted = true;
+ youcorn.raiseEvent( 'loadstart' );
+ }
+
+ // fully loaded
+ if ( bytesLoaded >= bytesToLoad ) {
+ youcorn.fullyLoaded = true;
+ youcorn.readyState = READY_STATE_HAVE_ENOUGH_DATA;
+ youcorn.raiseEvent( 'canplaythrough' );
+ clearInterval( youcorn.progressUpdater );
+ return;
+ }
+
+ youcorn.raiseEvent( 'progress' );
+ }, progressInterval);
+ }
+
+ /* Unsupported properties and events. */
+
+ /**
+ * Unsupported events are:
+ * * suspend
+ * * abort
+ * * emptied
+ * * stalled
+ * * canplay
+ * * seeking
+ * * ratechange
+ * * durationchange
+ */
+
+})( Popcorn );
+
-var ytReady = false;\r
-function onYouTubePlayerReady() {\r
- ytReady = true;\r
-}\r
-\r
-test( "Popcorn YouTube Plugin Startup", function() {\r
- var time = 0,\r
- wait = 100,\r
- timeout = 10000;\r
- \r
- // wait for YouTube to start\r
- stop( timeout + wait );\r
-\r
- // run in an interval check if YouTube has started\r
- var interval = setInterval(function() {\r
- time += wait;\r
-\r
- if ( ytReady ) {\r
- start();\r
- ok( true, "YouTube has started." );\r
- clearInterval( interval );\r
- return;\r
- }\r
-\r
- if ( time > timeout ) {\r
- ok( false, "YouTube cannot be started." );\r
- clearInterval( interval );\r
- }\r
- }, wait);\r
-});\r
-\r
-test( "Popcorn YouTube Plugin Event Tests", function() {\r
- if ( !ytReady ) {\r
- ok( false, "YouTube did not start." );\r
- return;\r
- }\r
-\r
- var popcorn = Popcorn( new Popcorn.youtube( 'video' ) );\r
- popcorn.volume(1); // is muted later\r
- \r
- // check time sync\r
- popcorn.exec(2, function() {\r
- ok( popcorn.currentTime() >= 2, "Check time synchronization." );\r
- });\r
- popcorn.exec(49, function() {\r
- ok( popcorn.currentTime() >= 49, "Check time synchronization." );\r
- });\r
- popcorn.exec(40, function() {\r
- ok( false, "This should not be run." );\r
- });\r
-\r
- // events must be fired in this order\r
- var expectedEvents = [\r
- 'play',\r
- 'loadeddata',\r
- 'playing',\r
- 'volumechange',\r
- 'pause',\r
- 'play',\r
- 'playing',\r
- 'seeked',\r
- 'playing',\r
- 'pause',\r
- 'ended'\r
- ];\r
-\r
- var expectedEventCount = expectedEvents.length;\r
- expect(expectedEventCount + 2);\r
-\r
- // register each events\r
- var eventCount = 0;\r
- var added = [];\r
- for ( var i in expectedEvents ) {\r
- (function( event ) {\r
- // skip same listeners already added\r
- for ( var i in added ) {\r
- if ( added[i] == event ) {\r
- return;\r
- }\r
- }\r
- \r
- popcorn.listen( event, function() {\r
- eventCount++;\r
- var expected = expectedEvents.shift();\r
- if ( expected == event ) {\r
- ok( true, event + " is fired." );\r
- } else {\r
- ok( false, event + " is fired, expecting: " + expected );\r
- }\r
- });\r
- added.push( event );\r
- })( expectedEvents[i] );\r
- }\r
-\r
- // begin the test\r
- popcorn.play();\r
-\r
- // operations set1\r
- var set1Executed = false;\r
- popcorn.listen( 'playing', function() {\r
- // prevent double calling\r
- if ( set1Executed ) {\r
- return;\r
- }\r
-\r
- // toggle volume 1 second after playing\r
- setTimeout(function() {\r
- popcorn.volume(0);\r
- }, 1000);\r
-\r
- // pause 3 seconds after playing\r
- setTimeout(function() {\r
- popcorn.pause();\r
- }, 3000);\r
-\r
- set1Executed = true;\r
- });\r
-\r
- // operations set2\r
- var set2Executed = false;\r
- popcorn.listen( 'pause', function() {\r
- if ( set2Executed ) {\r
- return;\r
- }\r
-\r
- // continue to play\r
- setTimeout(function() {\r
- popcorn.play();\r
- }, 500);\r
-\r
- // seek to the end\r
- setTimeout(function() {\r
- popcorn.currentTime(48);\r
- }, 1000);\r
-\r
- set2Executed = true;\r
- });\r
-\r
- var time = 0,\r
- wait = 100,\r
- timeout = 15000;\r
-\r
- stop( timeout + wait );\r
- var interval = setInterval( function() {\r
- time += wait;\r
- if ( time > timeout ) {\r
- clearInterval( interval );\r
- return;\r
- }\r
- if ( eventCount == expectedEventCount ) {\r
- start();\r
- clearInterval( interval );\r
- }\r
- }, wait );\r
-});\r
-\r
+var ytReady = false;
+function onYouTubePlayerReady() {
+ ytReady = true;
+}
+
+test( "Popcorn YouTube Plugin Startup", function() {
+ var time = 0,
+ wait = 100,
+ timeout = 10000;
+
+ // wait for YouTube to start
+ stop( timeout + wait );
+
+ // run in an interval check if YouTube has started
+ var interval = setInterval(function() {
+ time += wait;
+
+ if ( ytReady ) {
+ start();
+ ok( true, "YouTube has started." );
+ clearInterval( interval );
+ return;
+ }
+
+ if ( time > timeout ) {
+ ok( false, "YouTube cannot be started." );
+ clearInterval( interval );
+ }
+ }, wait);
+});
+
+test( "Popcorn YouTube Plugin Event Tests", function() {
+ if ( !ytReady ) {
+ ok( false, "YouTube did not start." );
+ return;
+ }
+
+ var popcorn = Popcorn( new Popcorn.youtube( 'video' ) );
+ popcorn.volume(1); // is muted later
+
+ // check time sync
+ popcorn.exec(2, function() {
+ ok( popcorn.currentTime() >= 2, "Check time synchronization." );
+ });
+ popcorn.exec(49, function() {
+ ok( popcorn.currentTime() >= 49, "Check time synchronization." );
+ });
+ popcorn.exec(40, function() {
+ ok( false, "This should not be run." );
+ });
+
+ // events must be fired in this order
+ var expectedEvents = [
+ 'play',
+ 'loadeddata',
+ 'playing',
+ 'volumechange',
+ 'pause',
+ 'play',
+ 'playing',
+ 'seeked',
+ 'playing',
+ 'pause',
+ 'ended'
+ ];
+
+ var expectedEventCount = expectedEvents.length;
+ expect(expectedEventCount + 2);
+
+ // register each events
+ var eventCount = 0;
+ var added = [];
+ for ( var i in expectedEvents ) {
+ (function( event ) {
+ // skip same listeners already added
+ for ( var i in added ) {
+ if ( added[i] == event ) {
+ return;
+ }
+ }
+
+ popcorn.listen( event, function() {
+ eventCount++;
+ var expected = expectedEvents.shift();
+ if ( expected == event ) {
+ ok( true, event + " is fired." );
+ } else {
+ ok( false, event + " is fired, expecting: " + expected );
+ }
+ });
+ added.push( event );
+ })( expectedEvents[i] );
+ }
+
+ // begin the test
+ popcorn.play();
+
+ // operations set1
+ var set1Executed = false;
+ popcorn.listen( 'playing', function() {
+ // prevent double calling
+ if ( set1Executed ) {
+ return;
+ }
+
+ // toggle volume 1 second after playing
+ setTimeout(function() {
+ popcorn.volume(0);
+ }, 1000);
+
+ // pause 3 seconds after playing
+ setTimeout(function() {
+ popcorn.pause();
+ }, 3000);
+
+ set1Executed = true;
+ });
+
+ // operations set2
+ var set2Executed = false;
+ popcorn.listen( 'pause', function() {
+ if ( set2Executed ) {
+ return;
+ }
+
+ // continue to play
+ setTimeout(function() {
+ popcorn.play();
+ }, 500);
+
+ // seek to the end
+ setTimeout(function() {
+ popcorn.currentTime(48);
+ }, 1000);
+
+ set2Executed = true;
+ });
+
+ var time = 0,
+ wait = 100,
+ timeout = 15000;
+
+ stop( timeout + wait );
+ var interval = setInterval( function() {
+ time += wait;
+ if ( time > timeout ) {
+ clearInterval( interval );
+ return;
+ }
+ if ( eventCount == expectedEventCount ) {
+ start();
+ clearInterval( interval );
+ }
+ }, wait );
+});
+