Open In App

Web API Picture-in-Picture

Web API Picture-in-Picture feature enables websites to display a video in a floating window that stays on top of windows. This allows users to keep watching videos while interacting with content or applications, on their device.

Concepts and usage

Web API Picture-in-Picture Interfaces

Web API Picture-in-Picture Methods

Web API Picture-in-Picture Properties

Web API Picture-in-Picture Events

The Picture-in-Picture API defines three events, which can be used to detect when picture-in-picture mode is toggled and when the floating video window is resized



Example: In the following example we simply create a web page with a centered video player and a “Enter to PIP” button. When the button is clicked, it requests Picture-in-Picture (PiP) on video element mode for the video if the browser supports it.




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>PiP Example</title>
    <style>
        body {
            background: linear-gradient(to bottom, #3498db, #2c3e50);
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
 
        .container {
            text-align: center;
            background-color: rgba(255, 255, 255, 0.8);
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
        }
 
        video {
            width: 200px;
            /* Set the desired width */
            height: 300px;
            /* Set the desired height */
            max-width: 100%;
            margin: 0 auto;
            /* Center the video horizontally */
            margin-bottom: 10px;
            display: block;
            /* Remove any default inline display */
        }
 
        button {
            padding: 10px 20px;
            font-size: 16px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
 
        button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
 
<body>
    <div class="container">
        <h1>Video Player</h1>
        <video controls>
            <source src="Sample.mp4" type="video/mp4">
            Your browser does not support the video tag.
        </video>
        <button id="pipButton">Enter to PIP</button>
    </div>
 
    <script>
        const videoElement =
                  document.querySelector('video');
        const pipButton =
                  document.getElementById('pipButton');
 
        //checking whether browser support pip
        if (videoElement && 'pictureInPictureEnabled' in document) {
            pipButton.addEventListener('click', enterToPip);
 
            async function enterToPip() {
                try {
                    //requesting for pip
                    await videoElement.requestPictureInPicture();
                }
                catch (error) {
                    console.error('Error PiP:', error);
                }
            }
        } else {
            console.error('PiP is not supported in this browser.');
            pipButton.style.display = 'none';
        }
    </script>
</body>
 
</html>

Output:



Output

Example 2: In this example we created a simple html page with one video player and with two buttons one is enter to PIP and another is Exit from PIP. when a user clicked on Enter to pip then video element enters into pip then user clicks the exit from pip then the video return s to its regular display mode.




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>PiP Example</title>
    <style>
        body {
            background: linear-gradient(to bottom, #3498db, #2c3e50);
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
 
        .container {
            text-align: center;
            background-color: rgba(255, 255, 255, 0.8);
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
        }
 
        video {
            width: 300px;
            /* Set the desired width */
            height: 200px;
            /* Set the desired height */
            max-width: 100%;
            margin: 0 auto;
            /* Center the video horizontally */
            margin-bottom: 10px;
            display: block;
            /* Remove any default inline display */
        }
 
        button {
            padding: 10px 20px;
            font-size: 16px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
 
        button:hover {
            background-color: #0056b3;
        }
 
        .hidden {
            display: none;
        }
    </style>
</head>
 
<body>
    <div class="container">
        <h1>Video Player</h1>
        <video controls>
            <source src="Sample.mp4" type="video/mp4">
            Your browser does not support the video tag.
        </video>
        <button id="enterPiPButton">Enter PiP</button>
        <button id="exitPiPButton" class="hidden">
              Exit PiP
          </button>
    </div>
 
    <script>
        const videoElement =
              document.querySelector('video');
        const enterPiPButton =
              document.getElementById('enterPiPButton');
        const exitPiPButton =
              document.getElementById('exitPiPButton');
 
        // Check if PiP is supported in the browser
        if (videoElement && 'pictureInPictureEnabled' in document) {
            enterPiPButton.addEventListener('click', enterPiP);
            exitPiPButton.addEventListener('click', exitPiP);
 
            async function enterPiP() {
                try {
                    // Request PiP mode
                    await videoElement.requestPictureInPicture();
 
                    // Hide the "Enter PiP" button and show the
                      // "Exit PiP" button
                    enterPiPButton.classList.add('hidden');
                    exitPiPButton.classList.remove('hidden');
                } catch (error) {
                    console.error('Error entering PiP:', error);
                }
            }
 
            async function exitPiP() {
                try {
                    // Exit PiP mode
                    await document.exitPictureInPicture();
 
                    // Hide the "Exit PiP" button and show the
                      // "Enter PiP" button
                    exitPiPButton.classList.add('hidden');
                    enterPiPButton.classList.remove('hidden');
                } catch (error) {
                    console.error('Error exiting PiP:', error);
                }
            }
        } else {
            console.error('PiP is not supported in this browser.');
            enterPiPButton.style.display = 'none';
        }
    </script>
</body>
 
</html>

Output:

Output

Browser support for PIP mode


Article Tags :