java web实现video播放
1.前端的实现
- 通过getQueryVariable获取url的参数(vid )
function getQueryVariable(variable){
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++){
var pair = vars[i].split("=");
if(pair[0] == variable){
return pair[1];}
}
return(false);
}
var data={
vid:getQueryVariable("vid") };
- 在single播放页加入播放控件
<!--播放-->
<video id ="video_iframe" src ="#"controls = "controls"style="..."class="video- grid-single-page-agileits"width="750" height="400"></video>
- 通过ajax上传获取的vid值和播放控件实现服务器的数据交换,实现网页布局更新
$.ajax({
url:"/getvideobyid",
type:"post",
data:data,
dataType:"json",
success:function (result) {
// 取id="video_iframe",调用后台反过来的videopath路径,实现播放功能
$("#video_iframe").attr("src","http://192.168.5.103/"+result.videopath);
$("#myname").html(result.name); }
});
- 在index页面实现跳转事件
<a href="single.html?vid=${movie.vid}" class="hvr-shutter-out-horizontal">
2.后台实现对数据的查询等交互功能
- 接收网页传过来的vid值
String vid = req.getParameter("vid");//1`
- VideoDao实现:通过vid查找并返回数据库中的movie对象
public class VideoDao extends BaseDao {
public Movie queryVideo(String vid){
String sql = "select * from video where vid = ?";
List<Map<String,Object>> maps = super.queryAnything(sql,vid);
String name = String.valueOf(maps.get(0).get("name"));
String author = String.valueOf(maps.get(0).get("author"));
String imagepath = String.valueOf(maps.get(0).get("imagepath"));
String videopath =String.valueOf(maps.get(0).get("videopath"));
Timestamp time =Timestamp.valueOf(maps.get(0).get("createtime").toString());
String type= String.valueOf(maps.get(0).get("type"));
Movie movie = new Movie();
movie.setCreatetime(time);
movie.setVideopath(videopath);
movie.setImagepath(imagepath);
movie.setType(type);
movie.setAuthor(author);
movie.setName(name);
return movie;
}
}
- 在VideoService中定义
public interface VideoService {
public Movie queryMovieById(String vid);
}
- 在VideoServiceImpl定义
//queryMovieById实现通过vid查找并返回相对应的movie对象
public class VideoServiceImpl implements VideoService{
@Override public Movie queryMovieById(String vid) {
VideoDao videoDao = new VideoDao();
Movie movie = videoDao.queryVideo(vid);
return movie;
}
}
- VideoPlayServlet实现返回查询结果(movie)给网页
public class VideoPlayServelet extends HttpServlet {
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("utf-8");
String vid = req.getParameter("vid");//1
VideoService videoService = new VideoServiceImpl();
Movie movie = videoService.queryMovieById(vid);
//ajax json 数据在网络上传输都是字符串
Gson gson = new Gson();
String result = gson.toJson(movie);
//将数据返给客户
resp.getWriter().write(result);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/34176.html