Popcorn.js Documentation

Popcorn Methods

Popcorn methods for adding, removing, getting or updating popcorn track events. This allows the user to dynamically author a popcorn instance, such as changing the start time of a text event, even while the video is playing.

add

Purpose

Builds popcorn track events from popcorn plugins. Example, popcorn.subtitle( options ) would create a subtitle track event from the subtitle plugin.

Options

  • id [Integer] - optional id assigned to your new track event, if omitted a random id is assigned to your track event.
  • options [object] - object of properties to create a track event from, like start, end or text.
  • options.id [Integer] - optional id assigned to your new track event, if omitted a random id is assigned to your track event.

Use Cases

You want to add some extra content to your media. Example: display a subtitle or image at a certain time.

Examples

 1     // Create your plugin
 2     Popcorn.plugin( "example", {
 3       start: function() {}
 4     });
 5 
 6     // Create Popcorn instance
 7     var pop = Popcorn( "#video-element-id" );
 8 
 9     // create an example track event from the example plugin
10     pop.example({ start: 1 });
11     pop.example("example-id", { start: 1 });
12     pop.example({ start: 1, id: "another-id" });

popcorn.getLastTrackEventId()

Purpose

popcorn.getLastTrackEventId() returns the id of the most recently added track event.

Use Cases

You just created a track event and want to store its id with all the other track events for future update or removal.

Examples

 1     // Create your plugin
 2     Popcorn.plugin( "example", {
 3       start: function() {}
 4     });
 5 
 6     // Create Popcorn instance
 7     var pop = Popcorn( "#video-element-id" );
 8 
 9     // create an example track event from the example plugin
10     pop.example({ start: 1 });
11 
12     // id is now a randomly assigned id
13     var id = pop.getLastTrackEventId();
14 
15     // create an example track event from the example plugin
16     pop.example({ start: 1, id: "example-id" });
17 
18     // now id is "example-id"
19     id = pop.getLastTrackEventId();

popcorn.getTrackEvent( id )

Purpose

popcorn.getTrackEvent() returns the track event assigned to the id parameter.

Options

  • id [String] - the id assigned to the desired track event.

Use Cases

You want to check values stored in a track event, and have the id.

Examples

 1     // Create your plugin
 2     Popcorn.plugin( "example", {
 3       start: function() {}
 4     });
 5 
 6     // Create Popcorn instance
 7     var pop = Popcorn( "#video-element-id" );
 8 
 9     // create an example track event from the example plugin
10     pop.example({ start: 1 });
11 
12     // id is now a randomly assigned id
13     var id = pop.getLastTrackEventId();
14 
15     var trackEvent = pop.getTrackEvent( id );
16 
17     pop.example({ start: 1, id: "example-id" });
18 
19     trackEvent = pop.getTrackEvent( "example-id" );

popcorn.getTrackEvents()

Purpose

popcorn.getTrackEvents() returns an array track event objects attached to the popcorn instance.

Use Cases

You need to look through all track events attached to an instance, or you need the number of track events attached to an instance.

Examples

 1     // Create your plugin
 2     Popcorn.plugin( "example", {
 3       start: function() {}
 4     });
 5 
 6     // Create Popcorn instance
 7     var pop = Popcorn( "#video-element-id" );
 8 
 9     // create an example track event from the example plugin
10     pop.example({ start: 1 });
11 
12     // trackEvents[ 0 ] is the example track event, and trackEvents.length is 1
13     var trackEvents = pop.getTrackEvents();

popcorn.removeTrackEvent( id )

Purpose

popcorn.removeTrackEvent() removes an existing track event from the popcorn instance, via a track event’s id.

Options

  • id [String] - id assigned to the track event you wish to remove.

Use Cases

You want to remove a track event from the poporn instance.

Examples

 1     // Create your plugin
 2     Popcorn.plugin( "example", {
 3       start: function() {}
 4     });
 5 
 6     // Create Popcorn instance
 7     var pop = Popcorn( "#video-element-id" );
 8 
 9     // create an example track event from the example plugin
10     pop.example({ start: 1 });
11 
12     // id is now a randomly assigned id
13     var id = pop.getLastTrackEventId();
14 
15     pop.removeTrackEvent( id );
16 
17     pop.example({ start: 1, id: "example-id" });
18 
19     pop.removeTrackEvent( "example-id" );

update

Purpose

Updates popcorn track event properties on existing track events, like the start time or end time. This can be done dynamically while the video is playing.

Options

  • id [Integer] - id assigned to the track event you wish to update
  • options [object] - new options object you wish to apply to the existing track event, existing values on the track event that are not in the update options object are left unchanged.

Use Cases

You want to change a track events data on the fly, instead of removing and adding a new modified version of the track event.

Examples

 1     // Create your plugin
 2     Popcorn.plugin( "example", {
 3       start: function() {}
 4     });
 5 
 6     // Create Popcorn instance
 7     var pop = Popcorn( "#video-element-id" );
 8 
 9     // traditional, unchanging plugin call
10     pop.example({
11       start: 12,
12       end: 13
13     });
14 
15     // set up a new changeable trackevent
16     pop.example( "plugin-id", {
17       start: 10,
18       end: 11,
19       content: "Hi!"
20     });
21 
22     // change it to start at 11
23     pop.example( "plugin-id", {
24       start: 11,
25       content: "Hola!"
26     });
27 
28     // change it to end at 12
29     pop.example( "plugin-id", {
30       end: 12
31     });