播放另一个时,停止任何嵌入的YouTube iframe [英] Stop any embeded YouTube iframe when another is played
问题描述
我在我的页面中嵌入了大约10个YouTube iframe,所有这些都有自己的自定义播放/暂停按钮。我使用这个解决方案开始 http://jsfiddle.net/buuuue/dzroqgj0/ 但我我不确定如何调整这段代码来阻止正在播放的其他视频如果另一个开始...
I am embedding about 10 YouTube iframes in my page, all with their own custom play/pause buttons. I have used this solution to begin http://jsfiddle.net/buuuue/dzroqgj0/ but I'm not sure how to tweak this piece of the code to stop ANY other video that is playing if another one begins...
function stopVideo(player_id) {
if (player_id == "player1") {
player2.stopVideo();
} else if (player_id == "player2") {
player1.stopVideo();
}
}
例如,如果玩家1当前正在玩,并且玩家3玩,我希望玩家1停止玩。我也考虑过这个解决方案 https://stackoverflow.com/a/38405859/1798756 ,但我想我需要通过JS创建播放器,以便我也可以创建自定义播放/暂停按钮。
For example, if player 1 is currently playing, and player 3 is played, I'd want player 1 to stop playing. I've also looked into this solution https://stackoverflow.com/a/38405859/1798756, but I think I need to create the players via JS so that I can also create the custom play/pause buttons.
提前感谢您的帮助!
推荐答案
创建玩家后将它们推到阵列上。
After creating players push them onto an array.
var vids =[];
player1 = new YT.Player('player1', {
//stuff
});
vids.push(player1);
player2 = new YT.Player('player2', {
// stuff
});
vids.push(player2);
....
..
playerN = new YT.Player('playerN', {
// stuff
});
vids.push(playerN);
每个玩家对象的id都可以通过 a.id $访问c $ c> property(
a
本身就是一个对象, id
是一个属性(字符串))
Every player object's id can be accessed by its a.id
property(a
is itself an object , id
is a property(string) )
现在,当用户玩一个给定的玩家时,你必须暂停所有其他游戏,除了正在玩的玩家(你在中获取的id> stopVideo
)。因为你有id很容易
Now when user plays a given player you have to just pause all other except the one being played(id of which you get in stopVideo
).Since you have id it's easy
function stopVideo(player_id) {
for(var i=0;i<vids.length;i++){
if (player_id !=vids[i].a.id)
vids[i].stopVideo();
}
}
示例: - http://jsfiddle.net/4t9ah1La/
如果你没有创造玩家通过脚本,而只是嵌入iframe然后这个答案 vbence 就足够了,下面是 Jennie
If you are not creating player through scripts but rather just embedding iframes then this answer mentioned by vbence is enough , below is a demo of that with improvements suggested by Jennie
示例: - http://jsfiddle.net/ fyb7fyw1 /
各自原作者的所有学分
这篇关于播放另一个时,停止任何嵌入的YouTube iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!