简体   繁体   English

如何在视频 spring 引导中滚动?

[英]How to scroll in video spring boot?

I have built a dummy video streaming application in spring boot,I have noticed that i cant skip to another frame or to a certain time in a video.我在 spring 启动中构建了一个虚拟视频流应用程序,我注意到我不能跳到另一个帧或视频中的某个时间。 Here is my code to read videoFile这是我阅读 videoFile 的代码

public byte[] getVideo() throws IOException {


        byte[] bytes = new byte[(int) file.length()];
        FileInputStream inputStream = new FileInputStream(file);
        inputStream.read(bytes);
        return bytes;
    }

and this is my what my video controller returns这就是我的视频 controller 返回的内容

return ResponseEntity.status(HttpStatus.OK)
                .header("Content-Type","video/mp4")
                .header("Content-length",String.valueOf(streamingService.file.length()))
                .body(streamingService.getVideo());

note i am not using any frontend注意我没有使用任何前端

So after little bit of extermination i found the reason for it was the browser(chrome) made another get request to certain byte range.and my getVideo() was not well written to accept the byte range and give back the required byte range.因此,经过一点点清除后,我发现它的原因是浏览器(chrome)对某个字节范围发出了另一个获取请求。而我的 getVideo() 写得不好,无法接受字节范围并返回所需的字节范围。

After rewritting my videoStreamingService the problem was solved for chrome重写我的 videoStreamingService 后,chrome 的问题已解决

here is my rewitten code for getVideo()这是我为 getVideo() 重写的代码

byte[] data;
        Long fileSize = file.length();
        String[] ranges = range.split("-");
        rangeStart = Long.parseLong(ranges[0].substring(6));
        if (ranges.length > 1) {
            rangeEnd = Long.parseLong(ranges[1]);
        } else {
            rangeEnd = fileSize - 1;
        }
        if (fileSize < rangeEnd) {
            rangeEnd = fileSize - 1;
        }
        contentLength = String.valueOf((rangeEnd - rangeStart) + 1);
       data = readByteRange( rangeStart, rangeEnd);
       return  data;

readRangeByte() method so i can read data appropriately readRangeByte() 方法,以便我可以适当地读取数据

public byte[] readByteRange(long start, long end) throws IOException {
        try (InputStream inputStream = (Files.newInputStream(Path.of(videoFileName)));
             ByteArrayOutputStream bufferedOutputStream = new ByteArrayOutputStream()) {
            byte[] data = new byte[128];
            int nRead;
            while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
                bufferedOutputStream.write(data, 0, nRead);
            }
            bufferedOutputStream.flush();
            byte[] result = new byte[(int) (end - start) + 1];
            System.arraycopy(bufferedOutputStream.toByteArray(), (int) start, result, 0, result.length);
            return result;
        }
    }

and my controller class code和我的 controller class 代码

public  ResponseEntity<byte[]> video(@RequestHeader(value = "Range",required = false)String range) throws IOException {
        if (range == null) {
            return ResponseEntity.status(HttpStatus.OK)
                    .header("Content-Type", "video/mp4")
                    .header("Content-Length", String.valueOf(streamingService.file.length()))
                    .body(streamingService.readByteRange(streamingService.getRangeStart(),streamingService.file.length()-1));
        }
        byte[] data = streamingService.getVideo(range);

        return  ResponseEntity.status(HttpStatus.PARTIAL_CONTENT)
                .header("Content-Type", "video/mp4")
                .header("Accept-Ranges", "bytes")
                .header("Content-Length", streamingService.getContentLength())
                .header("Content-Range", "bytes" + " " + streamingService.getRangeStart() + "-" + streamingService.getRangeEnd() + "/" + streamingService.file.length())
                .body(data);

After @Andreas suggestion the code works very well with both the browsers在@Andreas 建议之后,代码在两种浏览器上都运行良好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM