javascript - 在每个幻灯片转换上暂停多个 YouTube Iframe 视频

标签 javascript iframe youtube youtube-api flexslider

我是 YouTube Iframe API 的新手,当用户移动到另一张幻灯片时,我尝试暂停幻灯片(jQuery Flexslider 插件)中的多个嵌入式 Iframe 视频。如果播放器未暂停,视频将继续播放,并且由于幻灯片放映中会有多个视频,因此当显示新幻灯片时,我需要每个视频都暂停。 Iframe 不是通过 API 插入的,但由于我使用的 CMS,它们已经存在于 html 中。

我已通读 API 引用和我在此处找到的几个答案,但无法弄清楚如何在用户转换到新幻灯片时暂停任何/每个 Iframe 视频。

在我最近的尝试中,如下面的代码所示,我尝试在单击链接时暂停 Iframe,因为页面上的唯一链接将用于导航到下一张和上一张幻灯片。我还尝试暂停通过 flexslider API 提供的幻灯片转换事件上的所有视频,但没有成功。

var players = {}; //players storage object
function onYouTubeIframeAPIReady(){
    $('iframe[id]').each(function(){
        var identifier = this.id;
        var frameID = getFrameID(identifier);
        if (frameID) { // if iframe exists
            players[frameID] = new YT.Player(frameID, {
                events: {
                    "onStateChange" : createYTEvent(frameID, identifier)
                }
            });            
        }
    });
}

//returns a function to enable  multiple events
function createYTEvent(frameID, identifier){
    return function (event){
        var player = players[frameID]; //set player reference
        var done = false;

        $('a').click(function(){

            player.pauseVideo();
            return false;

        });        
    }
}

任何帮助将不胜感激

最佳答案

抱歉我的英语不好,我是法国人,但我最近遇到了同样的问题,也许我想我将来可以帮助你或其他人。

与您不同的是,我只使用了 1 个播放器加载 4 个视频的列表,而不是 4 个播放器。

这是我的 HTML:

<div id='videos'>
    <div id="player">
        <!-- the iframe drectly write in the html, with a list of videos (4 in the example) -->
        <iframe id="ytplayer" type="text/html" width="640" height="360" src="https://www.youtube.com/embed/youtubeVideoId1?playlist=youtubeVideoId2,youtubeVideoId3,youtubeVideoId4&version=3" frameborder="0" allowfullscreen></iframe>
    </div>
        <!-- below, the buttons for switching the 4 videos -->
        <span id='pagination'>
            <a onclick='video_slide(0);' id='puce1'></a>
            <a onclick='video_slide(1);' id='puce2'></a>
            <a onclick='video_slide(2);' id='puce3'></a>
            <a onclick='video_slide(3);' id='puce4'></a>
        </span>
    </div>
</div>

和我的 JavaScript:

<script type="text/javascript">
    // call the youtube API
    var tag = document.createElement('script');
    tag.src = "//www.youtube.com/iframe_api";
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

    // init the player
    var player;
    function onYouTubeIframeAPIReady() {
        player = new YT.Player('ytplayer', {
            events: {
                // the function "onPlayerStateChange is call when the player load, 
                   play, pause or stop a video
            'onStateChange': onPlayerStateChange
            }
        });
    }

    function onPlayerStateChange(event) {
        // if a new video is buffering (the event.data is -1 when buffering, 
           1 when playing, 2 when paused ...)
        if (event.data == -1) {
            // it mean that the player pass at the next video, 
               so we call the bellow function to turn on the corresponding button
            getTheRightPuce();
        }
    }
    // I add a function to recognize the current video playing, 
       and turn on the right button automatically when at the end of a video, 
       the player pass on the next one
    function getTheRightPuce(){
        var vidIndex = player.getPlaylistIndex();

        $("#pagination a").css('backgroundColor','#000000');

        if (vidIndex==0){$("#puce1").css('backgroundColor','red');}
        else if (vidIndex==1){$("#puce2").css('backgroundColor','red');}
        else if (vidIndex==2){$("#puce3").css('backgroundColor','red');}
        else if (vidIndex==3){$("#puce4").css('backgroundColor','red');}
    }

    // the function called by the buttons, change the current video 
       on the player and turn on the corresponding button, 
       the parameter is the index of the video in the play-list 
       (0 for the first, 1 for second...)
    function video_slide(sld){

        player.playVideoAt(sld);

        $("#videos").css('display','none');
        $("#pagination a").css('backgroundColor','#000000');
        $("#videos").fadeIn(500);
        if (sld==0){$("#puce1").css('backgroundColor','red');}
        else if (sld==1){$("#puce2").css('backgroundColor','red');}
        else if (sld==2){$("#puce3").css('backgroundColor','red');}
        else if (sld==3){$("#puce4").css('backgroundColor','red');}
    }
</script>

我试图让它尽可能清晰。

我希望这会对您或其他人有所帮助:)

关于javascript - 在每个幻灯片转换上暂停多个 YouTube Iframe 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12559301/

相关文章:

javascript - Yeoman Mean.js 生成的自定义 AngularJS 指令不起作用

google-chrome - 如何在 Chrome 中自动播放带有视频的 iframe

youtube - Google G-Suite/Youtube

javascript - 根据是否提交表单或单击 div,通过 AJAX 调用发送不同的数据

javascript - 将文本从数组添加到 div 并在 X 秒后将其删除

javascript - 高级语法 : "0[constructor][constructor]()()" - How does it works to evaluate code?

javascript - 如何在跨源请求后访问 iframe.contentDocument 以获得响应?

javascript - 使用 ServiceWorker 缓存 iframe 请求

java - YouTubePlayerSupportFragment无法播放视频

javascript - 多个 Youtube Iframe API