当选项卡处于后台时,requestVideoFrameCallback 不会触发

问题描述 投票:0回答:1

这是显示实际问题的代码片段,但基本上,当选项卡在 Chrome 中处于后台时,requestVideoFrameCallback 会停止运行。

function startTest() {
    video = document.querySelector('video');
    canvas = document.querySelector('canvas');
    ctx = canvas.getContext('2d');

    let start_time = 1.0, paint_count = 0, raf_count = 0;
    let last_time = 0;
    var paintFrame = function(timestamp, metadata) {
      if (start_time == 1.0)
        start_time = timestamp;
      updateCanvas(ctx, video);
      last_counter = metadata.presentedFrames;

      var elapsed_frame = 0;
      var elapsed = (timestamp - start_time) / 1000.0;
      
      if(last_time == 0){
        console.log('init')
        elapsed_frame = 0
      }else{
         elapsed_frame = (new Date()).getTime() - last_time
      }
      last_time = (new Date()).getTime()
      
      var fps_text = document.querySelector('#fps');
      var presented = metadata.presentedFrames + 1;
      fps_text.innerText =
          'actual fps: ' + (++paint_count / elapsed).toFixed(3) + ', ideal fps: ' +
          (presented / elapsed).toFixed(3) + ', missed: ' +
          (presented - paint_count) + '/' + presented +
          '(' +  ((presented - paint_count)/presented*100.0).toFixed(1) + '%)';
      
      if(elapsed_frame > 100){
        console.log('Long clock-time', elapsed_frame, 'missed', (presented - paint_count));
      }
      
      video.requestVideoFrameCallback(paintFrame);
    };
    video.onplay = function(){
      v = video.requestVideoFrameCallback(paintFrame);
    };

    video.playbackRate = 1.0;

    video.src="https://tguilbert-google.github.io/vid/buck360p_vp9.webm"

  };

https://codepen.io/jerodvenemafm/pen/gOqqWmJ

这里是 Chrome 文档的参考:

https://developer.chrome.com/blog/background_tabs/

并具体说明如果音频打开,应如何在后台运行:

https://bugs.chromium.org/p/chromium/issues/detail?id=1012063

两者似乎都暗示这不是预期的行为,所以我在这里有点不知所措。这是 Chrome 的错误,还是我缺少某种设置?

javascript google-chrome html5-video
1个回答
0
投票

requestVideoFrameCallback()
的规范说它......

注册一个回调,以便在下次呈现框架时触发 合成器。

https://wicg.github.io/video-rvfc/#dom-htmlvideoelement-requestvideoframecallback

我认为 Chrome 在选项卡不可见时不会执行此操作,因此也会停止调用回调。

类似地,当选项卡不可见时,对

requestAnimationFrame()
的调用也会停止。我想其中的原因非常相似。只要没有重绘,也就没有回调。

据我所知,播放音频(包括视频中的音频)的网站的例外情况仅适用于

setTimeout()
setInterval()

© www.soinside.com 2019 - 2024. All rights reserved.