Search code examples
javascripthtmlvideoyoutubemute

YouTube - how to play video only without audio?


I have a video which i want to play in the website like www.paypal.com playing a video without audio on landing.

Is this a BUG? But when i tried as following, i cant mute the audio at all, even the volume parameter is set to 0.

How can i mute the audio?

<iframe id="bg" src="https://www.youtube.com/embed/VfcZsyhUJ48?autoplay=1&controls=0&loop=1&showinfo=0&modestbranding=1&disablekb=1&enablejsapi=1&rel=0" frameborder="0" volume="0" 
        style="width: 100%;height:100%;
        opacity: 0.80 !important;" 
        allowtransparency="true"></iframe>

EDIT:

Troll 1: How to play lowest to highest? depending on mobile users?

  /*
  Yes there is:

  https://www.youtube.com/embed/kObNpTFPV5c?vq=hd1440
  https://www.youtube.com/embed/kObNpTFPV5c?vq=hd1080
  etc...
  Options are:

  Code for 1440: vq=hd1440
  Code for 1080: vq=hd1080
  Code for 720: vq=hd720
  Code for 480p: vq=large
  Code for 360p: vq=medium
  Code for 240p: vq=small   
  */

Troll 2: How to play random Youtube videos?

/* YouTube - playlister*/
var videos = [];
videos[0] = 'youtubeID';
videos[1] = 'youtubeID';
videos[2] = 'youtubeID';
videos[3] = 'youtubeID';
var ii = Math.floor( (Math.random()*4 ) );
var play_now = videos[ii];

var yt = 'https://www.youtube.com/embed/'+ play_now + '?autoplay=1&controls=0&loop=1&showinfo=0&modestbranding=1&disablekb=1&enablejsapi=1&rel=0&vq=medium';

Troll 3: How to keep playing YouTube video when it ends? and loop=true does not work?

function onStateChange(e) {
  if (e.data === YT.PlayerState.ENDED) {
    player.playVideo(); 
  }
}

or

onStateChange: function(e){
    var id = 'qzZuBWMnS08';

    if(e.data === YT.PlayerState.ENDED){
        player.loadVideoById(id);
    }
}

Solution

  • Well, the volume="0" no longer seems to work but the code below does:
        <!-- 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 = "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', {
              height: '195',
              width: '260',
              videoId: 'h3P1OR9gg2Y',
              events: {
                'onReady': onPlayerReady,
                'onStateChange': onPlayerStateChange
              }
            });
          }
          // 4. The API will call this function when the video player is ready.
          function onPlayerReady(event) {
               event.target.setVolume(0);
           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;
            }
               event.target.setVolume(0);
          }
        </script>