如何自动播放静音的 Youtube 视频 (IFrame API)? [英] How do I automatically play a Youtube video (IFrame API) muted?

查看:31
本文介绍了如何自动播放静音的 Youtube 视频 (IFrame API)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<iframe class="youtube-player" type="text/html" src="http://www.youtube.com/embed/JW5meKfy3fY?wmode=opaque&autohide=1&autoplay=1&volume=0&vol=0&mute=1" frameborder="0">&lt;br /&gt;</iframe>

视频没有静音!我希望第一次播放时音量为 0...

The video isn't muted! I want volume to be 0 when it first plays...

推荐答案

Youtube 不通过 url 参数提供静音(请参阅 http://code.google.com/apis/youtube/player_parameters.html).

Youtube don't provide muting through url parameter (see http://code.google.com/apis/youtube/player_parameters.html).

您必须为此使用 javascript.请参阅 http://code.google.com/apis/youtube/js_api_reference.html 了解详情.

You have to use javascript for that. see http://code.google.com/apis/youtube/js_api_reference.html for details.

但是,请注意上面链接页面上的警告:YouTube JavaScript Player API 已于 2015 年 1 月 27 日宣布弃用.YouTube Flash 嵌入也已弃用.有关更多信息,请参阅弃用政策.请将您的应用程序迁移到 IFrame API,它可以智能地使用任何嵌入式播放器 –HTML () 或 Flash () – 客户端支持."

However, please note the warning on the page linked above: "The deprecation of the YouTube JavaScript Player API was announced on January 27, 2015. YouTube Flash embeds have also been deprecated. See the deprecation policy for more information. Please migrate your applications to the IFrame API, which can intelligently use whichever embedded player – HTML () or Flash () – the client supports."

HTML

<iframe class="youtube-player" id="player" type="text/html" src="http://www.youtube.com/embed/JW5meKfy3fY?wmode=opaque&autohide=1&autoplay=1&enablejsapi=1" frameborder="0">&lt;br /&gt;</iframe>

请注意网址中的 enablejsapi=1.

please note enablejsapi=1 in the url.

Javascript

var player =  iframe.getElementById('player');
player.mute();

更新

以前的代码有一些问题,不能与当前的 API 一起使用(playerVars 语法错误).这是更新的代码.您可能需要修改所需的参数.

Previous code had some issues and did not work with current API (playerVars syntax was wrong). Here is the updated code. You may need to tinker with the parameters you need.

         
    <div id="player"></div>
    <script>
      // 1. 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);

      // 2. This function creates an <iframe> (and YouTube player)
      //    after the API code downloads.
      var player;
      function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
          height: '100%',
          width: '100%',
          playerVars: {
                    autoplay: 1,
                    loop: 1,
                    controls: 0,
                    showinfo: 0,
                    autohide: 1,
                    modestbranding: 1,
                    vq: 'hd1080'},
          videoId: '1pzWROvY7gY',
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }
        });
      }

      // 3. The API will call this function when the video player is ready.
      function onPlayerReady(event) {
        event.target.playVideo();
        player.mute();
      }

      var done = false;
      function onPlayerStateChange(event) {
        
      }
      function stopVideo() {
        player.stopVideo();
      }
    </script>

这篇关于如何自动播放静音的 Youtube 视频 (IFrame API)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆