【问题标题】:Check if picture in picture already exists in Javascript?检查Javascript中是否已经存在画中画?
【发布时间】:2021-02-12 01:54:49
【问题描述】:

我正在创建一个画中画元素,该元素将在 javascript 文件加载后立即触发。
最初我会使用网络摄像头流作为视频源,但在某个事件发生变化时,我想将视频源更改为画布元素。
有没有办法检查画中画元素是否已经在浏览器中打开?

【问题讨论】:

    标签: javascript canvas webrtc picture-in-picture


    【解决方案1】:

    如果 PIP 可用,document.pictureInPictureElement 将是视频元素,否则它将是 null

    https://developers.google.com/web/updates/2017/09/picture-in-picture

    // Hide button if Picture-in-Picture is not supported.
    pipButton.hidden = !document.pictureInPictureEnabled;
    
    pipButton.addEventListener('click', function() {
      // If there is no element in Picture-in-Picture yet, let's request Picture
      // In Picture for the video, otherwise leave it.
      if (!document.pictureInPictureElement) {
        video.requestPictureInPicture()
          .catch(error => {
            // Video failed to enter Picture-in-Picture mode.
          });
      } else {
        document.exitPictureInPicture()
          .catch(error => {
            // Video failed to leave Picture-in-Picture mode.
          });
      }
      
      console.clear()
      console.log(!document.pictureInPictureElement)
    });
    <button id="pipButton">Toggle PiP</button><br/>
    
    <video id="video" src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" width="50%"></video>

    【讨论】:

    • 我不认为这是 OP 的关注点,所以你有我的支持,但是这种方法不允许检查从其他文档打开的其他画中画窗口(IIRC 仍然只能是一个)跨度>
    猜你喜欢
    • 2016-12-04
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多