当前位置: 首页 > java >正文

从数据库到播放器:Java视频续播功能完整实现解析

在Java中实现视频续播功能,通常需要结合后端存储(如数据库)和前端播放器配合。以下是关键步骤和代码示例:


实现思路

  1. 记录播放进度:播放时定期保存当前播放位置

  2. 存储进度数据:将进度关联用户/设备ID和视频ID存储

  3. 恢复播放:再次打开视频时读取上次保存的位置

  4. 前端配合:播放器跳转到指定时间点


后端实现(Spring Boot示例)

1. 实体类 - 播放记录
@Entity
public class VideoProgress {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String userId;    // 用户标识(可用设备ID替代)private String videoId;   // 视频唯一标识private double lastPosition; // 最后播放位置(秒)private Date updateTime;// getters/setters
}
2. Repository接口
public interface VideoProgressRepository extends JpaRepository<VideoProgress, Long> {VideoProgress findByUserIdAndVideoId(String userId, String videoId);
}
3. Service层
@Service
public class VideoService {@Autowiredprivate VideoProgressRepository progressRepo;// 保存播放进度public void savePlaybackPosition(String userId, String videoId, double position) {VideoProgress progress = progressRepo.findByUserIdAndVideoId(userId, videoId);if (progress == null) {progress = new VideoProgress();progress.setUserId(userId);progress.setVideoId(videoId);}progress.setLastPosition(position);progress.setUpdateTime(new Date());progressRepo.save(progress);}// 获取播放进度public double getLastPosition(String userId, String videoId) {VideoProgress progress = progressRepo.findByUserIdAndVideoId(userId, videoId);return (progress != null) ? progress.getLastPosition() : 0;}
}
4. Controller层
@RestController
@RequestMapping("/api/video")
public class VideoController {@Autowiredprivate VideoService videoService;// 更新进度接口@PostMapping("/progress")public ResponseEntity<Void> updateProgress(@RequestParam String userId,@RequestParam String videoId,@RequestParam double position) {videoService.savePlaybackPosition(userId, videoId, position);return ResponseEntity.ok().build();}// 获取进度接口@GetMapping("/progress")public ResponseEntity<Double> getProgress(@RequestParam String userId,@RequestParam String videoId) {double position = videoService.getLastPosition(userId, videoId);return ResponseEntity.ok(position);}
}

前端实现(JavaScript示例)

使用HTML5 video标签配合AJAX请求:

<video id="myPlayer" controls><source src="/videos/sample.mp4" type="video/mp4">
</video><script>
const player = document.getElementById('myPlayer');
const userId = "device123"; // 实际中从登录信息获取
const videoId = "video456";// 1. 尝试获取历史进度
fetch(`/api/video/progress?userId=${userId}&videoId=${videoId}`).then(res => res.json()).then(position => {if(position > 0) {player.currentTime = position; // 跳转到续播位置}});// 2. 定时保存播放进度(每5秒)
setInterval(() => {if(!player.paused) {const position = player.currentTime;fetch(`/api/video/progress?userId=${userId}&videoId=${videoId}&position=${position}`, {method: 'POST'});}
}, 5000); // 5秒保存一次// 3. 视频结束时重置进度(可选)
player.addEventListener('ended', () => {fetch(`/api/video/progress?userId=${userId}&videoId=${videoId}&position=0`, {method: 'POST'});
});
</script>

关键优化点

  1. 节流控制:使用setTimeout替代setInterval避免并发问题

  2. 本地缓存:可先用localStorage暂存进度,网络恢复后同步到服务器

  3. 进度验证:后端校验position不超过视频总时长

  4. 过期策略:超过30天的进度自动清除

  5. 并发处理:使用@Transactional保证数据一致性


数据库表结构(MySQL示例)

CREATE TABLE video_progress (id BIGINT AUTO_INCREMENT PRIMARY KEY,user_id VARCHAR(64) NOT NULL,video_id VARCHAR(64) NOT NULL,last_position DOUBLE NOT NULL DEFAULT 0,update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,UNIQUE KEY (user_id, video_id)
);

扩展场景

  1. 多端同步:用户在不同设备续播

  2. 断点续传:结合视频分片技术(HLS/DASH)

  3. 历史记录:展示所有观看过的视频进度

  4. 智能续播:超过95%进度视为已完成

提示:实际生产环境中建议使用Redis缓存播放进度,降低数据库压力并提高响应速度。

通过此实现,用户再次观看视频时将自动从上次停止位置播放,大幅提升用户体验。

http://www.xdnf.cn/news/15378.html

相关文章:

  • Netty编程模型介绍
  • 聚宽sql数据库传递
  • 【WPF】WPF 自定义控件 实战详解,含命令实现
  • Node.js + Express的数据库AB View切换方案设计
  • 渗透笔记1-4
  • vim扩展
  • Spring Boot Cucumber 测试报告嵌入方法
  • Linux 基础命令详解:从入门到实践(1)
  • 微前端框架深度对决:qiankun、micro-app、wujie 技术内幕与架构选型指南
  • MFC UI表格制作从专家到入门
  • MyBatis 在执行 SQL 时找不到名为 name 的参数
  • Unsloth 实战:DeepSeek-R1 模型高效微调指南(下篇)
  • LeetCode 424.替换后的最长重复字符
  • Android展示加载PDF
  • 深入学习前端 Proxy 和 Reflect:现代 JavaScript 元编程核心
  • HarmonyOS应用无响应(AppFreeze)深度解析:从检测原理到问题定位
  • 深入理解Transformer:编码器与解码器的核心原理与实现
  • C++ STL算法
  • C++_编程提升_temaplate模板_案例
  • 传统机器学习在信用卡交易预测中的卓越表现:从R²=-0.0075到1.0000的华丽转身
  • 复习笔记 38
  • vue3+arcgisAPI4示例:自定义多个气泡窗口展示(附源码下载)
  • (三)OpenCV——图像形态学
  • 第8天:LSTM模型预测糖尿病(优化)
  • 2025年采购管理系统深度测评
  • 小架构step系列14:白盒集成测试原理
  • 北京饮马河科技公司 Java 实习面经
  • DeepSeek 本地部署
  • LeetCode经典题解:206、两数之和(Two Sum)
  • 面向对象的设计模式