-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPlaying only one embedded youtube video out of multiple videos
80 lines (65 loc) · 1.92 KB
/
Playing only one embedded youtube video out of multiple videos
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<!DOCTYPE html>
<html>
<head>
<title>youtube</title>
</head>
<body>
<div id="p1" ></div>
<div id="player2" ></div>
<div id="player3" ></div>
<script type="text/javascript"></script>
<script>
// This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player1,player2,player3;
var id;
function onYouTubeIframeAPIReady() {
player1 = new YT.Player('p1', {
height: '400',
width: '400',
videoId: '8jm_56vtUhM',
playerVars: { 'showinfo': 0 , 'autohide':1 , 'color':'white' , 'rel':0},
events: {
'onStateChange': onPlayerStateChange
//'onReady' : function_name
}
});
player2 = new YT.Player('player2', {
height: '400',
width: '400',
videoId: 'oRYshQCOHOs',
playerVars: { 'showinfo': 0 , 'autohide':1 , 'color':'white' , 'rel':0},
events: {
'onStateChange': onPlayerStateChange
}
});
player3 = new YT.Player('player3', {
height: '400',
width: '400',
videoId: 'zaqfuMbd06I',
playerVars: { 'showinfo': 0 , 'autohide':1 , 'color':'white' , 'rel':0},
events: {
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && id !=event.target.a.id) {
id = event.target.a.id;
pauseAllVideos();
event.target.playVideo();
}
}
function pauseAllVideos() {
var iframes = document.querySelectorAll('iframe');
Array.prototype.forEach.call(iframes, iframe => {
iframe.contentWindow.postMessage(JSON.stringify({ event: 'command',
func: 'pauseVideo' }), '*'); // stopVideo
});
}
</script>
</body>
</html>