【问题标题】:how to run js code when video is fully buffered/loaded视频完全缓冲/加载时如何运行js代码
【发布时间】:2014-01-29 21:01:06
【问题描述】:

我已经在谷歌上搜索了这个,但没有什么能做它应该做的事情 它在 IE 中完美运行......你可以在日志中看到它完美运行......但它不会在 chrome 中运行! 为什么它在 chrome 中这样做? ...它从 catche 加载 10sek,然后什么都没有 ...

<div style=" width:100%; height:320px; margin-top:-95px; background-image:url('video-logo.png'); background-repeat:no-repeat; background-position:center;"> 
<div id="videoRain" class="videoWrapper" style="text-align:center; display: none;"> 
<video id="rainVideo" width="100%" height="400px" preload="auto"> 
  <source src="rain-video.mp4" type="video/mp4"> 
  Your browser does not support the video tag. 
</video> 
</div> 
</div> 
<script> 
setTimeout(function(){ 
var refreshId = window.setInterval(function(){ 
   myVid=document.getElementById('rainVideo'); 
   puhvitud = Math.round(myVid.buffered.end(0)); 
   if (puhvitud >= 14) { 
  setTimeout(function(){document.getElementById('videoRain').style.display='block';},0); 
      setTimeout(function(){document.getElementById('rainVideo').play();},0); 
      clearInterval(refreshId); 
      setTimeout(function(){document.getElementById('videoRain').style.display='none';},15000); 
   } 
   console.log(puhvitud); 
}, 2000); 
},1000); 
</script> 

也许有人有另一种方法来做到这一点? 当视频完全加载...这应该运行...

setTimeout(function(){document.getElementById('videoRain').style.display='block';},0); 
setTimeout(function(){document.getElementById('rainVideo').play();},0); 
setTimeout(function(){document.getElementById('videoRain').style.display='none';},15000);

编辑:

试过这个:

    <script>

        function runVideoRain(){
            rainVideo.addEventListener("loadeddata", runRain, false);
        }
    function runRain()
    {
        setTimeout(function()                {document.getElementById('videoRain').style.display='block';},0);
        setTimeout(function(){document.getElementById('rainVideo').play();},0);
        setTimeout(function()        {document.getElementById('videoRain').style.display='none';},15000);
    }, false);
    </script>

不行!!

Uncaught SyntaxError: Unexpected token , 未捕获的 ReferenceError:未定义 runVideoRain 加载数据

【问题讨论】:

  • Onloadeddata="myfunc()" - 不起作用!!! ... readyState === 4 - 不起作用! ... canplaythrough - 不起作用! ...这些只是加载视频几秒钟
  • 刚试过这个... oncanplaythrough ...它只是在加载视频标签时启动功能,而不是等待视频加载!
  • 仅供参考(问题后 3 年)“canplay”适用于当前的 Chrome。 Javascript 示例:var video = document.getElementById('myvideo');video.oncanplay = function(){...}; 例如,我测试了在开始播放视频时淡出覆盖视频的图像。

标签: html video load buffer


【解决方案1】:

您可以使用 onloadeddata 属性。

onloadeddata : 加载媒体数据时运行的脚本

<video id="rainVideo" width="100%" height="400px" preload="auto" onloadeddata="runMyFunction();"> 
  <source src="rain-video.mp4" type="video/mp4"> 
  Your browser does not support the video tag. 
</video> 
<script>
    function runMyFunction(){
        alert('The video is loaded');
    }
</script>

似乎 onloadeddata 属性在 chrome 上不起作用。但是通过 addEventListener 附加一个事件处理程序就可以了!

rainVideo.addEventListener("loadeddata", myRunFunction, false);
//with jQuery
$(rainVideo).on("loadeddata", myRunFunction);

【讨论】:

  • 如果我使用这个......它的作用是当你的视频数据加载了几秒钟或更短的时间(不加载完整的视频)......然后运行函数( ) ...所以这无济于事...
  • 似乎 onloadeddata 属性在 chrome 上不起作用。但是通过 addEventListener 附加一个事件处理程序就可以了!我编辑了我的答案。它现在可以在我的 Chrome 浏览器上运行。它也适用于 jQuery。
  • 如何添加... display='block'; ... 玩(); ...显示='无'; ……给它?
  • 没有 jQuery : document.getElementById('rainVideo').style.display="none"; document.getElementById('rainVideo').style.display="display";
  • 我说的是什么……我把这些放在哪里? ...如果您的意思是我应该放这些而不是... alert('视频已加载'); ...然后它就行不通了...试过这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-08
  • 2014-09-01
相关资源
最近更新 更多