【问题标题】:Stop Other Youtube Videos From Playing When New Video Starts In Modal当新视频以模态开始时停止播放其他 Youtube 视频
【发布时间】:2021-02-07 02:00:40
【问题描述】:
var tag = document.createElement('script');
                            tag.src = "https://www.youtube.com/iframe_api";
                            var firstScriptTag = document.getElementsByTagName('script')[0];
                            firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

                            function onYouTubeIframeAPIReady() {
                                var $ = jQuery;
                                var players = [];
                                $('iframe').filter(function() {
                                    return this.src.indexOf('https://www.youtube.com/') == 0
                                }).each(function(k, v) {
                                    if (!this.id) {
                                        this.id = 'embeddedvideoiframe' + k
                                    }
                                    players.push(new YT.Player(this.id, {
                                        events: {
                                            'onStateChange': function(event) {
                                                if (event.data == YT.PlayerState.PLAYING) {
                                                    $.each(players, function(k, v) {
                                                        if (this.getIframe().id != event.target.getIframe().id) {
                                                            this.pauseVideo();
                                                        }
                                                    });
                                                }
                                            }
                                        }
                                    }))
                                });
                            }
                        });

它们上面的代码适用于嵌入式播放器,但是如果您在所有 iframe 嵌入式视频工作之前点击视频播放,它通常会停止工作,除非您刷新页面。有没有办法在所有 Youtube Embeddes 加载后调用此代码?

【问题讨论】:

  • 什么浏览器和播放器?
  • 最新版本的 Chrome。嵌入式播放器

标签: javascript jquery twitter-bootstrap youtube-iframe-api


【解决方案1】:

您可以使用onReady 事件和 Promises 来确定是否加载了所有视频。可能是这样的:

function onYouTubeIframeAPIReady() {
    var $ = jQuery;
    var players = [];
    var loaded = [];
    var allLoaded = false;
    $('iframe').filter(function() {
        return this.src.indexOf('https://www.youtube.com/') == 0
    }).each(function(k, v) {
        var that
        if (!this.id) {
            this.id = 'embeddedvideoiframe' + k
        }
        that = this.id;
        loaded.push(new Promise(function (resolve, reject) {
            players.push(new YT.Player(that.id, {
                events: {
                    'onReady': resolve,
                    'onStateChange': function(event) {
                        if (!allLoaded) return;
                        if (event.data == YT.PlayerState.PLAYING) {
                            $.each(players, function(k, v) {
                                if (that.getIframe().id != event.target.getIframe().id) {
                                    that.pauseVideo();
                                 }
                            });
                        }
                    }
                }
           }))
       }));
    });
    Promise.all(loaded).then(function () {
       allLoaded = true;
    });
}

【讨论】:

  • 这是完整的代码吗?我收到错误 www-widgetapi.js:607 Uncaught (in promise) 错误:YouTube 播放器元素 ID 需要
  • 哎呀,没有注意到有一些this 表达式...更新了答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-27
  • 1970-01-01
  • 1970-01-01
  • 2013-01-31
  • 2018-12-19
  • 1970-01-01
  • 2018-08-23
相关资源
最近更新 更多