The Picture-in-Picture API for Video
Picture-in-Picture (PiP) API enables video to play in a floating window while users navigate elsewhere; 1. Check support via 'pictureInPictureEnabled' and video's disablePictureInPicture attribute; 2. Trigger PiP with requestPictureInPicture() on user gesture; 3. Handle state changes with enterpictureinpicture and leavepictureinpicture events; the feature requires user interaction, allows only one PiP window at a time, has limited mobile support, may exclude DRM content, and offers no styling control, but enhances multitasking when used appropriately with proper UI feedback and support checks.
Picture-in-Picture (PiP) is a handy feature that lets users continue watching a video in a small, floating window while they navigate elsewhere on their device—like browsing another page or using a different app. On the web, this functionality is enabled through the Picture-in-Picture API, a relatively new but increasingly supported web API that gives developers control over PiP behavior for <video></video> elements.
Here’s what you need to know to use it effectively.
What the PiP API Does
The Picture-in-Picture API allows a web page to programmatically enter and exit PiP mode for a video. Once activated, the video pops out of the browser into a small movable window that stays on top of other apps, letting users multitask without interrupting playback.
It works only with <video></video> elements and requires user interaction to initiate (for security and user experience reasons). Browsers like Chrome, Edge, and some versions of Safari support it, but Firefox and others do not yet.
How to Use the PiP API
Using the API is straightforward. Here are the key parts:
1. Check Support and Availability
Before trying to use PiP, check if the browser supports it:
if ('pictureInPictureEnabled' in document) {
console.log('PiP is supported!');
}You can also check if a specific video can enter PiP:
if (!video.disablePictureInPicture) {
// Video is allowed to go into PiP
}Note: Use the
disablePictureInPictureattribute on the<video>tag to prevent PiP if needed.
2. Request PiP Mode
To enter PiP, call requestPictureInPicture() on the video element:
async function openPiP() {
try {
await video.requestPictureInPicture();
} catch (err) {
console.error("Failed to enter PiP:", err);
}
}This must be triggered by a user gesture (like a click), or it will fail.
3. Handle PiP State Changes
You can listen for when PiP starts or ends:
video.addEventListener('enterpictureinpicture', (event) => {
console.log('Entered PiP');
// Adjust UI, e.g., hide controls
});
video.addEventListener('leavepictureinpicture', (event) => {
console.log('Exited PiP');
// Restore UI
});These events help keep your interface in sync.
Practical Example: Add a PiP Button
<video id="myVideo" controls> <source src="demo.mp4" type="video/mp4"> </video> <button id="pipButton">Toggle PiP</button>
const video = document.getElementById('myVideo');
const pipButton = document.getElementById('pipButton');
pipButton.addEventListener('click', async () => {
if (document.pictureInPictureElement) {
// Exit if already in PiP
await document.exitPictureInPicture();
} else {
// Enter PiP
await video.requestPictureInPicture();
}
});
// Disable button when PiP isn't available
pipButton.disabled = !document.pictureInPictureEnabled;This creates a simple toggle button that respects the current PiP state.
Important Notes and Limitations
- User gesture required: You can’t auto-start PiP when a page loads.
- Only one PiP window at a time: If another video (or app) is in PiP, the current one will exit.
- Mobile support is limited: Most mobile browsers don’t support PiP due to UI constraints.
- Not all videos can enter PiP: DRM-protected content or videos with certain restrictions may be blocked.
- Styling limitations: The PiP window is controlled by the OS/browser, so you can’t customize its appearance.
Conclusion
The Picture-in-Picture API is a powerful but simple tool for enhancing user experience with video content. By letting users keep videos visible while browsing, it adds convenience—especially for tutorials, videos, or live streams.
Use it thoughtfully: provide clear controls, respect user intent, and always check for support. With growing browser adoption, PiP is becoming a standard feature for modern web video.
Basically, it’s not magic—just a small API that makes multitasking a lot smoother.
The above is the detailed content of The Picture-in-Picture API for Video. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20519
7
13632
4
How to create a progress bar for file uploads in HTML5? (Progress tag)
Mar 06, 2026 am 02:22 AM
Why can't the tag directly display the upload progress? It is a read-only visual component. It does not listen to network requests and is not automatically bound to the upload process of XMLHttpRequest or fetch. If you put it in and don't update the value manually, it will always stop at 0%. What really drives it is the event monitoring in the upload logic you write yourself. A common mistake is to only monitor onload (upload completed) but miss upload.onprogress. XMLHttpRequest (not fetch) must be used to obtain real-time upload progress, because fetch does not expose the max attribute of the event in the upload phase and must be set to the file size (file.size
How to create a tooltip using only HTML5? (Title attribute)
Mar 06, 2026 am 12:23 AM
The title attribute is not a tooltip component, but an accessibility prompt mechanism implemented by the browser. The behavior, style, and interaction are uncontrollable and are only suitable for simple scenarios such as pure information supplement.
How to center an image vertically in HTML5? (Layout techniques)
Mar 07, 2026 am 02:05 AM
Flexbox is the most stable for centered images. The key is to set display:flex and align-items:center in the parent container and specify the height; using place-items:center for Grid is more concise; absolute positioning requires top:50% with transform:translateY(-50%); vertical-align is invalid for block-level centering.
How to create a simple offline web app with HTML5? (Application Cache)
Mar 06, 2026 am 02:16 AM
ApplicationCache has been completely abandoned and will be removed from Chrome 61, Firefox 72, and Safari 11.1. ServiceWorker must be used instead; the latter requires HTTPS, manual registration and cache control, and the path, scope, and life cycle must match exactly.
How to create a contact form with validation in HTML5? (Required attribute)
Mar 06, 2026 am 02:06 AM
required only verifies that it is not empty, not the format; type="email" or pattern must be used together; native verification is only triggered when submitting, not real-time; checkbox/radio/select/textarea has special behavior; the server must re-verify and clean empty values.
How to create a collapsible details section in HTML5? (Summary tag)
Mar 06, 2026 am 02:25 AM
Use and implement native folding area. HTML5 natively supports folding and expansion. It can work without JS. It is a container and a click area. The browser adds a small triangle by default. Click it to expand/collapse the content. A common mistake is to write a closing tag (for example), which must have a start and end tag, and must be the first child element of , otherwise the folding logic will fail. The default is the collapsed state; add the open attribute to expand it by default: you can put text, icons, and even text inside, but don't nest another one - some browsers have inconsistent behavior and do not support IE. Edge79, Chrome12, Firefox49, and Safari6 have good support.
How to use the dialog element for modals in HTML5? (Native popups)
Mar 06, 2026 am 01:26 AM
The element does not render by default, does not occupy the layout, and is not recognized by screen readers. It must explicitly set the open attribute or call showModal()/show() to activate; to close, you need to call close(), click mask or press Esc to monitor manually; the old version of Safari needs to be polyfilled or downgraded; form submission will not automatically close and must be intercepted and manually controlled.
How to preload video or audio content in HTML5? (Preload attribute)
Mar 06, 2026 am 01:59 AM
What are the values of the preload attribute, and what are their effects? The preload of HTML5 is a suggestive attribute. The browser is not forced to comply with it, but it will affect the initial resource loading strategy. It has only three legal values: auto, metadata, none. auto: It is recommended that the browser downloads the entire media file (including audio and video data) as soon as possible, which is suitable for scenarios where users are likely to play it, such as carousel videos on the homepage; but it will waste bandwidth, especially on mobile terminals or under weak networks. metadata: only prefetch meta information such as duration, size, frame rate, cover image, etc., without loading the actual audio and video frames, which is a safe choice in most cases none: explicitly tell the browser "don't load it yet" and wait until the user triggers playback (such as clicking) before starting it





