Youtube iframe API with AJAX and autoplay/queuing - iframe

I'm using the Youtube iframe API, and I'm trying to get autoplay and queuing, polling, and start/stop working.
I'm starting with just getting autoplay to work. I've looked at the developer documentation so please don't post another link to that.
I include the API:
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
but because this is an AJAX request, and the DIV which holds the player isn't in the DOM when the page is loaded, I don't call function onYouTubePlayerAPIReady()
Instead, when the player is to be loaded (which is always long after the DOM has been loaded), I call
var videoID = video.split('=').pop();
var player = new YT.Player('video_holder', {
width: "480",
height: "295",
videoId: videoID,
events: {'onready': onPlayerReady}
});
function onPlayerReady(event) {
event.target.playVideo();
}
This code loads the video, but the video doesn't play. The only error I have in the console is the unsafe JavaScript attempt to access error, which I understand is common for the iframe API.

The event names are case sensitive so change onready to onReady.

Related

Enforce nocookie mode when using the YouTube iframe API

I'd like to ensure that when I initialise a new YT.player() that I can set it to make sure that it uses an embed from the www.youtube-nocookie.com domain. Is this currently possible? I haven't been able to find any reference to it in the docs yet.
I know, this is a late answer, but maybe relevant as it's still working:
You can use the host-parameter:
function onYouTubeIframeAPIReady() {
var player = new YT.Player('player', {
videoId: 'aqz-KE-bpKQ',
host: 'https://www.youtube-nocookie.com',
playerVars: {
origin: window.location.host
}
});
}
var s = document.createElement('script');
s.src = 'https://www.youtube.com/iframe_api';
document.head.appendChild(s);
<div id="player"></div>
Sadly, it does not work here on stackoverflow because of the same-origin-policy.
I have the same code running in an active project, though..
Ah. I managed to solve this just as I was about to post my question.
In my code I use something like:
<iframe id="myVideo" src="https://www.youtube-nocookie.com/embed/<VIDEO_ID>?enablejsapi=1"></iframe>
...then I can use the following to get the player that has already been embedded in the HTML via JS:
var player;
var onYouTubeIframeAPIReady = function() {
player = new YT.Player("myVideo");
};
Still, it would be nice if there was a way to initialise a www.youtube-nocookie.com video with JS too.

YouTube IFrame Player API - preview frame

Does anyone know if it's possible to define which video-frame is used as a preview, using the YouTube IFrame Player API?
When my video is embedded now, it shows a video-frame near the end of my video, while I want it to be one of the first frames.
I don't think that this is possible using the API, but you could display the preview image yourself. This was discussed on following stackoverflow page: YouTube embedded video: set different thumbnail
No. It's not (yet) possible.
But Niels his answer got me inspired.
Actually his referral was about Youtube video's which are already embedded in the document, while the YouTube IFrame Player API embeds the video by javascript after the document has finished loading.
HTML part:
<div id="ytplayer">
<img id="ytThumb" src="http://placehold.it/640x390&text=Play">
</div>
Javascript part:
$(document).ready(function(){
$('#ytThumb').click(function() {
//Embed the youtube player
// Load 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);
});
});
// Replace the 'ytplayer' element with an <iframe> and
// YouTube player after the API code downloads.
function onYouTubeIframeAPIReady() {
var player;
player = new YT.Player('ytplayer', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
'autoplay': 1,
'rel': 0
}
});
}
While my container, image and video all have the same dimensions.

Youtube API https Protocol

When I create an iframe player API using the onYouTubeIframeAPIReady, the link is created with the http protocol
Example:
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_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 onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
width: '560',
height: '600',
videoId: '7j8B_r4OfAw',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
iframe Result:
<iframe id="player" frameborder="0" allowfullscreen="1" title="YouTube video player" width="560" height="600" src="http://www.youtube.com/embed/7j8B_r4OfAw?enablejsapi=1"></iframe>
Does anyone know how to do the video to be created with the https protocol? Need to install the api on a platform.
Please, suggest!
You can specify https if you create the iframe element directly in your html, rather than using the div which gets replaced later on. You can create the iframe tag dynamically if you need to. Look at the bottom of this section, which spells out how to do it.
Beware — even if you load the player over https, the actual video stream may be served over http. That seems to cause a mixed-mode warning in Chrome, though not other browsers (in my experience last year; it may have since changed). See this official blog post, which explains that the player can be loaded over https but warns that the video still won't necessarily be served that way.

youtube api iframe loadvideobyid starts video where last one left off

I am using youtube api iframe and just getting going with the sample code. When I issue:
player.loadVideoById("ytidNo1") the proper video starts ("ytidNo1" is a valid youtube id)
Then, say, 10 seconds later, if I issue another:
player.loadVideoById("ytidNo2") the 2nd video starts, but it starts at the point where the last one was playing at (eg 10 seconds in).
This is new behavior in the last day or so and I have not changed my code.
To correct the problem, I tried:
player.loadVideoById("ytidNo1",0)
then
player.loadVideoById("ytidNo2",0)
and issue still happens.
Anyone else have or just starting to have this issue?
I also had the same problem. Playing around I eventually got it to work.
I had to use seekTo(0); (see Jim's comments)
More details about the problem.
If the video actually played to the end I didn't need to do anything.
If the video was skipped part way through I had to call
player.seekTo(0);
on the old video before calling loadVideoByID("ytid", 0); on the next video.
I also had to handle the case where after calling loadVideoByID I got an error. (e.g. 150) If I had an error I could not call seekTo because it would cause the next video to play audio only.
hope this helps
I am trying to reproduce the issue using the official sample::
https://developers.google.com/youtube/iframe_api_reference
after the stop I am selecting another video in the same way, and seems to work well:
<!DOCTYPE html>
<html>
<body>
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "//www.youtube.com/iframe_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 onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'u1zgFlCw8Aw',
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, 6000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
player.loadVideoById("tYrND5hMY3A");
}
</script>
</body>
</html>​
http://jsfiddle.net/5wxFv/3/

Google maps KmlLayer times out and InfoWindow not showing

I have a google map embedded in a site that loads a kml file at https://www.getstable.org/who-can-help/therapist-map-kml using KmlLayer. Sometimes the map doesn't load up, I presume because google maps has a strict timeout, and often some of the pins on the map aren't clickable but some are with no clear reason why. Does anyone know what the timeout limit is on kmlLayer and how to increase it? Also is there any reason why sometimes some of the pins aren't clickable (ie no InfoWindow appears when you click a pin and the cursor doesn't change to a hand)?
Here's the code that shows it (some of the fields are templated):
<div id="map_canvas" style="width: 856px;height: 540px;">Loading...</div>
<script type="text/javascript" src="{protocol}://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var the_map = {
options : {
zoom:{embed:zoom_level},
center:new google.maps.LatLng({embed:latitude},{embed:longitude}),
mapTypeId: google.maps.MapTypeId.ROADMAP
},
geocoder : null,
map : null,
init : function() {
this.geocoder = new google.maps.Geocoder();
$('#map_canvas').delegate('a', 'click', function(event) {
window.location.href=$(this).attr('href');
return false;
});
},
load_map : function() {
this.map = new google.maps.Map(document.getElementById("map_canvas"), this.options);
query = encodeURI('{site_url}{embed:map_url}');
var ctaLayer = new google.maps.KmlLayer(query,{
preserveViewport:true
});
ctaLayer.setMap(this.map);
}
}
$(document).ready(function() {
the_map.init();
the_map.load_map();
});
</script>
The Google Servers have an unspecified timeout, but testing shows it to be 3-5 seconds. This timeout is not something you can affect. The solution is to make your server respond faster. This issue almost always comes down to a file that is too big (yours isn't) or from dynamically generating the KML. You need to optimize this and that may mean finding a way to create a static KML file.
Features that are not clickable are almost certainly a problem with your KML. You can validate your KML to check for this:
Feed Validator
KML Validator
You can also test your KML by loading it at maps.google.com.

Resources