更改多个html5视频播放器的速度

时间:2017-05-24 12:30:01

标签: javascript html5 video html5-video playback

我有一个HTML5视频播放器的页面,我希望以0.5倍的速度播放所有这些播放器。我已经展示了仅针对单个视频播放器运行的js片段。

<script type="text/javascript">
  /* play video twice as fast */
  document.querySelector('video').defaultPlaybackRate = 1.0;
  document.querySelector('video').play();

  /* now play three times as fast just for the heck of it */
  document.querySelector('video').playbackRate = 3.0;
</script>

仅适用于第一个视频。每个视频都需要它。下面是html的一部分片段。

<ul class="regular slider">
    <li class="videodiv" style="background-image: url('boxing.jpg')">
      <video preload="yes" loop>
        <source src="12950321.mp4" type="video/mp4"/>
      </video>
      <div class="slantedcaption">Boxing</div>
      <a href="detail.html">
        <div class="caption">
          <h2>Boxing</h2>
          <div class="category">Passion</div>
        </div>
      </a>
    </li>
    <li class="videodiv" style="background-image: url('coding.jpg')">
      <video preload="yes" loop>
        <source src="14019065.mp4" type="video/mp4"/>
      </video>
      <div class="slantedcaption">Boxing</div>
      <a href="detail.html">
        <div class="caption">
          <h2>Boxing</h2>
          <div class="category">Passion</div>
        </div>
      </a>
    </li>
    <li class="videodiv" style="background-image: url('dance.jpg')">
      <video preload="yes" loop>
        <source src="12950321.mp4" type="video/mp4"/>
      </video>
      <div class="slantedcaption">Boxing</div>
      <a href="detail.html">
        <div class="caption">
          <h2>Boxing</h2>
          <div class="category">Passion</div>
        </div>
      </a>
    </li>
</ul>

2 个答案:

答案 0 :(得分:1)

将Javscript文件更改为:

<script type="text/javascript">
  /* play video twice as fast */
  document.querySelector('video').defaultPlaybackRate = 1.0;
  document.querySelector('video').play();

  /* now play three times as fast just for the heck of it */
  var videos =document.querySelectorAll('video');
  for (var i=0;i<videos.length;i++)
  {
    videos[i].playbackRate = 3.0;
  }
</script>

错误是当您使用querySelector时,它返回一个视频对象。 作为querySelectorAll,返回对象数组(作为所有视频标记)。因此,您必须迭代数组并提高视频的速度。

希望它可能有所帮助。 快乐编码!!

答案 1 :(得分:0)

这是因为querySelector只返回第一个元素。

如果您想要访问多个视频标签,可以使用querySelectorAll。

querySelectorAll将返回所有视频的数组,然后您需要循环它们以应用defaultPlaybackRate。

示例:

#include "xxx.h"

int main()
{
    xxx(int yyy));
    return 0;
}
相关问题