<!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
>