启动/停止嵌入式Facebook视频

时间:2015-06-02 21:14:38

标签: javascript jquery facebook iframe html5-video

Facebook现在有一个嵌入式html5 iframe,用于包含视频

实施例

(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s);
  js.id = id;
  js.src = "//connect.facebook.net/nl_NL/sdk.js#xfbml=1&version=v2.3";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
<div id="fb-root"></div>
<div class="fb-video" data-allowfullscreen="true" 
  data-href="/iloveamersfoort/videos/vb.785215881547195/804162796319170/?type=1">

如果您在页面上有多个嵌入视频,是否有任何机构知道如何使用JavaScript停止。

使用html5视频,我可以这样做:

document.addEventListener('play', function(e){
    var videos = document.getElementsByTagName('video');
    for(var i = 0, len = videos.length; i < len;i++){
        if(videos[i] != e.target){
            videos[i].pause();
        }
    }
}, true);

但也许有人已经有了解决方案?

1 个答案:

答案 0 :(得分:0)

    $(function(){
        var fb_play_arr=[];
        window.fbAsyncInit = function() {

        FB.init({
            xfbml: true,
            version: 'v2.5'
        });

        FB.Event.subscribe('xfbml.ready', function(msg) {
            if (msg.type === 'video') {
                fb_play_arr.push(msg);

                msg.instance.subscribe('startedPlaying', function() {
                    $.each(fb_play_arr, function(j){
                        if (fb_play_arr[j].id != msg.id) {
                            fb_play_arr[j].instance.pause();
                        } 
                    });
                });
            }
        });
    };

(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/zh_TW/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
});