javascript - 获取用户在 jwplayer 中观看视频的总时间

标签 javascript jquery jwplayer jwplayer6 jwplayer7

我想从 jwplayer 捕获视频的总播放/观看时间。使用下面的示例代码。

jwplayer("player").setup({
    "playlist": [
        {
            "sources": [
                {
                    "default": false,
                    "file": 'https://content.jwplatform.com/manifests/yp34SRmf.m3u8',
                    "label": "0",
                    "type": "hls",
                    "preload": "meta"
                }
            ]
        }
    ],
    "height": "240",
    "width": "100%",
    "aspectratio": "16:9",
    "stretching": "uniform",
    "controls": true,
    "autostart": true
});

jwplayer("player").on('time', function (e) {
    var count = this.getPosition();        
});

你能帮我做这件事吗?

最佳答案

最好的方法是依靠 JW Player 事件来实现此目的,特别是 on('time') 事件。

let totalTimeWatched = 0;
let previousPosition = 0;
jwplayer().on('time', (e) => {
    const { position } = e;
    totalTimeWatched += (position - previousPosition);
    previousPosition = position;
});

如果您确实还想计算广告时间,则可以使用 adTime 事件,该事件还具有 position 属性。

编辑

为了解释搜索行为,您可以使用额外的监听器 on('seek') 来重置 previousPosition。见下文:

let totalTimeWatched = 0;
let previousPosition = 0;
jwplayer().on('time', (e) => {
    const { position } = e;
    totalTimeWatched += (position - previousPosition);
    previousPosition = position;
});

jwplayer().on('seek', (e) => {
    previousPosition = e.offset;
});

关于javascript - 获取用户在 jwplayer 中观看视频的总时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60200894/

相关文章:

javascript - Angular 5 StaticInjectorError :[Http]

javascript - 如何从 ref 对象插入项目

javascript - 使用 Jquery 访问 JSON 解码中的第二个数组

jquery - Geb测试:缺少表单元素

javascript - Jwplayer闪烁 "Error loading player: Could not load player configuration"

javascript - 使用javascript为jwplayer的HTML元素覆盖设置wmode

javascript - 如何在 VS Code 中使用 JavaScript 私有(private)字段?

javascript - 从 select2 选中的选项中获取属性值

jquery - insertBefore - 如果元素不存在

iPad 上 jw 播放器上的 jQuery 点击事件