javascript - 防止多个YouTube View 不能在angularjs应用程序中运行

标签 javascript angularjs angular youtube

我有一个Angularjs应用程序,正在其中使用videogular node module列出Youtube视频。

问题是用户可以同时运行多个youtube视频。哪些违反YouTube政策?
我们如何限制使用的方式一定不能同时在同一页面上播放一个 View 。

最佳答案

这就是我实现

(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>'
    }
}
的方式
})();

关于javascript - 防止多个YouTube View 不能在angularjs应用程序中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62463671/

相关文章:

javascript - 按状态过滤 ng-repeat

javascript - Vue JS显示所选数组的内容

javascript - underscorejs中_.each(list)为空时如何显示消息?

angularjs - web api身份验证后将 token 存储在浏览器本地存储中是否安全

javascript - Angular 示意图条件属性/提示

angular 9 依赖注入(inject)错误。 "该类不能通过依赖注入(inject)创建,因为它没有 Angular 装饰器

angular - 类型 'next' 上不存在属性 'Observable<{}>'

javascript - after() 插入元素,然后取回

javascript - 嵌套 dom-repeat 中数组映射的更改通知

angularjs - 前端和后端使用MVC框架