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

javascript - How to make events fire when an HTML video reaches a certain time mark? - Stack Overflow

programmeradmin1浏览0评论

I'm wondering whether this is even possible, and if so, how it would be done. So I have an HTML5 video on my page

<video id="ssvid">
      <source src="assets/Cisco_SmartStack_04012016_NoText_2.mp4" type="video/mp4" />
</video>

and let's say I want functions to be invoked at the 2-, 13- and 15-second marks of the video playing. I'm trying to create an object like

$(function(){
    window.VidHandler = (function(divid){
        this.divid = divid;
        this.PauseFunctions = [
            {
                SecondMark: 2,
                OnSlideTo: function () { },
                OnSlideAway: function () { }
            },
            {
                SecondMark: 13,
                OnSlideTo: function () { },
                OnSlideAway: function () { }
            },
            {
                SecondMark: 15,
                OnSlideTo: function () { },
                OnSlideAway: function () { }
            }
        ];
    })("ssvid");
});

but I don't know if JavaScript has an "ontimemark(2)" type of event listener that I can set up. Will I have to create a setInterval to do this?

I'm wondering whether this is even possible, and if so, how it would be done. So I have an HTML5 video on my page

<video id="ssvid">
      <source src="assets/Cisco_SmartStack_04012016_NoText_2.mp4" type="video/mp4" />
</video>

and let's say I want functions to be invoked at the 2-, 13- and 15-second marks of the video playing. I'm trying to create an object like

$(function(){
    window.VidHandler = (function(divid){
        this.divid = divid;
        this.PauseFunctions = [
            {
                SecondMark: 2,
                OnSlideTo: function () { },
                OnSlideAway: function () { }
            },
            {
                SecondMark: 13,
                OnSlideTo: function () { },
                OnSlideAway: function () { }
            },
            {
                SecondMark: 15,
                OnSlideTo: function () { },
                OnSlideAway: function () { }
            }
        ];
    })("ssvid");
});

but I don't know if JavaScript has an "ontimemark(2)" type of event listener that I can set up. Will I have to create a setInterval to do this?

Share Improve this question asked Apr 6, 2016 at 0:07 Subpar Web DevSubpar Web Dev 3,2807 gold badges23 silver badges42 bronze badges 1
  • What's dvid supposed to be? – zer00ne Commented Apr 6, 2016 at 0:54
Add a ment  | 

3 Answers 3

Reset to default 5

How to make events fire when an HTML video reaches a certain time mark?

Use currentTime and timeupdate see the demo utilize both.

but I don't know if JavaScript has an "ontimemark(2)" type of event listener that I can set up.

timeupdate

Will I have to create a setInterval to do this?

No, see Snippet below.

  • Every timeupdate event fires the console.log

  • vidHandler() uses a switch to fire on second marks 2, 13, and 15.

  • You can replace the console.logs with the slide methods.

  • By the looks of it, currentTime looks more like 1sec = 3.5sec? Or the reported duration for the video element is inaccurate.

Snippet

  var ssvid = document.getElementById('ssvid');
  var tick = ssvid.currentTime;

  ssvid.addEventListener('timeupdate', function(e) {
    console.log('currentTime: ' + tick);
    tick++
    vidHandler(tick);
  }, false);


  function vidHandler(time) {
    switch (time) {
      case 2:
        console.log('2 second mark');
        break;
      case 13:
        console.log('13 second mark');
        break;
      case 15:
        console.log('15 second mark');
        break;

      default:
        return false;
    }
  }
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

<video id="ssvid" controls width="300">
  <source src="http://glpjt.s3.amazonaws./so/av/vs34s3.mp4" type="video/mp4" />
</video>

Maybe you can try using the video currentTime property inside a timer. There is also supposed to be a progress event. It is outlined in this document: https://www.w3/2010/05/video/mediaevents.html

Something like this may work:

    // maybe you can select this by id in jQuery rather than DOM
var video = document.getElementsByTagName('video')[0],
    lastPf // reference for slide away;

video.onprogress = function() {
    PauseFunctions.forEach(function(pf) {
        if (video.currentTime > pf.SecondMark) {
            if (lastPf !== undefined) {
                // slide away the previous thing
                lastPf.OnSlideAway();
            }

            // slide to the new one
            pf.OnSlideTo();
            lastPf = pf;
        }
    });
};

Do you think that will work?

The answer by @zer00ne is nearly there. timeupdate doesn't update every second, it's dependent on the user agent. So, we shouldn't be self incrementing a tick, we should just pass the latest currenttime which represents the current playback time in seconds.

According to Mozilla:

  • timeupdate frequency:

The timeupdate event is fired when the time indicated by the currentTime attribute has been updated.

The event frequency is dependent on the system load, but will be thrown between about 4Hz and 66Hz (assuming the event handlers don't take longer than 250ms to run).

  • currenttime:

A double-precision floating-point value indicating the current playback time in seconds.

If the media is not yet playing, the value of currentTime indicates the time position within the media at which playback will begin once the play() method is called.

References:

https://developer.mozilla/en-US/docs/Web/API/HTMLMediaElement/timeupdate_event

https://developer.mozilla/en-US/docs/Web/API/HTMLMediaElement/currentTime

发布评论

评论列表(0)

  1. 暂无评论