Show the poster after pausing the video in HTML

假如想象 提交于 2019-12-11 06:36:02

问题


I have this code

<video id="primorvid" onclick="this.paused ? this.play() : this.pause();" poster="http://primor.m-sites.co.il/wp-content/uploads/2016/08/Untitled-18.jpg" width="1903" height="564">
<source src="http://primor.m-sites.co.il/wp-content/uploads/2016/08/Primor-V.4-HD.mp4" type="video/mp4" />
</video>

the video works, it shows a poster image before i click the video, and the play/pause is working fine-

how do i proceed from here if i want the poster to reappear again after i pause the video?

Thank you very much David


回答1:


Add this JS code:

const vid = document.querySelector('#primorvid');
let currentTime = 0;

vid.addEventListener('click', function(e) {
    if (vid.paused) {
        currentTime = vid.currentTime;
        vid.load();
    } else {
        vid.currentTime = currentTime;
    }
});

Explanation:

const vid = document.querySelector('#primorvid');
let currentTime = 0;

Just getting the video and storing it in vid, and declaring a currentTime where we will store the time at which the video was paused.

We add a click event listener to the video:

vid.addEventListener('click', function(e) { ... });

If the video's last state was playing (i.e. it is now paused), save the current time and reload it (this shows the poster and pauses the video):

if (vid.paused) {
    currentTime = vid.currentTime;
    vid.load();
} else {
    vid.currentTime = currentTime;
}

Else, set the current time to what it was before and play again.

Another solution which wouldn't imply buffering the video again would be to set an overlaying element with your poster and show it when the video is paused, alongside an event listener.




回答2:


You can try this.

var vrVid = document.getElementById("primorvid");
var pauseTime = vrVid.currentTime;
vrVid.load();
vrVid.currentTime = pauseTime;
vrVid.play();

I think it will solve your issue but i don't think it's best solution because it will reload the video and it will buffer.



来源:https://stackoverflow.com/questions/39204677/show-the-poster-after-pausing-the-video-in-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!