Second JavaScript function cancelling first - youtube-api

I am using the youtube video api and ahve 2 seperate lightboxes that open the same video. One for mobile and one for desktop.
The problem I am having is that once I place seperate code for the mobile version the desktop version stops working. What I mean is that the lightbox still opens but the video does not dsiplay.
Desktop (video-home-popup) and mobile (video-homepopup-mobile) code:
<div id="video-home-popup" style="display:none; padding:0px;">
<div id="player"></div>
<script type="text/javascript">
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
playerVars: {
modestbranding: true,
theme: 'light',
rel: 0,
wmode: "opaque",
autoplay: '0'
},
height: '480',
width: '640',
videoId: '4IXAxJ8oPFg',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
/// event.target.playVideo();
}
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING) {
_gaq.push(['_trackEvent', 'Videos', 'Play',
player.getVideoUrl()]);
}
if (event.data == YT.PlayerState.PAUSED) {
_gaq.push(['_trackEvent', 'Videos', 'Paused',
player.getVideoUrl()]);
}
if (event.data == YT.PlayerState.ENDED) {
_gaq.push(['_trackEvent', 'Videos', 'Watch to End',
player.getVideoUrl()]);
}
}
// ]]>
</script>
</div>
<div id="video-home-popup-mobile" style="display:none; padding:0px;">
<div id="player1"></div>
<script type="text/javascript">
var player1;
function onYouTubePlayerAPIReady() {
player1 = new YT.Player('player1', {
player1Vars: {
modestbranding: true,
theme: 'light',
rel: 0,
wmode: "opaque",
autoplay: '0'
},
height: 'auto',
width: 'auto',
videoId: '4IXAxJ8oPFg',
});
}
// ]]>
</script>
</div>

Don't use two function : onYouTubePlayerAPIReady()
Live demo
<div id="player"></div>
<div id="player1"></div>
And the JS
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
player1 = new YT.Player('player1', {
player1Vars: {
modestbranding: true,
theme: 'light',
rel: 0,
wmode: "opaque",
autoplay: '0'
},
height: 'auto',
width: 'auto',
videoId: '4IXAxJ8oPFg',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
event.target.playVideo();
}
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING) {
} else {
}
}
function stopVideo() {
player.stopVideo();
}

Related

How to remove "More Videos" in embedded youtube while paused

When an embedded video is paused, YouTube will display a menu with "More Videos". I tried "$('.ytp-expand-pause-overlay .ytp-pause-overlay').css('display','none');" but it doesn't work. May I know how to disable the "More Videos" feature?
Here is the source code:
<iframe id="videoplayer"
width="640" height="360"
src="https://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&color=white&showinfo=0&ecver=2&rel=0&modestbranding=1&origin="
frameborder="0"
style="border: solid 4px #37474F">
</iframe>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script type="text/javascript">
var tag = document.createElement('script');
tag.id = 'videoplayer';
tag.src = 'https://www.youtube.com/iframe_api';
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('videoplayer',
{
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
document.getElementById('videoplayer').style.borderColor = '#FF6D00';
$('.ytp-expand-pause-overlay .ytp-pause-overlay').css('display','none');
}
function onPlayerStateChange(event) {
document.getElementById('videoplayer').style.borderColor = '#37474F';
}
</script>
Could you use playerVars for this? Documentation
<script type="text/javascript">
const tag = document.createElement("script");
tag.id = "videoplayer";
tag.src = "https://www.youtube.com/iframe_api";
const firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
let player;
function onYouTubeIframeAPIReady() {
player = new YT.Player("videoplayer", {
height: "360",
width: "640",
playerVars: {
autoplay: 1,
controls: 0,
disablekb: 1,
fs: 0,
iv_load_policy: 3,
modestbranding: 1,
rel: 0, // <--- I think this is the option you're looking for
showinfo: 0,
ecver: 2,
origin: "",
},
events: {
onReady: onPlayerReady(),
onStateChange: onPlayerStateChange(),
},
});
}
function onPlayerReady(event) {
document.getElementById("videoplayer").style.borderColor = "#FF6D00";
}
function onPlayerStateChange(event) {
document.getElementById("videoplayer").style.borderColor = "#37474F";
}
</script>

youtube api onPlayerStateChange not firing

i have looked through everything online and only see answers from a couple of years ago, and none of them help me. i see onYouTubeIframeAPIReady fire, but i never see onPlayerStateChange or onPlayerReady fire when the video plays or ends.
<iframe id="ytvideoframe1" width="100%" height="auto" src="https://www.youtube.com/embed/ZHqBp9vbO5E?enablejsapi=1&origin=http://www.earluminator.com"frameborder="0" allowfullscreen ></iframe>
<script>
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
console.log("onYouTubeIframeAPIReady");
player = new YT.Player('ytvideoframe1', {
playerVars: { 'controls': 0, 'modestbranding': 1, 'showinfo': 0 },
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
console.log("player1 created - onYouTubeIframeAPIReady");
}
function onPlayerReady(event) {
console.log("onPlayerReady");
player = new YT.Player('ytvideoframe1', {
playerVars: { 'controls': 0, 'modestbranding': 1, 'showinfo': 0 },
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerStateChange(event) {
console.log("onPlayerStateChange");
if ((event.data == YT.PlayerState.PLAYING ) ) {
console.log("PLAYING");
}
if ((event.data == YT.PlayerState.PAUSED ) ) {
console.log("PAUSED");
}
else {
console.log("OTHER");
}
}
Your onPlayerReady is not firing because as opposed to the suggested implementation here, your onPlayerReady is calling itself with the onReady event. Seems like you copied over the onYouTubeIframeAPIReady() code and forgot to make changes. As mentioned in the documentation linked above, I would suggest to change your onPlayerReady implementation as shown
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
Consequently, your onPlayerStateChange is not firing because according to your code, it is not getting there. Fixing your onPlayerReady should fix this problem as well.

How to detect youtube player finish playing

Hi I am trying to track/detect the youtube player finish video so i can redirect page into a new url. i found code somewhere but problem is that its not working in IE, have a look into my code
http://jsfiddle.net/yhb9yf21/
<div id="player"></div>
<script src="http://www.youtube.com/player_api"></script>
<script>
// create youtube player
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: '7oGgzGPK1SY',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// autoplay video
function onPlayerReady(event) {
event.target.playVideo();
}
// when video ends
function onPlayerStateChange(event) {
if(event.data === 0) {
alert('done');
}
}
</script>
its not working with IE???

youtube ie menu fix

i have the following code
<script>
window.onload = function(){
var a = document.getElementsByTagName("a");
for(var i=0; i<a.length; i++){
if(!/#ytplayer/.test(a[i].href)) continue;
var link = a[i].innerHTML.match(/\/vi\/([^\/]+)/);
if(link) (function(vidId){
a[i].onclick = function(){
player.loadVideoById(vidId);
}
})(link[1]);
}
}
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '480',
width: '853',
videoId: '963puDdR0bY',
wmode: 'transparent',
rel: '0',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 60000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
<script>
$(document).ready(function() {
$.ajax({
type: "POST",
url: "/publicPortal/restservices/tracking/click",
data: { docId: "enPubIntentionalInvesting", docType: "tvSpots" },
contentType: "text/plain"
});
});
</script>
and im calling the images here
<div id="player" style="padding-left:40px; z-index:-1;"></div>
</div>
<div id="nav" style="height:200px; padding-top:20px; padding-left:40px !important;">
<a href="#ytplayer" >
<img src="image.png" id="vid1" class="vidThumb" rel="http://www.youtube.com/watch?v=963puDdR0bY" />
<img style="display:none;" src="http://img.youtube.com/vi/963puDdR0bY/default.jpg" /></a>
<a href="#ytplayer" >
<img src="image.png" id="vid1" class="vidThumb" rel="http://www.youtube.com/watch?v=963puDdR0bY" />
<img style="display:none;" src="http://img.youtube.com/vi/963puDdR0bY/default.jpg" /></a>
<a href="#ytplayer" >
<img src="image.png" id="vid1" class="vidThumb" rel="http://www.youtube.com/watch?v=963puDdR0bY" />
<img style="display:none;" src="http://img.youtube.com/vi/963puDdR0bY/default.jpg" /></a>
</div>
you may notice i have two images there, but it seems the code needs to pull from you tube, but i wanted a custom image.
My issue is in ie the menu area goes UNDER the youtube player, ive looked around for fixes, but most are geared towards separate iframes.
ive tried adding wmode into the api call but its doesnt seem to be working! any help would be great!
so i figured it out!
you need to add playerVars to the function, like so;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '480',
width: '853',
videoId: '5hewAwxIy9k',
playerVars: {
controls: 1,
showinfo: 0 ,
modestbranding: 0,
rel: 0,
wmode: "opaque"
},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}

How can I get jCarousel to pause when a YouTube video is playing?

I have a jCarousel (jquery) running for several youtube videos. The carousel works great, and even pauses when you hover on a slide- but it will resume if you move your cursor off the slide, even if the video is playing.
Videos are embedded using the YouTube Player API.
Plugin: http://sorgalla.com/jcarousel/
This is the Carousel code I'm using:
<script type="text/javascript">
function mycarousel_initCallback(carousel, item, idx, state){
// Disable autoscrolling if the user clicks the prev or next button.
carousel.buttonNext.bind('click', function() {
carousel.startAuto(0);
});
carousel.buttonPrev.bind('click', function() {
carousel.startAuto(0);
});
// Pause autoscrolling if the user moves with the cursor over the clip.
$('#mycarousel').hover(function() {
carousel.stopAuto();
}, function() {
carousel.startAuto();
});
};
$(function() {
$('#mycarousel').jcarousel({
auto: 2,
wrap: 'both',
initCallback: mycarousel_initCallback
});
});
</script>
The YouTube script:
<script>
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var done = false;
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '505', // 535
width: '900',
videoId: 'DxZve0UV6UM',
playerVars: {
'autoplay': 0,
'controls': 0,
'modestbranding': 1,
'wmode': 'opaque'
},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(evt) {
player.setPlaybackQuality('hd720');
}
function onPlayerStateChange(evt) {
if (evt.data == YT.PlayerState.PLAYING && !done) {
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
And the HTML...
<ul id="mycarousel" class="jcarousel-skin-tango">
<li>
<div id="player"></div>
</li>
<li>
<div id="player"></div>
</li>
</ul>
Any suggestions?
Simply call carousel.startAuto() and carousel.stopAuto() when the player triggers an event:
function onPlayerStateChange(evt) {
if(evt.data == YT.PlayerState.PLAYING) {
global_carousel.stopAuto(); // Stop the carousel, if video is playing
} else {
global_carousel.startAuto(); // Otherwise, start it
}
}
Edit:
Make the variable carousel global inside mycarousel_initCallback:
var global_carousel;
function mycarousel_initCallback(carousel, item, idx, state) {
global_carousel = carousel;
//Other code
Then use that instead in the code before (as I've edited).

Resources