YouTube - 如何仅在没有音频的情况下播放视频?

时间:2016-09-06 07:22:09

标签: javascript html5 video youtube mute

我有一个视频,我希望在网站上播放,例如www.paypal.com在播放时播放没有音频的视频。

这是一个BUG吗?但是当我尝试跟随时,我根本不能将音频静音,即使音量参数设置为0。

如何将音频静音?

<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>

修改

巨魔1:如何从最低到最高?取决于移动用户?

  /*
  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   
  */

巨魔2:如何播放随机播放的Youtube视频?

/* 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';

巨魔3:如何在YouTube视频结束时继续播放?和loop = true不起作用?

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

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

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

2 个答案:

答案 0 :(得分:1)

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>

答案 1 :(得分:-1)

尝试添加新数据属性volume="0"

相关问题