示例#1
0
 init().then(() => {
     if (canAutoplay() && isDesktop()) {
         addCancelListener();
         triggerAutoplay(
             event.target.getCurrentTime.bind(event.target),
             duration
         );
     }
 });
 init().then(() => {
     expect(canAutoplay()).toBeFalsy();
     done();
 });
 it('should trigger autoplay when there is a next video', done => {
     expect(canAutoplay()).toBeTruthy();
     done();
 });
示例#4
0
export const initHostedYoutube = (el: HTMLElement): void => {
    const atomId: ?string = el.getAttribute('data-media-id');
    const duration: ?number = Number(el.getAttribute('data-duration'));

    if (!atomId || !duration) {
        return;
    }

    const youtubeTimer = document.getElementsByClassName(
        'js-youtube-current-time'
    )[0];

    let playTimer;

    initYoutubeEvents(atomId);
    initYoutubePlayer(
        el,
        {
            onPlayerStateChange(event: Object) {
                const player = event.target;

                // show end slate when movie finishes
                if (event.data === window.YT.PlayerState.ENDED) {
                    trackYoutubeEvent('end', atomId);
                    youtubeTimer.textContent = '0:00';
                    if (canAutoplay()) {
                        // on mobile show the next video link in the end of the currently watching video
                        if (!isDesktop()) {
                            triggerEndSlate();
                        }
                    }
                } else {
                    // update current time
                    const currentTime = Math.floor(player.getCurrentTime());
                    const seconds = currentTime % 60;
                    const minutes = (currentTime - seconds) / 60;
                    youtubeTimer.textContent =
                        minutes + (seconds < 10 ? ':0' : ':') + seconds;
                }

                if (event.data === window.YT.PlayerState.PLAYING) {
                    trackYoutubeEvent('play', atomId);
                    const playerTotalTime = player.getDuration();
                    playTimer = setInterval(() => {
                        sendPercentageCompleteEvents(
                            atomId,
                            player,
                            playerTotalTime
                        );
                    }, 1000);
                } else {
                    window.clearInterval(playTimer);
                }
            },
            onPlayerReady(event: Object) {
                init().then(() => {
                    if (canAutoplay() && isDesktop()) {
                        addCancelListener();
                        triggerAutoplay(
                            event.target.getCurrentTime.bind(event.target),
                            duration
                        );
                    }
                });
            },
        },
        el.dataset.assetId
    );
};