如何在自动播放时将多个YouTube视频静音?

时间:2017-02-15 06:30:00

标签: javascript youtube youtube-api

如何在自动播放时将多个YouTube视频静音?

我的代码:

    <iframe id="ytplayer0" src="https://www.youtube.com/embed/ab0TSkLe-E0?controls=1&showinfo=0&rel=0&enablejsapi=1" frameborder="0" allowfullscreen></iframe>

    <script>
    // http://jsfiddle.net/BFDKS/966/
    // http://stackoverflow.com/questions/24868226/how-do-you-mute-an-embedded-youtube-player
    // 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);

    var player;

    function onYouTubeIframeAPIReady() {
        player = new YT.Player('ytplayer0', {
            events: {
                'onReady': onPlayerReady
            }
        });
    }

    function onPlayerReady() {
        player.playVideo();
        // Mute!
        player.mute();
    }
    </script>

    <iframe id="ytplayer1" src="https://www.youtube.com/embed/QVGeilNsJFU?controls=1&showinfo=0&rel=0&enablejsapi=1" frameborder="0" allowfullscreen></iframe>

    <script>
    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('ytplayer1', {
            events: {
                'onReady': onPlayerReady
            }
        });
    }

    function onPlayerReady() {
        player.playVideo();
        // Mute!
        player.mute();
    }
    </script>

结果:

它只会静音并自动播放第二个......

有什么想法吗?

jsfiddle

1 个答案:

答案 0 :(得分:1)

你可能想要创建两个玩家,但<head></head>只会被调用一次。应用this method,您可以这样做:

Javascript(在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 players = new Array(); var players_attr = new Array(); players_attr["ytplayer0"] = { events: { 'onReady': onPlayer1Ready } }; players_attr["ytplayer1"] = { events: { 'onReady': onPlayer2Ready } }; function onYouTubeIframeAPIReady() { for (key in players_attr) { players[key] = new YT.Player(key, players_attr[key]); } } function onPlayer1Ready() { players["ytplayer0"].playVideo(); players["ytplayer0"].mute(); } function onPlayer2Ready() { players["ytplayer1"].playVideo(); players["ytplayer1"].mute(); } 中):

<iframe id="ytplayer0" src="https://www.youtube.com/embed/ab0TSkLe-E0?controls=1&showinfo=0&rel=0&enablejsapi=1" frameborder="0" allowfullscreen></iframe>

<iframe id="ytplayer1" src="https://www.youtube.com/embed/QVGeilNsJFU?controls=1&showinfo=0&rel=0&enablejsapi=1" frameborder="0" allowfullscreen></iframe>

HTML:

players
  • players_attr数组包含播放器对象
  • ytplayer0数组包含玩家属性

两个阵列的关键是玩家名称,例如ytplayer1&amp; mapToSource

你可以找到一个小提琴here