Spring boot video streaming example-HTML5

Hello everyone, Today we will learn how to stream MP4 video using Spring Boot and Spring Web Flux. The Source code download link is provided at the end of this post.

New,

User Interface 

Project Structure:



Gradle(build.gradle)

plugins {
   id 'org.springframework.boot' version '2.5.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}

group = 'com.knf.demo'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
}

test {
useJUnitPlatform()
}


Service (VideoStreamingService.java)

package com.knf.demo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

@Service
public class VideoStreamingService {

private static final String FORMAT = "classpath:mp4/%s.mp4";

@Autowired
private ResourceLoader resourceLoader;

public Mono<Resource> getVideo(String title) {
return Mono.fromSupplier(() -> this.resourceLoader.
                                   getResource( String.format(FORMAT, title)));
}
}


Rest Controller

We indite a controller class to handle requests emanating from the client. 
package com.knf.demo.controller;
import com.knf.demo.service.VideoStreamingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RestController
public class VideoStreamingController {

@Autowired
private VideoStreamingService service;

@GetMapping(value = "video/{name}", produces = "video/mp4")
public Mono<Resource> getVideo(@PathVariable String title,
                              @RequestHeader("Range") String range) {
return service.getVideo(title);
}
}


Configuration (EndPointConfig.java)

package com.knf.demo.config;
import com.knf.demo.service.VideoStreamingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

@Configuration
public class EndPointConfig {
@Autowired
private VideoStreamingService service;

@Bean
public RouterFunction<ServerResponse> router(){
return RouterFunctions.route()
.GET("video/{name}", this::videoHandler)
.build();
}

private Mono<ServerResponse> videoHandler(ServerRequest serverRequest){
String title = serverRequest.pathVariable("name");
return ServerResponse.ok()
.contentType(MediaType.valueOf("video/mp4"))
.body(this.service.getVideo(title), Resource.class);
}
}


Main Class

package com.knf.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringWebfluxVideoStreamingExampleApplication {

public static void main(String[] args) {
SpringApplication.
                run(SpringWebfluxVideoStreamingExampleApplication.class, args);
}
}


index.html

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/
                                                   dist/css/bootstrap.min.css">

<title>Spring WebFlux Video Streaming</title>
</head>
<body>

<div class="container">
<h2>Spring WebFlux Video Streaming Example</h2>
<video src="video/sample_960x540" width="750px"
                                  height="500px" controls preload="none">

</video>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/
                                                  3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/
                                            js/bootstrap.bundle.min.js" ></script>

</body>
</html>


Run

$ mvn spring-boot:run

Access the URL in  the browser: http://localhost:8080/


$ git clone:

Comments

  1. They helped expand the product offering the good web company thinking and implementation.

    ReplyDelete
  2. They maintained open lines of communication throughout the engagement. The team's professional yet collaborative approach set them apart from other providers.
    brand marketing firm

    ReplyDelete
  3. They were consistently great at communication and have been asked to work on several more projects.
    top app design consultancies

    ReplyDelete
  4. With their talent, they have provided great results. Customers can expect excellent communication from the team.
    top brand agency

    ReplyDelete
  5. Hello, this is fastidious post I actually loved reading this.
    UX design company

    ReplyDelete

Post a Comment

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java, Spring Boot Mini Project - Library Management System - Download

Java - Blowfish Encryption and decryption Example

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete

Java - DES Encryption and Decryption example

ReactJS - Bootstrap - Buttons

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Top 5 Java ORM tools - 2024

Java - How to Count the Number of Occurrences of Substring in a String