视频在结束后重新从头开始播放,而不是停止/暂停。

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

video on ended plays again from start instead of stop/pause

问题

<video class='vsingle' id='vsingle' src='ph/video.mp4' poster='ph/vposter.jpg' controls></video>

JavaScript:

$('#vsingle').on('ended', function(){
    $(this)[0].load();
});

这段代码会导致视频重新从头开始播放。
我只想重新加载它,这样起始海报会可见,而不播放。
怎么做到这一点?

我还尝试了下面这个,但没有成功。

$(this)[0].load();
$(this)[0].pause();
英文:
&lt;video class=&#39;vsingle&#39; id=&#39;vsingle&#39; src=&#39;ph/video.mp4&#39; poster=&#39;ph/vposter.jpg&#39; controls&gt;&lt;/video&gt;

js

$(&#39;#vsingle&#39;).on(&#39;ended&#39;,function(){
	$(this)[0].load();
});

This code results in playing video again from start
I want just to reload it, so starting poster to be visible and not to play
How can I to this ?

I also tried this, vithout success

$(this)[0].load();
$(this)[0].pause();

答案1

得分: 2

根据MDN文档 @https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/load:

HTMLMediaElement方法load()将媒体元素重置为其初始状态,并开始选择媒体源并加载媒体,以准备开始播放。

尝试使用 currentTime,像这样:

$(this)[0].pause();
$(this)[0].currentTime = 0;

编辑:

检查是否有其他事件侦听器(导致播放开始)吗?

我刚刚尝试在这里重现您的代码,并且简单地添加:

video.addEventListener('ended', () => {   video.load(); })

得到了预期的结果,视频没有自动播放。

英文:

According to MDN Docs @<https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/load>:
> The HTMLMediaElement method load() resets the media element to its initial state and begins the process of selecting a media source and loading the media in preparation for playback to begin at the beginning.

Try using currentTime, like this:

$(this)[0].pause();
$(this)[0].currentTime = 0;

Edit:<br>

Check if maybe there other event listeners (causing play to begin)?

I just tried to reproduce your code here and simply adding:

video.addEventListener(&#39;ended&#39;, () =&gt; {   video.load(); })

gave the expected result, and the video did not start auto-playing.

huangapple
  • 本文由 发表于 2023年3月3日 23:50:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629223.html
匿名

发表评论

匿名网友

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

确定