Spring boot video streaming example-HTML5

Hello everyone, Today we will learn how to stream MP4 video using Spring. You can download the source code of this simple application from our GitHub repository. The Source code download Link is provided at the end of this post.

User Interface 



Project Structure




Maven(pom.xml)

A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details utilized by Maven to build the project. It contains default values for most projects.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<!-- lookup parent from repository -->
</parent>
<groupId>com.knowledgefactory</groupId>
<artifactId>springboot-videostream</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-videostream</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- spring mvc, rest -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<description>springboot-videostream</description>
</project>

Home Controller.class

We indite a controller class to handle requests emanating from the client. The @Controller annotation indicates that a particular class serves the role of a controller.
@Controller
public class HomeController {
@Autowired
private MyResourceHttpRequestHandler handler;
private final static File MP4_FILE =
new File("D:\\videofiles\\video1.mp4");


@GetMapping("/index")
public String home() {
return "index";
}

// supports byte-range requests
@GetMapping("/byterange")
public void byterange(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute(MyResourceHttpRequestHandler.ATTR_FILE, MP4_FILE);
handler.handleRequest(request, response);
}
}

MyResourceHttpRequestHandler.class

@Component
public class MyResourceHttpRequestHandler extends ResourceHttpRequestHandler {
final static String ATTR_FILE = MyResourceHttpRequestHandler.
class.getName() + ".file";

@Override
protected Resource getResource(HttpServletRequest request) throws
IOException {
final File file = (File) request.getAttribute(ATTR_FILE);
return new FileSystemResource(file);
}
}


Spring Boot(Main class)

A Spring Boot application's main class is a class that contains a public static void main() method that commences up the Spring ApplicationContext. By default, if the main class isn't explicitly designated, Spring will probe for one in the classpath at compile time and fail to commence if none or multiple of them are found.

@SpringBootApplication
@ComponentScan({ "com" })
public class KnowledgefactorydemoApplication {
public static void main(String[] args) {
SpringApplication.run(KnowledgefactorydemoApplication.class, args);
}
}


view(index.ftl)

<div class="row align-items-center justify-content-center">
<video width="1000" height="400" controls>
<source src="byterange" type="video/mp4">
</video>
</div>

Run 

$ mvn spring-boot:run

App running at: 



More Topics:


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

Post a Comment