Запретить несколько просмотров YouTube нельзя запускать в приложении angularjs - PullRequest
0 голосов
/ 19 июня 2020

У меня есть приложение Angularjs, в котором я просматриваю список видео Youtube, используя videogular node module.

Проблема в том, что пользователь может запускать одновременно несколько видео YouTube. Что является нарушением политики Youtube? Как мы можем ограничить использование, мы не должны воспроизводить одно представление за раз на одной странице.

1 Ответ

1 голос
/ 03 июля 2020

Вот как я получил

(function () {

'use strict';

angular
    .module('app')
    .directive('youtubePost', youtubeEmbedDir)

function youtubeEmbedDir($rootScope, $http, config, logger) {
    'ng-inject';
    return {
        restrict: 'A',
        scope: {
            item: '<item'
        },
        link: function (scope, element, attrs) {
            var thisPlayer;
            scope.isLoading = true;
            var videoId = null;

            $rootScope.$on('stopOtherYouttubePlayer', function (traget, id) {
                if (videoId && (id != videoId) && thisPlayer) {
                    try {
                        thisPlayer.stopVideo()
                    } catch (error) {
                        logger.error(error);
                    }
                }
            });

            /**
             *  YT is a windows object from youtube embed js
             */
            function createYoutubeWidget() {

                if (YT && YT.Player && new RegExp(/^(http(s)?:\/\/(www.)?youtube.com\/)/g).test(attrs.href)) {
                    videoId = attrs.href.match(/v=(\w|-)*/g)[0];
                    var el = angular.element(element).find('.youtube-player')[0];
                    videoId = videoId.replace('v=', '').replace('&', '');
                    thisPlayer = new YT.Player(el, {
                        width: '100%',
                        // related videos - off, watch later/ share - off, Annotations - off
                        playerVars: { rel: 0, showinfo: 0, iv_load_policy: 3 },
                        events: {
                            'onReady': onPlayerReady,
                            'onStateChange': onStateChange,
                            'onError': onError
                        }
                    });

                    function onStateChange(event) {
                        if (event.data == YT.PlayerState.PLAYING) {
                            $rootScope.$emit('stopOtherYouttubePlayer', videoId);
                        }
                    }

                    function onPlayerReady() {
                        thisPlayer.cueVideoById(videoId);
                    }

                    function onError(err) {
                        scope.$emit('failedToLoadSocialData', true);
                    }
                }
            }

            $http({
                method: 'GET',
                url: config.apiUrl + '/V1.0/proxy/youtube?url=' + attrs.href
            }).then(function (instagramEmbedJson) {
                if (instagramEmbedJson.status == 200) {
                    scope.isLoading = false;
                    createYoutubeWidget();
                }
                else {
                    scope.$emit('failedToLoadSocialData', true);
                }
            }, function () {
                scope.$emit('failedToLoadSocialData', true);
            });
        },
        template: '<div loading-pulse="isLoading"></div><div class="youtube-player"></div></div>'
    }
}

}) ();

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...