使用Spring Boot,我想制作基于RESTful的视频播放器。我在文件浏览器中有我的.mp4扩展名视频。如何通过创建rest端点在前端提供这些视频?
我尝试过这种方法。 视频可以开始或停止。但是无法向后或向前。无法获得所需的分钟数并开始。
Spring Content支持视频流媒体播放。使用Spring Content和文件系统(FS),您可以创建一个以文件系统为后台的视频存储库,将您的视频放入该存储库中,并使用伴随库Spring Content REST将它们通过HTTP提供给任何前端视频播放器。
通过start.spring.io或您的IDE Spring项目向导(在撰写本文时为Spring Boot 1.5.10)创建新的Spring Boot项目。添加以下Spring Content依赖项,确保你最终有以下这些依赖项:
<dependencies>
<!-- Standard Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.3</version>
</dependency>
<!-- Spring Content -->
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-fs-boot-starter</artifactId>
<version>0.0.9</version>
</dependency>
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-rest-boot-starter</artifactId>
<version>0.0.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
在你的Spring Boot应用程序类中创建一个VideoStore。将其注释为Store REST资源。这会导致Spring Content注入实现(该接口的文件系统)以及添加REST端点来保存您不必自己编写的任何内容:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@StoreRestResource(path="videos")
public interface VideoStore extends Store<String> {}
}
默认情况下,Spring Content 将在 java.io.tmpdir 下创建一个存储库。因此,您还需要设置 SPRING_CONTENT_FS_FILESYSTEM_ROOT 环境变量,指向您的“存储库”的根目录。
将视频复制到此“根”位置。启动应用程序,您的视频将可以从以下位置流式传输:
/videos/MyVideo.mp4
val video = UrlResource("file:$videoLocation/$name")
return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT)
.contentType(MediaTypeFactory
.getMediaType(video)
.orElse(MediaType.APPLICATION_OCTET_STREAM))
.body(video)