I am trying to embed the new iframe version of a YouTube video and get it to auto play.
As far as I can tell, there is no way of doing this by amending flags to the URL. Is there a way to do it by using javascript & the API?
I am trying to embed the new iframe version of a YouTube video and get it to auto play.
As far as I can tell, there is no way of doing this by amending flags to the URL. Is there a way to do it by using javascript & the API?
This works in Chrome but not Firefox 3.6 (warning: RickRoll video):
The JavaScript API for iframe embeds exists, but is still posted as an experimental feature.
UPDATE: The iframe API is now fully supported and "Creating YT.Player objects - Example 2" shows how to set "autoplay" in JavaScript.
The embedded code of youtube has autoplay off by default. Simply add autoplay=1
at the end of "src" attribute. For example:
2014 iframe embed on how to embed a youtube video with autoplay and no suggested videos at end of clip
rel=0&autoplay
Example Below: .
visit my site and shoot me an email and I can help you out if need be. www.imediafy.com
The flags, or parameters that you can use with IFRAME and OBJECT embeds are documented here; the details about which parameter works with what player are also clearly mentioned:
YouTube Embedded Players and Player Parameters
You will notice that autoplay
is supported by all players (AS3, AS2 and HTML5).
Multiple queries tip for those who don't know (past me and future me)
If you're making a single query with the url just ?autoplay=1
works as shown by mjhm's answer
If you're making multiple queries remember the first one begins with a ?
while the rest begin with a &
Say you want to turn off related videos but enable autoplay...
This works
and this works
But these won't work..
example comparisons
https://jsfiddle.net/Hastig/p4dpo5y4/
more info
Read NextLocal's reply below for more info about using multiple query strings
at the end of the iframe src, add &enablejsapi=1
to allow the js API to be used on the video
and then with jquery:
jQuery(document).ready(function( $ ) { $('.video-selector iframe')[0].contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*'); });
this should play the video automatically on document.ready
note, that you could also use this inside a click function to click on another element to start the video
More importantly, you cannot auto-start videos on a mobile device so users will always have to click on the video-player itself to start the video
Edit: I'm actually not 100% sure on document.ready the iframe will be ready, because YouTube could still be loading the video. I'm actually using this function inside a click function:
$('.video-container').on('click', function(){ $('video-selector iframe')[0].contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*'); // add other code here to swap a custom image, etc });
1 - add &enablejsapi=1
to IFRAME SRC
2 - jQuery func:
$('iframe#your_video')[0].contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*');
Works fine
To use javascript api,
You need Flash player 8+ and JavaScript enabled to view this video.
To play the youtube with the id:
swfobject.embedSWF
reference: https://developers.google.com/youtube/js_api_referencemagazine