JavaScript Picture-in-Picture API

By  on  

As a huge fan of media on the web, I'm always excited about enhancements to how we can control our media. Maybe I get excited about simple things like the <video> tag and its associated elements and attributes because media on the web started with custom codecs, browser extensions, and Flash. The latest awesome media feature I'm excited about on the web is Picture-in-Picture, a native web API that displays docked video. Let's have a look at how it works!

Benefits of Picture-in-Picture

Before using Picture-in-Picture, it's important to know why to use it and the benefits you get:

  • No need to create your own docked video element when the user scrolls -- the browser does it for you!
  • Big performance, usability, and maintenance win
  • The docked video displays above all tabs in your browser, not just the tab it was launched in
  • Stays in PiP mode until you change pages

Detect Picture-in-Picture Support

Before using any API it's best to ensure the feature is supported and enabled; for that the browser exposes document.pictureInPictureEnabled, as well as a per-video attribute:

if(
    document.pictureInPictureEnabled && 
    !videoElement.disablePictureInPicture
) {
    // Yay, we can use the feature!
}

With the browser supporting the API, it's time to get started!

Launching Picture-In-Picture

Before launching into picture-in-picture, it's important to first check that we aren't already in PiP:

if(!document.pictureInPictureElement) {
    videoElement.requestPictureInPicture();
}

Once we confirm that PiP isn't being used, we can launch PiP by calling requestPictureInPicture() on the HTMLVideoElement.

Exiting Picture-in-Picture

To exit Picture-in-Picture mode, you can call exitPictureInPicture():

document.exitPictureInPicture();

The PiP window will be removed from the bottom of your browser.

Safe Picture-in-Picture Function

Bringing together the code samples provided, we can put together a safe Picture-in-Picture utility function:

function enterPictureInPicture(videoElement) {
    if(
        document.pictureInPictureEnabled && 
        !videoElement.disablePictureInPicture) {
            try {
                if (document.pictureInPictureElement) {
                    document.exitPictureInPicture();
                }
                videoElement.requestPictureInPicture();
            } catch(err) {
                console.error(err);
            }
    }
}

This function can safely put your video into Picture-in-Picture while protecting you from errors or videos already PiP.

Picture-in-Picture Events

Along with the functionality to enter and exit Picture-in-Picture, a set of JavaScript events are emitted during each step:

  • enterpictureinpicture: fired on the HTMLVideoElement that was placed into Picture-in-Picture
  • leavepictureinpicture: fired on the HTMLVideoElement for which PiP was exited

Here's a quick example of each:

$("#myVideo").addEventListener("enterpictureinpicture", e => {
    // Entered PiP!
});

$("#myVideo").addEventListener("leavepictureinpicture", e => {
    // Exited PiP!
});

Picture-in-Picture mode is an amazing API to allow users to consume video content as they explore other tabs or simply navigate within your page. Coupled with the scroll event or even with Intersection Observer API, Picture-in-Picture is a powerful feature your users will love!

Recent Features

  • By
    Designing for Simplicity

    Before we get started, it's worth me spending a brief moment introducing myself to you. My name is Mark (or @integralist if Twitter happens to be your communication tool of choice) and I currently work for BBC News in London England as a principal engineer/tech...

  • By
    9 Mind-Blowing WebGL Demos

    As much as developers now loathe Flash, we're still playing a bit of catch up to natively duplicate the animation capabilities that Adobe's old technology provided us.  Of course we have canvas, an awesome technology, one which I highlighted 9 mind-blowing demos.  Another technology available...

Incredible Demos

  • By
    Using jQuery and MooTools Together

    There's yet another reason to master more than one JavaScript library: you can use some of them together! Since MooTools is prototype-based and jQuery is not, jQuery and MooTools may be used together on the same page. The XHTML and JavaScript jQuery is namespaced so the...

  • By
    Growl-Style Notifications Using MooTools Roar

    When I think of premier MooTools plugin developers, Harald "digitarald" Kirschner is usually one of the first people that come to mind. Harald has built some of MooTools' most popular plugins, including AutoCompleter, FancyUpload, and History Manager. My favorite plugin created...

Discussion

  1. doesn’t seem to work in Firefox…

    • Adam

      You can check the support here: https://caniuse.com/#feat=picture-in-picture

      Looks like Firefox: “Partial support in Firefox refers to the fact that Mozilla provides an equivalent proprietary feature, which allows users to use Picture-in-Picture mode for all playing videos.”

  2. Very handy when I have my end user hat on: Google made an extension for Chrome which just puts a page’s element in Picture-in-Picture via keyboard shortcut. This is pretty great since many players don’t support PiP out of the box yet.

    https://chrome.google.com/webstore/detail/picture-in-picture-extens/hkgfoiooedgoejojocmhlaklaeopbecg

  3. Works like a charm except on Firefox. where it isn’t completely compatible yet. Thank you so much or sharing! We really appreciate these tricks your share!

  4. Very handy. Is there a way to have multiple Picture-in-Picture’s running?

  5. johnnyysmith

    That’s a lot!It worked for me. That’s what I was looking for

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!