本文介绍了如何在选择列表中更改选项时停止所有YouTube视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在隐藏的div中有几个YouTube播放器,它们根据WP模板中的选择显示/隐藏.我遇到的问题是,如果您正在播放视频,请使用隐藏后视频继续播放的选择列表来更改频道.加载新内容时,您仍然会听到原始视频中的音频.我在全局var'players'->'players.pauseVideo不是函数'时出错.在代码中,我创建了一个视频播放器数组,并尝试创建一个全局停止功能,无论我使用什么var,players,players_list等,都遇到相同的错误.

I have several YouTube players in hidden div's that show / hide based on selection in a WP template. The issue I am running into is that if you are playing a video, then change the channel by using the select list the video keeps playing after it is hidden. You still hear audio from the original video while the new content loads. I get an error with the global var 'players' -> 'players.pauseVideo is not a function'. In the code I create an array of video players and tried to create a global stop function, whatever var I use, players, players_list etc I get the same error.

var players = new Array();
var players_list = ["ytpl-player1", "ytpl-player2", "ytpl-player3"];

function stopGlobal() {
    players_list.pauseVideo();
}

推荐答案

有几个错误.只是看看其中之一.

There are several mistakes. Just look at one of them.

var players = new Array();
var players_list = ["ytpl-player1", "ytpl-player2", "ytpl-player3"];

for (item in players_list) {
    console.log('Trying to set "'+ players_list[item] + '" as index of array.');
    players[players_list[item]] = 'whatever';
}

console.log('Result:');
console.log(players);

修复:

var players = {
  "ytpl-player1": null,
  "ytpl-player2": null,
  "ytpl-player3": null,
};

Object.keys(players).forEach(function(key) {
  players[key] = 'new YT.Player(key, ...';
});

console.log('Result:');
console.log(players);

然后,您可以像上面一样再次进行迭代,并在每个值上调用pauseVideo().

Then you can iterate again like above and call pauseVideo() on each value.

这篇关于如何在选择列表中更改选项时停止所有YouTube视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 22:16