Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1005

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T04:08:09+00:00 2024-11-25T04:08:09+00:00

Make Your Video Player Float Using PiP API

  • 62k

In this article, I am going to make your video player float by using Picture-in-Picture (PiP) JavaScript Web API. This could be an important feature you can add to your website. If your website is showing a demo or has many videos.

Most of you might have heard of this Picture-in-Picture Extension. It allows you to watch videos in a floating window (always on top of other windows). It works mostly on every site.

But we will create our custom Button that makes this feature available for our website by default. So our users won't need this extension. Let's dive into it.

What is Picture-in-Picture API?

The Picture-in-Picture API allows websites to create a floating video window always on top of other windows so that users may continue consuming media while they interact with other content sites or applications on their devices. Simple, Right?

Let's look at the demo

The following gif shows the demo of how that works:

Let's build it

First, we need a structure means HTML so for that I have simply used two things video and button.

<div class="video-container">    <video id="videoElement" controls loop src="https://imgur.com/yIVEIR9.mp4"></video>    <button id="toggleButton">Enable Floating Video</button> </div> 
Enter fullscreen mode Exit fullscreen mode

CSS for this can be found at the end of the article in the Codepen. Now the main thing is JavaScript. Let's add that.

First getting the video and button:

const video = document.getElementById("videoElement"); const toggleBtn = document.getElementById("toggleButton"); 
Enter fullscreen mode Exit fullscreen mode

After that, we need to verify if your browser supports that or not. You can do like the following:

// 👇Checking that if it is working or not if ("pictureInPictureEnabled" in document) {     /// .... other code } 
Enter fullscreen mode Exit fullscreen mode

Now we need to add an event listener on the toggleBtn and when it is clicked then we will run a function called togglePiPMode

 // 👇Checking that if it is working or not if ("pictureInPictureEnabled" in document) {   // 👇 Running 'togglePiPMode' function when user click the button   toggleBtn.addEventListener("click", togglePiPMode); } 
Enter fullscreen mode Exit fullscreen mode

Now it's time to create togglePiPMode function. First, I check if the user is already in PiP mode or not. If he is not the only then request for Picture-in-Picture mode. otherwise simply just exit from PiP. I have wrapped the code in try...catch block to make sure if something goes wrong then it will console the error.

function togglePiPMode() {   try {     // 👇 Checking that if user should not be in PiP mode     if (video !== document.pictureInPictureElement) {       video.requestPictureInPicture();     } else {       document.exitPictureInPicture(); // 👈 If already in PIP Mode then exit     }   } catch (error) {     console.log(error); // Console any error if any   } } 
Enter fullscreen mode Exit fullscreen mode

Now Picture-in-Picture mode will work flawlessly.

Let's understand events in PiP

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.*

  • enterpictureinpicture: Sent to a HTMLVideoElement when it enters picture-in-picture mode.
  • leavepictureinpicture : Sent to a HTMLVideoElement when it leaves picture-in-picture mode.
  • resize : Sent to a PictureInPictureWindow when it changes size.

Let's use enterpictureinpicture & leavepictureinpicture in our little project. I'll be changing the Button Text as these events are fired. Let's take a look at the code:

 // 👇 event trigger when you enter in PiP mode   video.addEventListener("enterpictureinpicture", () => {     toggleBtn.textContent = "Exit PiP Mode";   });    // 👇 event trigger when you leave the PiP mode   video.addEventListener("leavepictureinpicture", () => {     toggleBtn.textContent = "Enter PiP Mode";   }); 
Enter fullscreen mode Exit fullscreen mode

Now, Let's take a look at the resize event. When we run requestPictureInPicture then it returns a promise and we add resize event on pictureInPictureWindow when it fires then we call a function windowDimensions.

// 👇 This will print the dimensions of the window function windowDimensions(evt) {   const pipWindow = evt.target;   console.log(`Floating window Dimensions : ${pipWindow.width}x${pipWindow.height}px`); }  // 👇 resize event on pip window video.requestPictureInPicture().then((pictureInPictureWindow) => {   pictureInPictureWindow.onresize = windowDimensions; }); 
Enter fullscreen mode Exit fullscreen mode

From PictureInPictureWindow interface we can get the width and height and resize event of the floating video window.

Full JavaScript Code

const video = document.getElementById("videoElement"); const toggleBtn = document.getElementById("toggleButton");  // 👇Checking that if it is working or not if ("pictureInPictureEnabled" in document) {   // 👇 Running 'togglePiPMode' function when user click the button   toggleBtn.addEventListener("click", togglePiPMode);    // 👇 event trigger when you enter in PiP mode   video.addEventListener("enterpictureinpicture", () => {     toggleBtn.textContent = "Exit PiP Mode";   });    // 👇 event trigger when you leave the PiP mode   video.addEventListener("leavepictureinpicture", () => {     toggleBtn.textContent = "Enter PiP Mode";   }); }  function togglePiPMode() {   try {     // 👇 Checking that if user should not be in PiP mode     if (video !== document.pictureInPictureElement) {       video.requestPictureInPicture();     } else {       document.exitPictureInPicture(); // 👈 If already in PIP Mode then exit     }   } catch (error) {     console.log(error); // Console any error if any   } } 
Enter fullscreen mode Exit fullscreen mode

Codepen

Conclusion

In this article, I have explained how you can make a floating video player by using Picture-in-Picture (PiP) JavaScript Web API. Now, you can try to add this functionality to your website.

If you enjoyed this article, then don't forget to give ❤️ and Bookmark 🏷️for later use and if you have any questions or feedback then don't hesitate to drop them in the comments below. I'll see in the next one.

You might be interested in-

  • How to use Web Storage API?
  • How to use Battery Status API?
  • How to share anything from your website by Web Share API

Connect with me

Twitter GitHub LinkedIn Instagram Website Newsletter Support

beginnersjavascripttutorialwebdev
  • 0 0 Answers
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.