停止播放所有YouTube iframe嵌入? [英] Stop all YouTube iframe embeds from playing?

查看:97
本文介绍了停止播放所有YouTube iframe嵌入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困住了!我正在使用jquery,点击,拉一个youtube json-c feed,抓住第一个id,然后使用它将html5 iframe视频添加到ul的顶部。然后我递增它并拉出第二个id,第三个id等,并相应地添加它们。

I'm stuck! I'm using jquery to, on click, pull a youtube json-c feed, grab the first id, and use it to prepend the html5 iframe video to the top of the ul. I then increment it and pull the second id, third id, etc and prepend them accordingly.

问题是,当一个新视频被预先添加时,我想要任何一个之前的视频停止播放。他们用javascript / jquery完成这个任务吗?

The problem is, when a new video is prepended, I want any of the previous videos to stop playing. Is their any way to accomplish this with javascript/jquery?

这是函数调用:

function videoCall(i) {
$.getJSON('http://gdata.youtube.com/feeds/api/standardfeeds/most_recent?v=2&max-results=50&alt=jsonc', function(vid) { 
    var fullItem ='';
    var videoID = (vid.data.items[i].id);
    var videoLink = 'http://www.youtube.com/watch?v=' + videoID;
    var videoTitle = (vid.data.items[i].title);

    fullItem += '<li class="videoItem">';
    fullItem += '<iframe title="YouTube video player" width="900" height="536" src="http://www.youtube.com/embed/' + videoID + '" frameborder="0" allowfullscreen></iframe>';
    fullItem += '<a href="mailto:?subject=ThinkerBot Video&body=' + videoLink + '">Email This</a>';
    fullItem += '</li>';

    $(fullItem).hide().prependTo('#content ul').slideDown('slow');
});
}


推荐答案

你应该可以做类似这样的事情:

You should be able to do something like this:

function players(func, args) {
    var iframes = document.getElementsByTagName('iframe');
    for (var i=0; i<iframes.length; ++i) {
        if (iframes[i]) {
            var src = iframes[i].getAttribute('src');
            if (src) {
                if (src.indexOf('youtube.com/embed') != -1) {
                    iframes[i].contentWindow.postMessage(JSON.stringify({'event': 'command','func': func,'args': args || []}), "*");
                }
            }
        }
    }
}

// example usage
players('stopVideo');

这篇关于停止播放所有YouTube iframe嵌入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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