Skip to content

play() is pausing #90

Description

@jamesvclements
CleanShot.2022-04-13.at.12.26.52.mp4

Trying to add autoplay functionality to videos a Webflow site. The videos should play when in view, pause when out of view.

I have all the intersection observer functionality working, but I can't seem to play the videos using playerjs. Whenever I call play(), the pause event is fired. Could this have to do with the Vimeo sources themselves?

/* utility function to fetch embed URL to uniquely identify players */
const getEmbed = (target) => target.querySelector('iframe.embedly-embed');
const getEmbedSrc = (target) => getEmbed(target).src;

$(document).ready(function () {
    console.log('video-autoplay.js');

    const players = {};
    const options = {
        root: null,
    };
    const autoplayObserver = new IntersectionObserver((entries) => {
        entries.forEach((entry) => {
            const id = getEmbedSrc(entry.target);
            console.log(id);
            const player = players[id];
            player.on('ready', () => {
                if (entry.isIntersecting) {
                    console.log(`intersecting: ${entry.target.id}`);
                    player.play();
                } else {
                    console.log(`not intersecting: ${entry.target.id}`);
                    player.pause();
                }
            });
        });
    }, options);

    const autoplayTargets = document.querySelectorAll('[data-autoplay=true]');

    autoplayTargets.forEach((target) => {
        const id = getEmbedSrc(target);
        const player = new playerjs.Player(getEmbed(target));
        player.on('play', () => {
            console.log(`playing: ${id}`);
        });
        player.on('pause', () => {
            console.log(`paused: ${id}`);
        });
        players[id] = player;

        autoplayObserver.observe(target);
    });

    console.log('players');
    console.log(players);
    globalThis.players = players;
});

Crossing my fingers this repo is still maintained 🤞 @screeley

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions