Wistia Video Player API

Overview

The Wistia video player has a JavaScript API which supports a number of ways to interact with and control the video player. It uses the same interface for both Flash and HTML5 versions of the player, and provides convenience functions to accomplish common goals.

Note: for custom javascript you write to interface with a Wistia embed, add it to your source under the Wistia embed (ie. the footer)

The Methods

Method Description
bind(event, function) This lets you execute a function when a video event occurs. Possible values for “event” are: “play”, “pause”, “end”, “conversion”, and “timechange”.
height() Gets the current height of the embed (the video plus any plugins above or below).
height(h) Sets the height of the embed. The video will be resized to fit with the plugins fully visible.
duration() Returns the length of the video in seconds.
pause() This causes the video player to pause the video if it is currently playing.
play() This causes the video player to start (or continue playing from a paused state) playing the video.
ready(function) This method is only necessary for advanced use cases. It lets you run a function as soon as the video is loaded and ready to be played.
setEmail(email) Associates the email string with this view of the video.
state() This returns the current state of the video player: “unknown” (a.k.a not started), “ended”, “playing”, “paused”.
time() This returns the viewer's current position in the video (in seconds).
time(t) This causes the video player to seek to time specified by the t parameter (in seconds).
unbind(event, function) Lets you remove a previously binded function from an event. If function is not specified, all bindings for the event will be removed.
videoHeight() Gets the current height of the video, excluding any plugins.
videoHeight(h) Sets the height of the video. The height of the embed will be automatically resized to fit the plugins.
videoWidth() Gets the current width of the video, excluding any plugins.
videoWidth(w) Sets the width of the video. The width of the embed will be automatically resized to fit the plugins.
volume() Returns the current volume level. Value between 0 and 1.
volume(level) Sets the current volume level. 'level' is a decimal value between 0 and 1.
width() Gets the current width of the embed (the video plus any plugins on the left and right).
width(w) Sets the width of the embed. The video will be resized to fit with the plugins fully visible.

How to use the API

Accessing the Player API is different depending on the type of embed code you're using.

  • For the iframe embed code (this is the default embed type), you can use the iframe player API.
  • Or, if you're using the API embed code, you can access the JavaScript API directly.

iframe embed code

To access the API when using iframe embed codes, you just need to include an extra script on your page and you're off to the races. Take a look at the iframe player API page for all the details.

API embed code

The 'API' version of the Wistia embed codes includes a variable wistiaEmbed to make this easy.

var wistiaEmbed = Wistia.embed("bfc34aa023", { ... options ... });

In this instance, you can reference the video object using the wistiaEmbed variable. If you have multiple videos on your page, you should update this variable to something specific to this video.

As an example, if the following JS code is executed, the video will start to play:

wistiaEmbed.play();

Examples

Trigger an event at a specific time

Wistia's video player API provides functionality to easily accomplish common goals.

In this example, let's assume that we want to fire a Javascript function when the viewer gets 60 seconds into the video. In order to accomplish this, we only need the bind method from the API. The Javascript code can be seen below:

<script type="text/javascript"> 
  wistiaEmbed.bind("timechange", function (t) {
    if(t > 60 && t < 62) {
      // Insert code to be executed here
    }
  });
</script> 

The bind function monitors the state of the video in an event loop. Every 500 milliseconds, it checks to see if the video's time position has changed. If it has, it runs your function with the current time (t) as the only argument.

Alert when the video ends

<script type="text/javascript"> 
  wistiaEmbed.bind("end", function () {
    alert("Hello world!");
  });
</script> 

Grab the email address of your video viewers

With Wistia Turnstile, you can require your viewers to enter an email address to view video content on your webpage. Using the “conversion” event, you can trigger actions based on the email being entered - including passing that email on to another service!

<script type="text/javascript">
wistiaEmbed.bind("conversion", function(type, val) {
// function to be executed
});
</script>


At this time, the “type” of conversion is always 'pre-roll-email', and the “val” is the viewers email address.

Alert on play just once

With the bind method, every time “play” is triggered, your function will be executed. But sometimes a user will scroll back to the beginning and hit Play again. If you want to avoid your function being executed again, you need to unbind it.

<script type="text/javascript">
  function playFunc() {
    alert("Played the first time!");
    wistiaEmbed.unbind("play", playFunc);
  }
  wistiaEmbed.bind("play", playFunc);
</script>

Embedding Options

In our example embed Wistia.embed(“bfc34aa023”, { … options … }); there are two arguments: the media's hashed ID, and a set of embedding options. Here is a list of available options:

Option Name Type Description
autoPlay boolean flash/html5 only. Doesn't work on mobile. When true, the video plays as soon as it's ready.
canonicalTitle string The title of the page, used for social features.
canonicalUrl string The url of the page, used for social features.
chromeless boolean flash only. When true, player is created without controls.
container string The element that should container the embed. Defaults to wistia_{hashed_id}
controlsVisibleOnLoad boolean flash only. When true, controls are visible before you click play.
doNotTrack boolean When true, embed will not track views.
endVideoBehavior string flash only. Behavior when the video ends: default/reset/loop.
fullscreenButton boolean Show fullscreen button. Default is true.
pageUrl string The page that the embed thinks it's embedded on. Defaults to the current page.
platformPreference string Accepts: 'flash', 'html5', or 'external'. This specifies the preferred underlying video embed mechanism. If your specified type is not supported by a client it will seamlessly fallback to the other types. Defaults to 'flash'.
playButton boolean flash/external only. When true, display play button over video poster.
playbar boolean Show playbar. Default is true.
smallPlayButton boolean Show small playbutton in the bottom left. Default is true.
stillUrl string The still image that should appear before the video is played.
trackEmail string flash/html5 only. The email address to associate with the viewing session.
videoFoam boolean The embed will conform to the width of the parent element, resizing to maintain the correct aspect ratio. For iframes, requires the iframe API. API/SEO embeds don't need any modifications. Check out the demo!
videoQuality string Specify the starting video quality. sd-only/hd-only/auto
videoWidth integer The original width of the video.
videoHeight integer The original height of the video.
volumeControl boolean Show volume control. Default is false.
wmode string flash only. The flash window mode of the embed. window/direct/opaque/transparent/gpu.

Miscellaneous Options

To show how options work, suppose I want to embed a video that defaults to the HTML5 player and plays automatically on page load. I might alter the embed javascript to look like this:

<div id="my_container"></div>
<script type="text/javascript"> 
  var wistiaEmbed = Wistia.embed("bfc34aa023", {
    platformPreference: "html5",
    autoPlay: true,
    container: "my_container"
  });
</script>

Plugin Options

Our plugins have a lot of options too! Check them out:

And if you're looking at these, you might want more info on constructing an embed code.