Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 当一个VideoJS实例启动时,如何暂停页面上的所有播放VideoJS实例?_Jquery_Video - Fatal编程技术网

Jquery 当一个VideoJS实例启动时,如何暂停页面上的所有播放VideoJS实例?

Jquery 当一个VideoJS实例启动时,如何暂停页面上的所有播放VideoJS实例?,jquery,video,Jquery,Video,我现在想出了这个解决方案: var myPlayer1 = _V_("ID1"); var myPlayer2 = _V_("ID2"); ... ... var myFunc1 = function(){ var myPlayer1 = this; myPlayer2.pause(); myPlayer3.pause(); myPlayer4.pause(); ...

我现在想出了这个解决方案:

    var myPlayer1 = _V_("ID1");
    var myPlayer2 = _V_("ID2");
    ...
    ...

    var myFunc1 = function(){
        var myPlayer1 = this;

        myPlayer2.pause();
        myPlayer3.pause();
        myPlayer4.pause();
        ...
    };
    myPlayer1.on("play", myFunc1);

    var myFunc2 = function(){
        var myPlayer2 = this;

        myPlayer1.pause();
        myPlayer3.pause();
        myPlayer4.pause();
        ...
    };
    myPlayer2.on("play", myFunc2);

    ...
还有其他明智的方法吗


谢谢。

假设您使用jQuery,您可以尝试此解决方案

您可以尝试避免暂停当前未播放的视频。这可以通过在循环中添加另一个检查来实现——在这里,当另一个视频开始播放时,页面上的所有视频都会暂停

另外,可能有更好的方法来避免视频ID,比如$this.player或类似的东西,但这确实起到了作用

 $(".video-js").each(function (videoIndex) {
    var videoId = $(this).attr("id");

    _V_(videoId).ready(function(){
        this.on("play", function(e) {
            //pause other video
            $(".video-js").each(function (index) {
                if (videoIndex !== index) {
                    this.player.pause();
                }
            });
        });

    });
});

假设您使用jQuery,您可以尝试此解决方案

您可以尝试避免暂停当前未播放的视频。这可以通过在循环中添加另一个检查来实现——在这里,当另一个视频开始播放时,页面上的所有视频都会暂停

另外,可能有更好的方法来避免视频ID,比如$this.player或类似的东西,但这确实起到了作用

 $(".video-js").each(function (videoIndex) {
    var videoId = $(this).attr("id");

    _V_(videoId).ready(function(){
        this.on("play", function(e) {
            //pause other video
            $(".video-js").each(function (index) {
                if (videoIndex !== index) {
                    this.player.pause();
                }
            });
        });

    });
});
在纯JS上也是如此:

在纯JS上也是如此:

我将此用于新的Videojs v5

$'.video js'.eachfunction{ videojsthis.id.readyfunction{ myPlayer=这个; myPlayer.play; }; }; 我将此用于新的Videojs v5

$'.video js'.eachfunction{ videojsthis.id.readyfunction{ myPlayer=这个; myPlayer.play; };
}; 这是一个在单击播放时暂停页面上所有其他videojs实例的有效解决方案

$(".video-js").each(function (videoIndex) {
    var videoId = $(this).attr("id");
    videojs(videoId).ready(function(){
        this.on("play", function(e) {
            $(".video-js").each(function (index) {
                if (videoIndex !== index) {
                    this.player.pause();
                }
            });
        });
    });
});

找到解决方案。

这是一个工作解决方案,用于在单击播放时暂停页面上的所有其他videojs实例

$(".video-js").each(function (videoIndex) {
    var videoId = $(this).attr("id");
    videojs(videoId).ready(function(){
        this.on("play", function(e) {
            $(".video-js").each(function (index) {
                if (videoIndex !== index) {
                    this.player.pause();
                }
            });
        });
    });
});

找到解决方案。

不适用于7.1.0版@Stepan回答在pureJS中不适用于7.1.0版@Stepan是pureJS中有效的答案