Sunday, 22 October 2023

insert Youtube video in webpage with auto play function

 <!DOCTYPE html>

<html>

    <head>

        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, minimum-scale=1.0, user-scalable=yes">

    </head>

  <body>

<!-- 1. The <iframe> (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 = "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: '300px',

      height: '300px',

      videoId: 'x5CrknRXWZ0',

      playerVars: { 'autoplay': 1, 'playsinline': 1 },

      events: {

        'onReady': onPlayerReady

      }

    });

  }


  // 4. The API will call this function when the video player is ready.

  function onPlayerReady(event) {

     event.target.mute();

    event.target.playVideo();

  }

</script>

  </body>

</html>