【问题标题】:JW Player - Stop (target) Multiple Players with an Event HandlerJW Player - 使用事件处理程序停止(目标)多个玩家
【发布时间】:2018-02-16 17:18:55
【问题描述】:

我有一个点击处理程序来触发内置的jwplayer().stop(); 功能。然而,只有我的第一个处理程序执行,另一个失败(但没有记录错误)。

视频被嵌入到视图中的元素 div 中。

换句话说:我可以停止第一个视频,但不能停止第二个、第三个等等,即使我能够在我的function() 中记录每次后续点击。

想法? 谢谢。

$(function(){
    console.log('ready');
    $('.stop').on('click',function stopVideo () {
       //stop JW player
        if (typeof(jwplayer) != 'undefined') {
                console.log('video stopped');
                jwplayer().stop();

            }         
    })
});

查看

<body>
<div class="container">
    <!-- first video -->
    <div class="jw" style="width: 40%; margin: 10px auto">
        <script src="//content.jwplatform.com/players/4sng2RGX-UQtQ90mG.js"></script>
        <button class="stop" style="padding: 8px 19px; float: right; margin: 10px">stop video</button>
    </div>

    <!-- second video -->
    <div class="jw" style="width: 40%; margin: 10px auto">
        <script src="//content.jwplatform.com/players/z5Jka98V-UQtQ90mG.js"></script>
        <button class="stop" style="padding: 8px 19px; float: right; margin: 10px">stop video</button>
    </div>
</div>
</body>

【问题讨论】:

    标签: javascript jquery callback jwplayer jwplayer7


    【解决方案1】:

    Targeting Multiple Players 下的 JW 文档:

    在您的 API 调用中不包含 ID 或索引将始终针对页面上的第一个玩家。 https://developer.jwplayer.com/jw-player/docs/developer-guide/api/javascript_api_introduction/

    这里有一个解决方案:

    $(function(){
        //get every video with class .stopVideo
        var count = $('.stopVideo').length;
    
        $('#stop').on('click',function() {
           //loop through all videos stored in count
           for(var i = 0; i < count; i++) {
            if (typeof(jwplayer) != 'undefined') {
                    console.log('video stopped');
                    //stop player
                    jwplayer(i).stop();
    
                } 
            }        
        })
    });
    

    查看

    <body>
    <div class="container">
        <!-- first video -->
        <div class="stopVideo">
            <script src="//content.jwplatform.com/players/4sng2RGX-UQtQ90mG.js"></script>
        </div>
    
        <!-- second video -->
        <div class="stopVideo">
            <script src="//content.jwplatform.com/players/z5Jka98V-UQtQ90mG.js"></script>
        </div>
        <button id="stop">stop video</button>
    </div>
    </body>
    

    我同意,如果有人在没有给出理由的情况下对某个问题投反对票,那是很不幸的。继续努力,祝编码愉快!

    【讨论】:

    • 非常感谢@Null 是真的。我非常感谢您的洞察力。
    • 是的,没有概率@tonkihonks13
    【解决方案2】:

    您的stopVideo() 函数无法知道它应该停止哪个视频。 jwplayer 只是一些全局变量,可能与您单击的按钮相关,也可能不相关。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-18
    • 2012-10-31
    • 2014-05-06
    • 1970-01-01
    • 2014-03-09
    相关资源
    最近更新 更多