最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML5 Video - Start video at certain time and play for x amount of time - Stack Overflow

programmeradmin4浏览0评论

I'm trying to create buttons that start my local video at a certain point in time and have it play for a certain duration. I've got it to play at a certain point but not sure how to let it play for only certain duration.

Here's the code.

HTML:

<video id="myvideo" width="1000">
        <source src="video.mp4" type="video/mp4">
        Your browser does not support HTML5 video.
</video>
    <div>
        <p>
            <button id="playme">Play/Pause Video</button>
        </p>
        <p>
            <button id="jump">Jump to 4 seconds</button>
        </p>
        <p>
            <button id="jump2">Jump to 11 seconds</button>
        </p>
    </div>

Javascript:

var myvideo = document.getElementById('myvideo'),
playbutton = document.getElementById('playme'),
jumplink = document.getElementById('jump'),
jumplink2 = document.getElementById('jump2');

jumplink.addEventListener("click", function (event) {
    event.preventDefault();
    myvideo.play();
    myvideo.pause();
    myvideo.currentTime = 4;
    myvideo.play();
}, false);

jumplink2.addEventListener("click", function (event) {
    event.preventDefault();
    myvideo.play();
    myvideo.pause();
    myvideo.currentTime = 11;
    myvideo.play();
}, false);

playbutton.addEventListener("click", function () {
    if (myvideo.paused) {
        myvideo.play();
    } else {
        myvideo.pause();
    }
}, false);

I'm trying to create buttons that start my local video at a certain point in time and have it play for a certain duration. I've got it to play at a certain point but not sure how to let it play for only certain duration.

Here's the code.

HTML:

<video id="myvideo" width="1000">
        <source src="video.mp4" type="video/mp4">
        Your browser does not support HTML5 video.
</video>
    <div>
        <p>
            <button id="playme">Play/Pause Video</button>
        </p>
        <p>
            <button id="jump">Jump to 4 seconds</button>
        </p>
        <p>
            <button id="jump2">Jump to 11 seconds</button>
        </p>
    </div>

Javascript:

var myvideo = document.getElementById('myvideo'),
playbutton = document.getElementById('playme'),
jumplink = document.getElementById('jump'),
jumplink2 = document.getElementById('jump2');

jumplink.addEventListener("click", function (event) {
    event.preventDefault();
    myvideo.play();
    myvideo.pause();
    myvideo.currentTime = 4;
    myvideo.play();
}, false);

jumplink2.addEventListener("click", function (event) {
    event.preventDefault();
    myvideo.play();
    myvideo.pause();
    myvideo.currentTime = 11;
    myvideo.play();
}, false);

playbutton.addEventListener("click", function () {
    if (myvideo.paused) {
        myvideo.play();
    } else {
        myvideo.pause();
    }
}, false);
Share Improve this question edited Dec 4, 2017 at 23:23 Eric Nguyen asked Dec 4, 2017 at 22:33 Eric NguyenEric Nguyen 1,0464 gold badges17 silver badges44 bronze badges 1
  • You haven't mentioned which browser you're using, but if it's Chrome, you may be experiencing a bug. bugs.chromium/p/chromium/issues/detail?id=1018533 (Similar bugs have been filed and closed numerous times over the years, because it apparently only occurs with some videos.) – Michael Scheper Commented Apr 3, 2020 at 22:29
Add a ment  | 

1 Answer 1

Reset to default 13

In order to achieve this you need to poll the video's currentTime property as it's playing, perhaps with something like this general code...

var button = document.getElementById('play')
var video = document.getElementById('video');
var startTime = 10;
var endTime = 20;

button.addEventListener('click', playVideo, !1);

function playVideo(e) {

    function checkTime() {
        if (video.currentTime >= endTime) {
           video.pause();
        } else {
           /* call checkTime every 1/10th 
              second until endTime */
           setTimeout(checkTime, 100);
        }
    }

    video.currentTime = startTime;
    video.play();
    checkTime();
}

UPDATE

Let's look at how you might implement this for your app.

You have two 'jump' buttons and a play button. When any of these buttons are activated you could call a single function which sets the start time and end time according to the HTML id of the clicked button. Those values could them be passed into something like the function I've already outlined above.

To begin with, assign an event listener to each button...

var myvideo = document.getElementById('myvideo');

/* add the same event and 
   handler function to each 
   of the three buttons */
var buttons = ['playme','jump','jump2'];

buttons.forEach(function(bn) {
    document.getElementById(bn).addEventListener(
        'click', buttonEvents, !1
    );
});

Here each of you three HTML buttons will call a buttonEvents() function when clicked. That function might look something like this...

function buttonEvents(e) {
    /* get the id of the clicked button */
    var element_id = e.target.id;
    /* E.G. element_id = 'playme', 'jump', or 'jump2' */

    /* declare variables before setting them */
    var timeStart = 0;
    var timeEnd = 0;

    /* set start and end values depending 
       on which button was clicked */
    switch(element_id) {
        case 'playme':
            /* example values... */
            timeStart = 0;
            timeEnd = 100; 
            break;
        case 'jump':
            timeStart = 4;
            timeEnd = 12;
            break;
        case 'jump2':
            timeStart = 12;
            timeEnd = 24;
    }

    /* call 'playVideo()' */
    playVideo(timeStart, timeEnd);
}

Combining your 'playbutton' function with the general function outlined above would give us something like this: a function that receives a start and end time as it's arguments and plays the video if it's paused or jumps to a new start time if it's playing...

function playVideo(startTime, endTime) {

    function checkTime() {
        if (myvideo.currentTime >= endTime) {
           myvideo.pause();
        } else {
           /* call checkTime every 1/10th 
              second until endTime */
           setTimeout(checkTime, 100);
        }
    }

    /* stop if playing (otherwise ignored) */
    myvideo.pause();
    /* set video start time */
    myvideo.currentTime = startTime;
    /* play video */
    myvideo.play();
    /* check the current time and 
       pause IF/WHEN endTime is reached */
    checkTime();
}

Hope that helped.

发布评论

评论列表(0)

  1. 暂无评论