Springboot项目使用html5的video标签完成视频播放功能
文件的上传与下载会另外再写一篇博客,本篇博客只是记录视频播放功能的实现过程
1.首先引入pom文件: pom.xml
<?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 http://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.5.release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <groupid>com.wulaobo</groupid> <artifactid>excellentcourse_springboot</artifactid> <version>0.0.1-snapshot</version> <name>excellentcourse_springboot</name> <description>demo project for spring boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- pagehelper --> <dependency> <groupid>com.github.pagehelper</groupid> <artifactid>pagehelper-spring-boot-starter</artifactid> <version>1.2.5</version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-jdbc</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-thymeleaf</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.mybatis.spring.boot</groupid> <artifactid>mybatis-spring-boot-starter</artifactid> <version>2.0.1</version> </dependency> <dependency> <groupid>log4j</groupid> <artifactid>log4j</artifactid> <version>1.2.17</version> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <scope>runtime</scope> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>com.alibaba</groupid> <artifactid>druid</artifactid> <version>1.1.12</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> <configuration> <mainclass>com.wulaobo.excellentcoursespringbootapplication</mainclass> </configuration> </plugin> </plugins> </build> </project>
2.使用video标签来播放视频:
video 元素支持三种视频格式: mp4, webm, 和 ogg:
mp4 = 带有 h.264 视频编码和 aac 音频编码的 mpeg 4 文件
webm = 带有 vp8 视频编码和 vorbis 音频编码的 webm 文件
ogg = 带有 theora 视频编码和 vorbis 音频编码的 ogg 文件
我使用的是mp4格式的视频,video标签对mp4视频的编码格式有要求,所以使用 格式工厂 来对视频进行编码.
下载链接:
2.下载安装之后点击格式工厂
3. 点击 -> mp4
4. 点击输出配置,配置视频的编码格式
5.设置视频编码:
注意:视频编码要设置为 avc(h264) ,音视频编码要设置为 aac
6. 然后点击确定 ->添加文件 ->确定 ->开始就可以了
切记:上传视频时,一定要上传使用格式工厂转码后的视频,否则video标签不能播放视频
3.视频播放功能的具体实现:
1.点击播放按钮如下图所示:
2.点击播放按钮进入videocontroller获取文件的存放路径,代码如下所示:
package com.wulaobo.controller; import com.github.pagehelper.pagehelper; import com.github.pagehelper.pageinfo; import com.wulaobo.bean.video; import com.wulaobo.service.videoservice; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.ui.modelmap; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.postmapping; import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.multipart.multipartfile; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import java.io.*; import java.net.urlencoder; import java.sql.timestamp; import java.text.decimalformat; import java.util.list; import java.util.random; @controller public class videocontroller { @autowired private videoservice videoservice; //点击播放按钮,开始播放视频 @getmapping(value = "/videoplaybyidandadmin") public string videoplaybyidandadmin(integer id,modelmap model) { video video = videoservice.getvideobyid(id); model.addattribute("title",video.gettitle()); model.addattribute("path",video.getpath()); return "videoplay"; } }
3.controller层调用service层: videoserviceimpl.java
package com.wulaobo.service.impl; import com.wulaobo.bean.video; import com.wulaobo.mapper.videomapper; import com.wulaobo.service.videoservice; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import java.util.list; @service public class videoserviceimpl implements videoservice { @autowired private videomapper videomapper; @override public video getvideobyid(integer id) { return videomapper.getvideobyid(id); } }
4.service层调用dao层:videomapper.java
package com.wulaobo.mapper; import com.wulaobo.bean.video; import java.util.list; public interface videomapper { video getvideobyid(integer id); }
5.mybatis配置文件:
<?xml version="1.0" encoding="utf-8" ?> <!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.wulaobo.mapper.videomapper"> <select id="getvideobyid" parametertype="integer" resulttype="video"> select * from t_video where id=#{id} </select> </mapper>
6.获取到视频存放路径之后,跳转到视频播放页面:videoplay.html
<!doctype html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>在线视频播放</title> <link rel="shortcut icon" th:href="@{/favicon.ico}" rel="external nofollow" /> <link rel="stylesheet" th:href="@{/static/bootstrap/css/bootstrap.min.css}" rel="external nofollow" > <script th:src="@{/static/js/jquery-3.3.1.min.js}"></script> <script th:src="@{/static/bootstrap/js/bootstrap.min.js}"></script> </head> <body> <!--<div th:replace="header::header1"></div><br/><br/><br/>--> <div class="text-right "> <span class="glyphicon glyphicon-hand-right" aria-hidden="true"></span> <a th:href="@{getallnews}" rel="external nofollow" >点此返回主页</a> </div> <div class="panel panel-default"> <div class="panel-body" align="center"> <div id="a1" align="center" th:text="${title}"></div><br/> <video width="600" height="400" align="center" controls> <source th:src="@{'/upload/'+${path}}" type="video/mp4"> 您的浏览器不支持 html5 video 标签。 </video> </div> </div> </body> </html>
7.然后视频就开始播放啦。。。
项目完整代码已托管到github平台上:链接: .
到此这篇关于springboot项目使用html5的video标签完成视频播放功能的文章就介绍到这了,更多相关springboot视频播放内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!