Utility methods provide various helper functions that may be useful to the user. Each of these utility methods are accesable through the Popcorn namespace and are not instance specific.
Popcorn.destroy( instance )
Purpose
Stops the instance’s timeUpdate loop and removes any associated events.
Options
instance [Object] - a reference to the instance you wish to destroy;
Use Case
Your web application needs to create a new instance of Popcorn, using the same video source as the first.
Iterates over either an object or an array, passing each item or property to callback( value, key ). If a context parameter is provided, it will be used as the this value of callback. If it is not provided, the current context is used instead.
Options
item [Object
Array] - the item to be parsed
callback [Function] - a callback function with the following parameters:
value [String
Number] - the value of the current object that is keyed by key
key [String
Number] - the position in the object or value where the value is stored
url [String] - a string of the name of the script to be loaded
callback=jsonp ( or similar ) [Function] - params are required to use with any JSONP service
successCallback [Function] - a function to be run when the script has been successfully loaded
Examples
An example showing how to get JSON data from a local json file using Popcorn.xhr
1
2
3
4
5
6
7
8
9
10
11
Popcorn.xhr({url:"jsonp.json?callback=jsonp",dataType:"jsonp",success:function(data){/*
`data` will be the parsed json object
*/}});
An example showing how to get JSON data from a local json file using Popcorn.getJSONP
1
2
3
4
5
6
7
8
9
10
Popcorn.getJSONP("jsonp.json?callback=jsonp",function(data){/*
`data` will be the parsed json object
*/});
An example showing how to get JSON data from a remote site using Popcorn.xhr
1
2
3
4
5
6
7
8
9
10
11
Popcorn.xhr({url:"http://domain.com/service/?callback=jsonp",dataType:"jsonp",success:function(data){/*
`data` will be the parsed json object
*/}});
An example showing how to get JSON data from a remote site using Popcorn.JSONP
1
2
3
4
5
6
7
8
9
10
Popcorn.getJSONP("http://domain.com/service/?callback=jsonp",function(data){/*
`data` will be the parsed json object
*/});
Popcorn.getScript( url, successCallback )
Purpose
Request remote JavaScript resources.
Options
url [String] - a string of the name of the script to be loaded
successCallback [Function] - a function to be run when the script has been loaded
Use Case
Load an external script for a plugin ( take a look at many of the current Popcorn plugins )
Examples
An example showing how to get a remote script, no callback being used
1
2
3
4
// Load in Processing.jsPopcorn.getScript("http://processingjs.org/content/download/processing-js-1.0.0/processing-1.0.0.min.js");
An example how to get a local script using Popcorn.xhr with a callback function
1
2
3
4
5
6
7
8
9
10
Popcorn.xhr({url:"local-script-resource.js",dataType:"script",success:function(){/*
fired when script has loaded and is ready to use
*/}});
An example of how to get a local script using Popcorn.getScript with a callback
1
2
3
4
5
6
7
8
9
Popcorn.getScript("local-script-resource.js",function(){/*
fired when script has loaded and is ready to use
*/});
An example of how to get a remote script using Popcorn.getScript with a callback
1
2
3
4
5
6
7
8
9
Popcorn.getScript("http://processingjs.org/content/download/processing-js-1.0.0/processing-1.0.0.min.js",function(){/*
fired when script has loaded and is ready to use
*/});
Popcorn.guid( [prefix] )
Purpose
Return a unique id with an optional prefix
Options
prefix [String] - string representing the string that will prefix the guid that is returned. For example if prefix is “foo” the returned value would be foo1319565291420
When a page loads, Popcorn will initialize a table of useful language and locale related values. The table can be updated with a call to Popcorn.locale.set(), eg. Popcorn.locale.set(“fr-CA”);. Calls to Popcorn.locale.set(); will result in the “locale:changed” event being fired on all currently existing Popcorn instances. This allows program code, plugins and players to listen for changes in the locale and react accordingly.
Options
Popcorn.locale.get() [Function] - returns the current locale object
Popcorn.locale.set( langRegion ) [Function]- set the locale, which is a string that takes the form of “lang-region” ex: “fr-CA”
Event: “locale:changed” - an event fired when the locale changes
parserName [String] - will be the name for your newly created parser. Will also be the name of the instance method in which your parser will be accessed through.
fn [Function] - function that will get run in order to parse the data being passed in
Popcorn.plugin.debug is a boolean flag that toggles error suppression on Popcorn plugin functions (start, end, etc.).
When it is set to false, errors are caught and stored inside Popcorn.plugin.errors, and an “error” event is triggered on the instance.
When set to true, errors will not be suppressed.
Options
Popcorn.plugin.debug [Boolean] - Toggle debug mode on or off
Use Case
During plugin development, debug mode may be turned on to have Popcorn suppress errors thrown inside of plugins.
In production environments, debug mode should be set to false, allowing error handling to be performed by listening to the “error” event on the Popcorn instance.
Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Turn on debug modePopcorn.plugin.debug=true;// Create a plugin that will throw an error when it's start function runspop.plugin("foo",{start:function(){thrownewError();}});// Create Popcorn instancevarpop=Popcorn("#video-element-id");// set up "foo" eventpop.foo({start:1});// The error will not be caught by Popcornpop.play();
// Debug mode is off by default// Create a plugin that will throw an error when it's start function runsPopcorn.plugin("foo",{start:function(){thrownewError();}});// Create Popcorn instancevarpop=Popcorn("#video-element-id");// set up "foo" eventpop.foo({start:1});// The error will be caught by Popcorn.plugin.errorspop.listen("error",function(e){// 'e' is Popcorn.plugin.errors, which can be accessed outside the "error" event callback.console.log(e);});pop.play();
Popcorn.plugin.errors is an array object that contains errors thrown from plugins when Popcorn is not in debug mode.
Each error object in Popcorn.plugin.errors will look like:
Options
Popcorn.plugin.errors [Array Object] - Stores suppressed errors from plugins
Use Case
Inspection of this object can help a developer solve problems with their Popcorn projects.
The redirection of errors to Popcorn.plugin.errors makes sure that execution of the entire project continues even in the case of a single plugin failing during runtime.
// Debug mode is off by default// Create a plugin that will throw an error when it's start function runsPopcorn.plugin("foo",{start:function(){thrownewError();}});// Create Popcorn instancevarpop=Popcorn("#video-element-id");// set up "foo" eventpop.foo({start:1});// The error will be caught by Popcorn.plugin.errorspop.listen("error",function(e){// 'e' is Popcorn.plugin.errors, which can be accessed outside the "error" event callback.console.log(e);});pop.play();
Creates a new Popcorn proto method of the name provided with the definitionObject.
All Popcorn instances will inherit this newly created method. See below for plugin authoring patterns; see the Step-by-step instructions for creating Popcorn Plugins for detailed plugin authoring information.
Options
pluginName [String] - will be the name for your newly created plugin. Will also be the name of the instance method in which your plugin will be accessed through.
definitionObject [Object] - an object in which the setup, start, end, teardown functions will be implemented for the given plugin
manifest [Object] - an object that explains the given plugin. Any options that are passed on to the user are documented here. Manifest are read in by Popcorn-Maker in order to create a meaningful editor. A more in-depth explanation can be found here
Use Cases
Develop a plugin for a popular web service in order to extend additional content during a popcorn video
An optional way to use Popcorn’s constructor. Instead of passing in an id as a parameter, a callback function is passed in that will be called when the DOM is ready.
Options
callback [Function] - function. A function that will be called when the DOM is ready
smpte [String] - a string that contains a valid timestamp in the form of either HH:MM:SS.MMM or HH:MM:SS;FF. Hours and minutes are optional and will default to 0.
fps - an optional number specifying the frames per second