视频文件保存在不为人知的地方,总之前端不能直接访问的位置,需要通过后端接口取出来再返回给前端。
前端这样子播放
<video controls="controls" controls="controls">
<source src="http://ip:port/xxxxxxx" type="video/mp4" />
</video>
src=后端接口
如果后端直接这样子写
//path为本地文件路劲
public void play(String path, HttpServletRequest request, HttpServletResponse response) {
//获取视频文件流
FileInputStream fileInputStream = null;
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
fileInputStream = new FileInputStream(new File(path));
byte[] cache = new byte[1024];
response.setHeader(HttpHeaders.CONTENT_TYPE, "video/mp4");
response.setHeader(HttpHeaders.CONTENT_LENGTH, fileInputStream.available()+"");
int flag;
while ((flag = fileInputStream.read(cache)) != -1) {
outputStream.write(cache, 0, flag);
}
outputStream.flush();
outputStream.close();
}catch (Exception e){
log.error("文件传输错误", e);
throw new

本文介绍了一种视频断点播放的实现方法,通过解析HTTP请求头中的Range信息确定视频文件的读取范围,并利用RandomAccessFile类实现了视频文件的随机读取。此方案解决了大文件播放卡顿的问题,并支持拖动进度条播放。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



