1

I'm trying to embedded a video on my site and when the site opens, the video start to play automatically. I'm using Google Chrome v76. This is my code so far:

<iframe src="https://www.youtube.com/embed/SjVwyhbUUXE?autoplay=1" width="100%" height="100%" frameborder="0" align="center"></iframe>

The frame shows the video, but is not auto playing it; the user needs to manually click on play to start playing.

What else should I do?

TylerH
  • 20,799
  • 66
  • 75
  • 101
delphirules
  • 6,443
  • 17
  • 59
  • 108

3 Answers3

2

As mentioned in the comments, most modern browsers block autoplay videos these days unless they are muted. Just change your iFrame code to this

<iframe src="https://www.youtube.com/embed/SjVwyhbUUXE?autoplay=1&mute=1" width="100%" height="100%" frameborder="0" align="center"></iframe>
APAD1
  • 13,509
  • 8
  • 43
  • 72
2

The YouTube iframe API will give you what you need. Here's some example code to get you started

<div id="video-player"></div>

<script>
    // Load the Google SAPI scripting
    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('video-player', {
            height: '100%',
            width: '100%',
            videoId: 'SjVwyhbUUXE',
            events: {
                'onReady': onPlayerReady
            }
        });
    }

    function onPlayerReady(event) {
        event.target.playVideo();
    }
</script>
touson
  • 154
  • 5
  • 1
    @MikePoole It *should* be fairly obvious from the inclusion of an API named "YouTube iframe API" and a bunch of code vs just trying to use a URL in an HTML iframe as OP's attempt illustrates... – TylerH Aug 12 '19 at 18:25
-3

This is the solution for this if you don't play automatically

<iframe src="https://www.youtube.com/embed/SjVwyhbUUXE?autoplay=0&rel=0" width="100%" height="100%" frameborder="0" align="center"></iframe>