How to embed an autoplaying YouTube video in an iframe?
Solution 1
This works in Chrome but not Firefox 3.6 (warning: RickRoll video):
<iframe width="420" height="345" src="http://www.youtube.com/embed/oHg5SJYRHA0?autoplay=1" frameborder="0" allowfullscreen></iframe>
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.
Solution 2
Since April 2018, Google made some changes to the Autoplay Policy. You not only need to add the autoplay=1
as a query param, but also add allow='autoplay'
as an iframe's attribute
So you will have to do something like this:
<iframe src="https://www.youtube.com/embed/VIDEO_ID?autoplay=1" allow='autoplay'></iframe>
Solution 3
The embedded code of youtube has autoplay off by default. Simply add autoplay=1
at the end of "src" attribute. For example:
<iframe src="http://www.youtube.com/embed/xzvScRnF6MU?autoplay=1" width="960" height="447" frameborder="0" allowfullscreen></iframe>
Solution 4
August 2018 I didn't find a working example on the iframe implementation. Other questions were related to Chrome only, which gave it away a little.
You'll have to mute sound mute=1
in order to autoplay on Chrome. FF and IE seem to be working just fine using autoplay=1
as parameter.
<iframe src="//www.youtube.com/embed/{{YOUTUBE-ID}}?autoplay=1&mute=1" name="youtube embed" allow="autoplay; encrypted-media" allowfullscreen></iframe>
Solution 5
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: .
<iframe width="640" height="360" src="//www.youtube.com/embed/JWgp7Ny3bOo?rel=0&autoplay=1" frameborder="0" allowfullscreen></iframe>
Related videos on Youtube
472084
Updated on January 06, 2022Comments
-
472084 over 2 years
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?
-
daniel metlitski almost 7 yearsIs there a way to mute the sound when the video starts playing, through code, I don't want to surprise my users with sound
-
mercury almost 3 yearsBrowser has settings for that
-
-
Linus Caldwell over 9 yearsHint: On mobile devices it's possible both the src parameter and the api call will not work due to restrictions: developers.google.com/youtube/…
-
Someone Somewhere over 9 yearsI find that on mobile (Webview on Android 5.0) the function
onYouTubeIframeAPIReady()
isn't triggered. Anyone have a solution ? -
tyler.frankenstein about 8 yearsUtilizing this answer, and wrapping the jQuery selector in a
setTimeout
worked for me. -
Squirrl almost 8 yearsadd
?autoplay=1
to the src -
Waheed ur Rehman almost 8 yearsyes actually autoplay=1 while ? indicate src may have parameters like color, controls and each separated by & sign. detail description can be seen here developers.google.com/youtube/player_parameters#Parameters
-
jaggedsoft over 7 yearsThe reason yours didn't work is because you already had
?rel=0
in there. If you removed that it would have worked. Read more about query string syntax here if you'd like.?rel=0?autoplay=1
should be either?autoplay=1
or?rel=0&autoplay=1
-
Hastig Zusammenstellen over 7 years@NextLocal Thanks, I'll come back to this and edit/delete my answer next time I'm working on something that iframes youtube videos.
-
jaggedsoft over 7 yearsthe example you're providing
http://www.youtube.com/embed/oHg5SJYRHA0&autoplay=1
definitely won't work, buthttps://www.youtube.com/embed/oHg5SJYRHA0?autoplay=1
should -
Hastig Zusammenstellen over 7 years@NextLocal Ha, I got it now. Not sure how I didn't realize that :D Thanks again.
-
daniel metlitski almost 7 yearsIs there a way to mute the sound when the video starts playing, through code, I don't want to surprise my users with sound
-
daniel metlitski almost 7 yearsIs there a way to mute the sound when the video starts playing, through code, I don't want to surprise my users with sound
-
Matthias Kleine almost 7 yearsSame here, just fire the event several times every second and it will work.
-
Hastig Zusammenstellen about 6 years@danielmetlitski I'm sure you've figured how to mute by now but here's an answer future readers
-
MattAllegro almost 6 years
-
Jeffz almost 6 yearsYou may also need: allow="autoplay" for your iFrame
-
mate.gvo almost 6 yearsIn my case it works from the console, or when I already interacted with the video manually. Doesn't work on load, even timed-out
-
shaneonabike over 4 yearsworks a trick! I couldn't figure out why page loads were only showing the video sometimes and not other times !
-
Gkiokan almost 4 yearsautoplay=1 as paramter is not enough (time of writing July 20). I'd to add allow='autoplay' to the iframe to get the autoplay feature working.
-
MatayoshiMariano almost 4 years@Gkiokan Yep, u need that. I had that in the snippet :)
-
Gkiokan almost 4 yearsYea, I wanted to make it clear to the others that the solution is not only the param in the URL but also the extra parameter :) Maybe you should mention that explicit.
-
Bhavin almost 4 years@danielmetlitski You can use muted attributes so you can mute your video
-
robbclarke over 3 yearsMost helpful answer by far. Makes sense.
-
Ahmed El-Atab over 2 yearsIt is not about firing the event frequently! YouTube had recently updated their policy on auto playing embedded videos. The new policy is that a video CANNOT be auto played until there is a real user interaction on the page (clicking, scrolling...). I think that in your case since you're firing the code every second, the code just run normally after the first user interaction, so you'd think that the reason is the frequent firing of the event! Nevertheless, it is NOT a good practice to fire a code like this every second!
-
BVernon over 2 yearsHave all that but still not working :(
-
Pyzard over 2 yearsThank you so much for the answer, but how could I unmute the video after autoplaying it?
-
Tim Vermaelen over 2 yearsThe point is it's impossible programmatically for embedded videos. To unmute, a user increases the volume on his player. At that point his consent is given to even play audio.
-
Toniq over 2 yearsThis is not even in the official docs: developers.google.com/youtube/player_parameters#autoplay
-
Tim Vermaelen over 2 yearsIt's a browser related thing, not a youtube thing: developer.chrome.com/blog/autoplay