Why video.requestPictureInPicture() works only once?

后端 未结 1 594
借酒劲吻你
借酒劲吻你 2021-01-25 03:18

I\'m trying enter and exit PIP mode of video via Javascript onscroll function and I can only once enter and exit this mode. Here\'s my codepen:



        
1条回答
  •  孤独总比滥情好
    2021-01-25 03:53

    If it doesn't work, it's because scroll is not part of the user-trusted events.

    Now, that sometimes it works is actually weird... but has a rational explanation.

    User trusted events are usually considered alive for quite some time, but they should die eventually:

    btn_500ms.onclick = e => trigger_in(500); // works
    btn_6s.onclick = e => trigger_in(6000); // fails
    
    function trigger_in(ms) {
      setTimeout(() => {
        video.requestPictureInPicture()
          .then(() => {
            // auto-exit in 1s
            setTimeout(() => {
              document.exitPictureInPicture();
            }, 1000);
          })
          .catch(console.error);
      }, ms);
    };
    
    
    

    So I guess that what you interpreted as being working only on first scroll was actually caused by some circumstances where you did scroll after less than the max-life-time of a user trusted event (seems to be 5s in current Chrome74 btw). You can try by simply clicking anywhere in your codepen page before scrolling again.

    0 讨论(0)
提交回复
热议问题