Home > Blockchain >  Automatically Pause Embedded iFrame Vimeo Video at Specific Time?
Automatically Pause Embedded iFrame Vimeo Video at Specific Time?

Time:05-24

I'm using a Vimeo video-embed (iFrame embed code from their website) on my website, and I need the video to automatically stop at a specific timestamp (I'll use 6 seconds here) whenever users of my site play the video. The content after the timestamp is unnecessary.

But unlike YouTube, Vimeo doesn't seem to have an easy way to set end times for any video you embed from them. Unfortunately, I do not own the video so I can't edit the footage directly, so I believe a JavaScript solution is the best option.

Here's the aforementioned iFrame embed HTML from Vimeo that I use on my site:

<iframe id="vidz" src="https://player.vimeo.com/video/401649410?h=11d74aa27c&amp;portrait=0" width="450" height="253" frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen=""></iframe>

Upon trying to select elements within the iFrame (like the video itself), my JS selectors don't seem to be working at all, perhaps because the video is from a separate source not hosted on my site?

Here's code I've been working with, but it doesn't appear to be interacting with the iFrame, as I believe I would need to add this eventListener to a video, directly. But I can't select the embeded video via JS either, it seems. So I'm not quite sure how to handle this:

var vid = document.querySelector("#vidz");
vid.addEventListener("timeupdate", function(){
if(t >= 6000) 
  {
vid.pause();
  }
});

I can also provide the HTML to any elements within the iFrame, but again, I'm not sure how I'd be able to interact with those elements.

Any ideas? Any and all help would be deeply appreciated. Cheers.

CodePudding user response:

use Vimeo js file and below is a script you want video will pause after 6 minutes (specific time) (360 means 6 minutes)

    <script src="https://player.vimeo.com/api/player.js"></script>

    var iframe = document.querySelector('iframe');
    var player = new Vimeo.Player(iframe);
    function foo() {
        
        player.getCurrentTime().then(function(time) {
            console.log('time:', time);
            if(time >= 360){  
                player.pause()
            }
        });
        setTimeout(foo, 1000);
    }
    player.on('play', function() {
        foo();
    });
</script>
  • Related