Picture-in-Picture API

Picture-in-Picture API

The Picture-in-Picture (PiP) API is a web API that allows developers to create a floating, always-on-top video window, separate from the main browser window. This is especially useful for enhancing user experience on media-heavy websites like video streaming platforms, video conferencing apps, or tutorials.

🧠 What is Picture-in-Picture?

Picture-in-Picture is a browser feature that allows users to continue watching a video in a small overlay window while interacting with other content. This window remains visible above other windows and can be repositioned by the user.

For example, YouTube and Google Meet both utilize PiP so users can multitask while still seeing the video feed.


🛠️ How to Use the PiP API

Here’s a step-by-step guide to implementing PiP on an HTML5 video element.

1. Basic HTML

<video id="video" controls>
  <source src="sample.mp4" type="video/mp4">
  Your browser does not support the video tag.
</video>
<button id="pipButton">Toggle PiP</button>
        

2. JavaScript to Toggle PiP

const video = document.getElementById('video');
const button = document.getElementById('pipButton');

button.addEventListener('click', async () => {
  try {
    if (document.pictureInPictureElement) {
      await document.exitPictureInPicture();
    } else {
      // Ensure video is playing first
      if (video !== document.pictureInPictureElement) {
        await video.requestPictureInPicture();
      }
    }
  } catch (error) {
    console.error('PiP toggle failed:', error);
  }
});        

Use event listeners to react to PiP changes:

video.addEventListener('enterpictureinpicture', () => {
  console.log('Entered Picture-in-Picture');
});

video.addEventListener('leavepictureinpicture', () => {
  console.log('Exited Picture-in-Picture');
});        


To view or add a comment, sign in

More articles by Fardeen Qureshi

  • Web Cypto Api

    The Web Cypto Api is the powerful cryptographic interface built into the Modern Web Browsers that enables developers to…

  • Drag and Drop Web API

    Introduction The Drag and Drop API, introduced with HTML5, allows developers to implement native drag-and-drop…

  • Virtual Keyboard API

    Introduction As more users access websites from mobile and touchscreen devices, managing the virtual (on-screen)…

  • Abort Controller

    Modern Web Applications often perform the asynchrous operations like network requests, which may become obsolete if the…

  • Media Capture and Streams API (Media Stream)

    The Media Capture and Streams API, commonly referred to as the MediaStream API or , allows web applications to access…

  • The Ideal Detection API

    Ideal Detection Api provides the means to detect the user idle status,active,idle and locked,and to be notified of…

  • Web Share Api

    The Web Share Api is a simple Javascript API that allows the web apps to share the content (text,URLs, or files) using…

  • Page Visibility Api

    The Page Visibility API is a web standard provided by modern browsers that allows developers to determine the…

  • Screen Wake Lock Api

    The Screen Wake Lock API allows web applications to request that a device’s screen remains on, even if the user is not…

  • Cookie Store Api

    The Cookie Store API is a relatively new web API that provides an asynchronous, Promise-based way to manage cookies in…

Explore topics