视频播放器中的交互按钮

huangapple go评论65阅读模式
英文:

Interactive buttons in video player

问题

我正在尝试创建一个简单的index.php,在其中我想显示一个视频,但当视频达到20秒时,询问用户"你在听吗"或类似的内容。在阅读了很多教程之后,我成功在第20秒显示了按钮,但当我点击按钮时,视频不会在接下来的20秒内恢复,只有几毫秒而已。完整的想法是每20秒询问一次"你在听吗"。以下是我的代码:

<body>
  <div class="video-container">
    <video id="myVideo" controls>
      <source src="video.mp4" type="video/mp4">
    </video>
    <button class="interactive-button" data-time="20">你在听吗?</button>
  </div>

  <script>
    document.addEventListener("DOMContentLoaded", function () {
      const video = document.getElementById("myVideo");
      const buttons = document.querySelectorAll(".interactive-button");

      buttons.forEach((button) => {
        const time = parseInt(button.dataset.time);
        button.addEventListener("click", function () {
          if (video.currentTime >= time) {
            video.currentTime = time; 
            video.play(); 
          }
        });
      });

      video.addEventListener("timeupdate", function () {
        const currentTime = video.currentTime;
        buttons.forEach((button) => {
          const time = parseInt(button.dataset.time);
          if (currentTime >= time) {
            button.style.display = "block";
            video.pause();
          } else {
            button.style.display = "none";
          }
        });
      });
    });
  </script>
</body>

这是你提供的代码的翻译部分。

英文:

I am trying to make a simple index.php where I want to display a video but when the video reaches 20 seconds ask the user "Are you listening" or something like this.
After reading so many tutorials I achieved displaying the button on the 20th second but when I click on the button the video is not resuming for the next 20 seconds but for a few milliseconds.
The complete idea is to ask every 20 seconds "Are you listening"
Here is my code now:

&lt;body&gt;
  &lt;div class=&quot;video-container&quot;&gt;
    &lt;video id=&quot;myVideo&quot; controls&gt;
      &lt;source src=&quot;video.mp4&quot; type=&quot;video/mp4&quot;&gt;
    &lt;/video&gt;
    &lt;button class=&quot;interactive-button&quot; data-time=&quot;20&quot;&gt;Are you listening carefully?&lt;/button&gt;
    
  &lt;/div&gt;

  &lt;script&gt;
    document.addEventListener(&quot;DOMContentLoaded&quot;, function () {
      const video = document.getElementById(&quot;myVideo&quot;);
      const buttons = document.querySelectorAll(&quot;.interactive-button&quot;);

      buttons.forEach((button) =&gt; {
        const time = parseInt(button.dataset.time);
        button.addEventListener(&quot;click&quot;, function () {
          if (video.currentTime &gt;= time) {
            video.currentTime = time; 
            video.play(); 
          }
        });
      });

      video.addEventListener(&quot;timeupdate&quot;, function () {
        const currentTime = video.currentTime;
        buttons.forEach((button) =&gt; {
          const time = parseInt(button.dataset.time);
          if (currentTime &gt;= time) {
            button.style.display = &quot;block&quot;;
            video.pause();
          } else {
            button.style.display = &quot;none&quot;;
          }
        });
      });
    });
  &lt;/script&gt;
&lt;/body&gt;

答案1

得分: 1

timeupdate 方法中,始终检查视频时间是否大于20秒。因此,当超过20秒时,视频不会重新开始播放,因为它已经超过了20秒标记。

每次都应该更新 button.dataset.time 的值。

buttons.forEach((button) => {
    button.addEventListener("click", function () {
        const time = parseInt(button.dataset.time);
        if (video.currentTime >= time) {
            video.currentTime = time;
            button.dataset.time = time + 20;
            video.play();
        }
    });
});
英文:

In the timeupdate method, it is always checked whether the video time is greater than 20 seconds. Therefore, when it exceeds the 20-second mark, the video doesn't restart because it has already passed the 20-second mark.

You should update the button.dataset.time value each time.

buttons.forEach((button) =&gt; {
    button.addEventListener(&quot;click&quot;, function () {
        const time = parseInt(button.dataset.time);
        if (video.currentTime &gt;= time) {
            video.currentTime = time;
            button.dataset.time = time + 20;
            video.play();
        }
    });
});

huangapple
  • 本文由 发表于 2023年7月18日 14:38:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定