The native picture-in-picture API allows you to create a floating, fixed HTML5 video overlaid on your workspace. This API is seamlessly integrated into the HTMLVideoElement interface and is super easy to use

<video id="vid" src="my-video.mp4"></video>

<button id="pip-btn">Enter PIP</button>
const vid = document.querySelector('#vid');
const pipBtn = document.querySelector('#pip-btn');

// On click of button, enter PIP mode
pipBtn.addEventListener('click', () => {
  vid.requestPictureInPicture();
});

By calling the requestPictureInPicturevideo element, our video will enter PIP mode

image.png

If needed, this API will also expose enterpictureinpicture and leavepictureinpicture events that you can use to run callbacks

vid.addEventListener('enterpictureinpicture', () => {
  pipBtn.textContent ='Vid is now PIP';
  pipBtn.disabled = true;
});

vid.addEventListener('leavepictureinpicture', () => {
  pipBtn.textContent ='Enter PIP';
  pipBtn.disabled = false;
});

All modern browsers support picture-in-picture, but Firefox has similar proprietary features

点赞(1)

评论列表 共有 0 评论

暂无评论

微信服务号

微信客服

淘宝店铺

support@elephdev.com

发表
评论
Go
顶部