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
- Multitasking: PiP allows users to watch a video while simultaneously interacting with other content or applications on the same screen. This is particularly useful for productivity and multitasking scenarios.
- Customized Video Playback: Developers can provide users with the ability to control the size and position of the PiP video window, ensuring it doesn’t obstruct important content.
- Accessibility: PiP can improve accessibility by allowing users to move the video to a more convenient location on the screen or by enabling screen readers to focus on video content while users browse other webpage elements.
- User Engagement: Websites can use PiP to keep users engaged by allowing them to continue watching videos while navigating through different sections of the site or performing other tasks.
- User Control: PiP gives users more control over their viewing experience. They can resize, move, or close the PiP window as needed, providing a customizable and user-friendly interface.
Web API Picture-in-Picture Interfaces
- PictureInPictureWindow: The interface represents the floating video window; also contains width and height properties, and an “onresize” event handler property.
Web API Picture-in-Picture Methods
- HTMLVideoElement.requestPictureInPicture(): Method used to request that a video element on a web page enters Picture-in-Picture (PiP) mode.
- Document.exitPictureInPicture(): It is used to exit the Picture-in-Picture (PiP) mode and return the video or media element to its regular display mode.
Web API Picture-in-Picture Properties
- HTMLVideoElement.disablePictureInPicture: this property is used to tell the web browser not to suggest or automatically enable the Picture-in-Picture (PiP) feature for a video.
- Document.pictureInPictureEnabled: This property tells us whether we can use PIP mode are not. This is false if picture-in-picture mode is not available for any reason.
- Document.pictureInPictureElement: The pictureInPictureElement property tells you which Element is currently being displayed in the floating window. it is null if no element is in pip mode.
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
- enterpictureinpicture: This event is fired when any media / video element enters into pip mode.
- leavepictureinpicture: This event is fired when video element leaves the pip mode.
- resize: Tigered when pip 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:
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:
Browser support for PIP mode
- Google Chrome
- Microsoft edge
- Mozilla Firefox
- Safari