欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

SpringBoot接口实现视频在线播放

时间:2023-06-16

 获取所有的mp4在线播放地址,并当前端调用该接口时返回mp4视频文件名及其播放地址,mp4视频存放在D盘的video文件夹下

@ApiOperation("获取所有MP4播放地址") @GetMapping("/get/video/address") public List> getVideoAddress() { File file = new File("D:\video"); List> fileNs = new ArrayList>(); String filename = ""; File[] subFile = file.listFiles(); for (File value : subFile) { // 判断是否为文件夹 if (!value.isDirectory()) { Map jsonVideos = new HashMap<>(); String subFileName = value.getName(); // 判断是否为mp4结尾 if (subFileName.trim().toLowerCase().endsWith(".mp4")) { filename = "http://localhost:8080/video/alone/video/play" + subFileName; String chFileName = VIDEO_EN_CH.get(subFileName); jsonVideos.put("filename",chFileName); jsonVideos.put("address",filename); fileNs.add(jsonVideos); } } } return fileNs; }

单个视频在线播放接口,这里需要传mp4文件名

@ApiOperation("单个MP4播放") @GetMapping(value = "/alone/video/play/{filename}" ,produces ="application/json;charset=utf-8") public void aloneVideoPlay(HttpServletRequest request, @PathVariable("filename") String fileName, HttpServletResponse response) { InputStream is = null; OutputStream os = null; try { response.setContentType("video/mp4"); File file = new File("D:\video\" + fileName); response.addHeader("Content-Length", "" + file.length()); is = new FileInputStream(file); os = response.getOutputStream(); IOUtils.copy(is, os); } catch (Exception e) { log.error("播放MP4失败", e); } finally { if (null != os) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } }

完整Controller

@Api(tags = "视频Controller")@RestController@RequestMapping("/video")public class VideoController { @ApiOperation("获取所有MP4播放地址") @GetMapping("/get/video/address") public List> getVideoAddress() { File file = new File("D:\video"); List> fileNs = new ArrayList>(); String filename = ""; File[] subFile = file.listFiles(); for (File value : subFile) { // 判断是否为文件夹 if (!value.isDirectory()) { Map jsonVideos = new HashMap<>(); String subFileName = value.getName(); // 判断是否为mp4结尾 if (subFileName.trim().toLowerCase().endsWith(".mp4")) { filename = "http://localhost:8080/video/alone/video/play" + subFileName; String chFileName = VIDEO_EN_CH.get(subFileName); jsonVideos.put("filename",chFileName); jsonVideos.put("address",filename); fileNs.add(jsonVideos); } } } return fileNs; } @ApiOperation("单个MP4播放") @GetMapping(value = "/alone/video/play/{filename}" ,produces ="application/json;charset=utf-8") public void aloneVideoPlay(HttpServletRequest request, @PathVariable("filename") String fileName, HttpServletResponse response) { InputStream is = null; OutputStream os = null; try { response.setContentType("video/mp4"); File file = new File("D:\video\" + fileName); response.addHeader("Content-Length", "" + file.length()); is = new FileInputStream(file); os = response.getOutputStream(); IOUtils.copy(is, os); } catch (Exception e) { log.error("播放MP4失败", e); } finally { if (null != os) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } } }

扩展对视频进行分页

        PageUtil

public class PageUtil { public static List startPage(List list, Integer pageNum, Integer pageSize) { if (list == null) { return null; } if (list.size() == 0) { return null; } Integer count = list.size(); // 记录总数 Integer pageCount = 0; // 页数 if (count % pageSize == 0) { pageCount = count / pageSize; } else { pageCount = count / pageSize + 1; } int fromIndex = 0; // 开始索引 int toIndex = 0; // 结束索引 if (pageNum != pageCount) { fromIndex = (pageNum - 1) * pageSize; toIndex = fromIndex + pageSize; } else { fromIndex = (pageNum - 1) * pageSize; toIndex = count; } List pageList = list.subList(fromIndex, toIndex); return pageList; }}

获取所有MP4播放地址(分页),需要前端调该接口时,传两个参数,pageNum及pageSize,页码和每页多少条

@ApiOperation("获取所有MP4播放地址") @PostMapping("/get/video/address") public List> getVideoAddress(@RequestBody Map json) { File file = new File("D:\video"); List> fileNs = new ArrayList>(); String filename = ""; File[] subFile = file.listFiles(); for (File value : subFile) { // 判断是否为文件夹 if (!value.isDirectory()) { Map jsonVideos = new HashMap<>(); String subFileName = value.getName(); // 判断是否为mp4结尾 if (subFileName.trim().toLowerCase().endsWith(".mp4")) { filename = "http://localhost:8080/video/alone/video/play" + subFileName; String chFileName = VIDEO_EN_CH.get(subFileName); jsonVideos.put("filename",chFileName); jsonVideos.put("address",filename); fileNs.add(jsonVideos); } } } String pageNum = json.get("pageNum"); String pageSize = json.get("pageSize"); return PageUtil.startPage(fileNs, Integer.parseInt(pageNum), Integer.parseInt(pageSize)); }

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。