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 sample application from our GitHub repository. Download Link is provided at the end of this post.
User Interface 

Project Structure


Home Controller.class

@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);
}
}

Spring Boot(Main class)

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

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);
}
}

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>
$ git clone  

Comments

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

    ReplyDelete

Post a Comment