HTML5视频流+spring boot

ua4mk5z4  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(476)

我有一个控制器:

@GetMapping(value = "videos/{id}/{name}")
@ResponseBody
public final ResponseEntity<InputStreamResource>
retrieveResource(@PathVariable(value = "id") final String id,
                 @PathVariable(value = "name") final String name) throws Exception {

    File initialFile = new File(id + name);
    InputStream targetStream = new FileInputStream(initialFile);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.valueOf("video/mp4"));
    headers.set("Accept-Ranges", "bytes");
    headers.set("Expires", "0");
    headers.set("Cache-Control", "no-cache, no-store");
    headers.set("Connection", "keep-alive");
    headers.set("Content-Transfer-Encoding", "binary");

    return new ResponseEntity<>(new InputStreamResource(targetStream), headers, HttpStatus.OK);

}

当我使用浏览器访问时http://localhost:7080/视频/2/3
我看到了视频,但当我把它放在一个html页面时却看不到:

<div class="media-box foto">
                <video width="320" height="240" controls>

                    <video url="http://localhost:7080/videos/2/3" type="video/mp4">

                </video>
            </div>
vojdkbi0

vojdkbi01#

你的java代码看起来不错。这个问题与你的html有关。
请尝试:

<div class="media-box foto">
  <video src="http://localhost:7080/videos/2/3"
         width="320" height="240" controls>
    <p>Your browser does not support HTML5 video. Here is a <a href="http://localhost:7080/videos/2/3">link to the video</a> instead.</p>
  </video>
</div>

除非有多个视频格式,否则通常不会显示视频格式。例如:

<div class="media-box foto">
  <video width="320" height="240" controls>
    <source src="http://localhost:7080/videos/2/3?format=mp4" type="video/mp4">
    <source src="http://localhost:7080/videos/2/3?format=webm" type="video/webm">
    <p>Your browser does not support HTML5 video. Here is a <a href="http://localhost:7080/videos/2/3">link to the video</a> instead.</p>
  </video>
</div>

可以包含其他属性,如 autoplay 或者 muted . 请参阅mdn的相关文档。

相关问题