【前端】web移动端进行监控是否完播 | 视频完播率设计

1、整体设计

1、前端播放监控:

使用移动端的前端播放器,比如 HTML5 video 标签或者第三方播放器(如 Video.js 或者 H5Player)。

在播放器中添加事件监听器来监控播放状态,例如 timeupdate、ended 等事件。

2、记录播放状态:

通过 timeupdate 事件不断记录当前播放时间。

在 ended 事件触发时,记录视频播放完毕的状态。

3、数据上报:

将播放状态和相关数据(如视频ID、用户ID、播放时长等)上报到后端服务器。

可以使用 AJAX 或 Fetch API 进行数据上报。

4、后端处理:

后端服务器接收到上报的数据后,可以将这些数据存储到数据库中,以便后续分析和监控。

2、代码实现

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Video Monitoring</title>
</head>
<body>
    <video id="myVideo" width="320" height="240" controls>
        <source src="https://your-cos-url.com/your-video-file.mp4" type="video/mp4">
        Your browser does not support the video tag.
    </video>
    
    <script>
        const video = document.getElementById('myVideo');
        let totalWatchTime = 0;
        let lastTime = 0;
        let isPlaying = false;

        video.addEventListener('play', function() {
            isPlaying = true;
            lastTime = video.currentTime;
        });

        video.addEventListener('pause', function() {
            if (isPlaying) {
                totalWatchTime += video.currentTime - lastTime;
                isPlaying = false;
            }
        });

        video.addEventListener('timeupdate', function() {
            if (!isPlaying) {
                // 如果用户拖动进度条,也需要计算观看时间
                totalWatchTime += Math.abs(video.currentTime - lastTime);
                lastTime = video.currentTime;
            }
        });

        video.addEventListener('ended', function() {
            if (isPlaying) {
                totalWatchTime += video.currentTime - lastTime;
                isPlaying = false;
            }
            console.log('Total Watch Time:', totalWatchTime);
            reportPlayStatus(video.src, 'user123', video.duration, totalWatchTime);
        });

        function reportPlayStatus(videoUrl, userId, duration, watchTime) {
            fetch('https://your-server-endpoint.com/report', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    videoUrl: videoUrl,
                    userId: userId,
                    duration: duration,
                    watchTime: watchTime,
                    status: 'ended'
                }),
            })
            .then(response => response.json())
            .then(data => {
                console.log('Success:', data);
            })
            .catch((error) => {
                console.error('Error:', error);
            });
        }
    </script>
</body>
</html>

video.duration 是按秒计算的。HTML5 视频元素的 duration 属性返回的是一个浮点数,表示视频的总时长,单位是秒。如果视频的时长是 5 分 30 秒,video.duration 将返回 330.0。

此外,video.currentTime 属性也以秒为单位,表示当前播放位置的时间点。

js 复制代码
const express = require('express');
const app = express();
const port = 3000;

app.use(express.json());

app.post('/report', (req, res) => {
    const { videoUrl, userId, duration, watchTime, status } = req.body;

    // 处理上报的数据,例如存储到数据库
    console.log('Received report:', { videoUrl, userId, duration, watchTime, status });

    // 返回响应
    res.status(200).json({ message: 'Report received' });
});

app.listen(port, () => {
    console.log(`Server is running on http://localhost:${port}`);
});
相关推荐
January12079 分钟前
【AI方法笔记】2026-11-02:模型流式吐 JSON 时,前端会遇到四种崩法
前端·人工智能·笔记·json
asdzx6712 分钟前
C# 操作 PDF 元数据:从属性写入到字段提取的完整流程
前端·pdf·c#
吹什么轩29 分钟前
constexpr 完全指南:从 const 的两种含义,到编译期计算的四次演进
java·前端·javascript
美狐美颜SDK开放平台1 小时前
开发直播APP、电商短视频与视频社交平台时,能否共用一套美颜SDK?
android·图像处理·人工智能·音视频·直播美颜sdk
AI日报派送佬1 小时前
2026年11月02日 AI 行业日报|商汤开源 Loop-9B、Pika 发布 H3 Flex 音视频模型、Meta× 傅利叶推出人形机器人 Max
人工智能·微软·开源·音视频·openai·1024程序员节·ai日报
Bruce_Liuxiaowei1 小时前
前端校验登录态,等于没校验——一次应急广播终端高危漏洞的发现与报送
运维·前端·安全
Meta Luo1 小时前
AI 时代,Web 自动化测试该怎么做?
前端·人工智能·playwright 替代工具·无代码自动化测试工具·低代码 ui 自动化测试·web 回归测试工具
去伪存真1 小时前
AI Agent 性能评估指标与自动化测试落地方案
前端·agent
揽秀亭长2 小时前
音频、五线谱、MIDI 如何转成简谱?
人工智能·音视频
MaAidan2 小时前
用 Agent 排查线上问题:SLS 日志分析 + DB/Redis 数据验证,一条链路搞清楚
前端·bootstrap·html