【问题标题】:Multiple YouTube players in iframe, select box to change channel - stop playing videoiframe 中的多个 YouTube 播放器,选择框以更改频道 - 停止播放视频
【发布时间】:2018-11-28 17:41:50
【问题描述】:

我已经坚持了一周。创建一个新问题比update 上一个问题更干净。我有一个带有选择框的简单 WP 模板:

<select name="channelChooser" id="channelChooser">
    <option value="" selected>Please Select Platform</option>
    <option value="cqgd_ytpl">CQG Desktop</option>
    <option value="sc_ytpl">Sierra Chart</option>
    <option value="cqgq_ytpl">CQG Q Trader</option>
</select>

模板内有一个可见的 div 和 3 个隐藏的 div。我使用选择框切换可见 div,以便您可以显示/隐藏 YT 频道。

jQuery(document).ready(function() {
    $('#channelChooser').change(function () {
        var playerName = jQuery(this).closest('.embed-container').find('iframe').attr('id');
        if($(this).val() == 'cqgd_ytpl') {
           $('.ypt_wrapper').not('#cqgd_ytpl').hide();
           $('#cqgd_ytpl').show();
        } else if($(this).val() == 'sc_ytpl') {
            $('.ypt_wrapper').not('#sc_ytpl').hide();
            $('#sc_ytpl').show();
        } else if($(this).val() == 'cqgq_ytpl') {
            $('.ypt_wrapper').not('#cqgq_ytpl').hide();
            $('#cqgq_ytpl').show();
        }
    });
});

很遗憾,如果您开始播放视频,请更改频道(显示/隐藏 div),视频会继续播放,但不再可见。我曾尝试使用“pauseVideo”、“stopVideo”等,但出现同样的错误“...不是函数”

如何制作全局停止功能,以便每次使用选择框时都停止当前正在播放的视频。 $(.ytpl-playing).stopVideo();获取 'ytpl-playing' 未定义。

【问题讨论】:

  • 嗨,不要使用ids,而是获取播放器参考以停止视频,例如player1.stopVideo();
  • 我将如何设置这些,小提琴中的代码显示了 id 构造

标签: javascript jquery wordpress youtube


【解决方案1】:

你可以在这里查看工作代码JSBin

我刚刚添加了完成您的任务所需的代码。随意根据您的需要进行更改。

jQuery(document).ready(function() {
        $('#channelChooser').change(function() {
            // $('#ytpl-player1').data("data-pl", this.value);
            var playerName = jQuery(this).closest('.embed-container').find('iframe').attr('id');
            if ($(this).val() == 'cqgd_ytpl') {
                $('.ypt_wrapper').not('#cqgd_ytpl').hide();
                $('#cqgd_ytpl').show();
                players["ytpl-player2"].stopVideo();
                players["ytpl-player3"].stopVideo();
            } else if ($(this).val() == 'sc_ytpl') {
                $('.ypt_wrapper').not('#sc_ytpl').hide();
                $('#sc_ytpl').show();
                players["ytpl-player1"].stopVideo();
                players["ytpl-player3"].stopVideo();
            } else if ($(this).val() == 'cqgq_ytpl') {
                $('.ypt_wrapper').not('#cqgq_ytpl').hide();
                $('#cqgq_ytpl').show();
                players["ytpl-player1"].stopVideo();
                players["ytpl-player2"].stopVideo();
            }
        });
      });

【讨论】:

  • 太棒了!谢谢!所以我理解正确,var playerName 获取 id,那么“玩家”数组在哪里挂钩?
  • 所以基本上,因为我创建了一个数组 - 我必须以数组表示法访问它!伙计,我很笨!
  • 当您使用YT.Player(...) 创建播放器时,您已将该引用存储在数组中。所以,如果你想执行任何 youtube 定义的操作,你必须使用特定的播放器引用来实现它。
猜你喜欢
  • 2014-01-22
  • 2015-12-12
  • 2014-05-01
  • 2011-04-05
  • 2018-08-23
  • 2013-01-25
  • 1970-01-01
  • 2011-11-23
  • 1970-01-01
相关资源
最近更新 更多