js判断视频是否可以成功播放 layui打开视频页面 thymeleaf显示视频列表

本文探讨了JavaScript中如何处理视频跨域加载问题,通过playVideo函数实现视频预加载及错误提示,并对比了使用XMLHttpRequest进行不跨域请求的方法。

html

<div class="play_btn" th:data-id="${video.Url}" th:data-size="${video.Size}" onclick="playVideo(this.getAttribute('data-id'),this.getAttribute('data-size'))"></div>

js
跨域

function playVideo(content,size) {

    var video = document.createElement('video');

    video.onload = function() {
        layerOpenMsg("<p style=\"text-align: center;font-size: 25px;margin-top: 120px;\"><b>视频存在</b></p>");
    }

    video.onerror = function() {
        var msg;
        if(size > 52428800){
            msg = "<p style=\"text-align: center;font-size: 25px;margin-top: 120px;\"><b>视频超过50M,未下载</b></p>";
        }else {
            msg = "<p style=\"text-align: center;font-size: 25px;margin-top: 120px;\"><b>视频正在下载,请稍后再播</b></p>";
        }
        layerOpenMsg(msg);
    }

    video.src = content;
    //不同浏览器情况不同,这里判断在该浏览器是否可以播放
    video.oncanplaythrough = function() {
        layer.open({
            type: 2,//iframe层
            title: false,//不显示标题
            area: ['630px', '360px'],
            shade: 0.8,
            closeBtn: 0,
            shadeClose: true,
            content: content
        });
    };

}

不跨域

var http = new XMLHttpRequest();
http.open('HEAD', '/folder/video.mp4');

http.onreadystatechange = function() {
    if (this.readyState == this.DONE) {
        if (this.status != 404) {
          // resource exists
        }
    }
};

http.send();
### SpringBoot 整合 Layui 实现视频播放功能 #### 项目背景 SpringBoot 是一种用于简化新 Spring 应用程序初始构建和开发过程的框架[^4]。而 Layui 则是一个经典的前端 UI 框架,能够帮助开发者快速构建美观的网页界面。通过将两者结合起来,可以实现高效的前后端分离架构。 要实现在 SpringBoot 中集成 Layui 并完成视频播放的功能,可以通过以下方法: --- #### 方案概述 为了在 SpringBoot 和 Layui 的组合中实现视频播放功能,通常需要以下几个部分的支持: 1. **HTML 页面布局**:利用 Layui 提供的模板引擎或 HTML 结构来嵌入 `<video>` 标签。 2. **后端接口支持**:提供视频资源路径或者动态加载视频流的服务。 3. **静态资源配置**:确保 SpringBoot 能够正确访问到前端所需的静态文件(如 CSS、JS 文件)。 以下是具体的实现方案及其代码示例。 --- #### 前端页面 (Layui 部分) 在 LayuiHTML 页面中,可以直接使用标准的 HTML5 `<video>` 标签来展示视频内容。下面是一段简单的示例代码: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Video Player</title> <!-- 引入 Layui 的样式 --> <link rel="stylesheet" href="/layui/css/layui.css"> </head> <body> <div class="layui-container"> <h1 style="text-align:center;">视频播放器</h1> <div style="margin-top:20px;"> <video width="640" height="360" controls autoplay> <source src="${videoUrl}" type="video/mp4"> <!-- 后端返回的视频 URL --> Your browser does not support the video tag. </video> </div> </div> <!-- 引入 LayuiJS 文件 --> <script src="/layui/layui.js"></script> <script> // 初始化 Layui 组件 layui.use(['layer'], function(){ var layer = layui.layer; // 可在此处添加其他交互逻辑 }); </script> </body> </html> ``` 上述代码中的 `${videoUrl}` 表达式会由后端渲染替换为实际的视频地址。 --- #### 后端控制器 (SpringBoot 部分) 在 SpringBoot 中,可以通过 Controller 来暴露视频资源的下载链接。如果视频存储于本地服务器上,则可通过 `ResourceLoader` 加载并返回给客户端;如果是远程视频,则直接返回对应的 URL 即可。 ##### 示例代码:本地视频资源服务 ```java import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import java.io.IOException; @RestController public class VideoController { private final PathMatchingResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver(); @GetMapping("/api/video/{filename}") public ResponseEntity<Resource> getVideo(@PathVariable String filename) throws IOException { Resource file = resourceLoader.getResource("classpath:static/videos/" + filename); // 视频存放在 resources/static/videos 下 if (!file.exists()) { return ResponseEntity.notFound().build(); } return ResponseEntity.ok() .header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + file.getFilename() + "\"") .contentType(MediaType.parseMediaType("video/mp4")) .contentLength(file.contentLength()) .body(file); } } ``` 此代码片段展示了如何从本地读取视频文件并通过 HTTP 接口将其发送至浏览器。 --- #### 静态资源配置 确保 SpringBoot 正确识别静态资源位置非常重要。默认情况下,SpringBoot 已经自动扫描了 `src/main/resources/static` 目录下的所有文件。因此只需将 Layui 所需的 CSS 和 JS 文件放置于此即可。 对于视频文件,建议也放入 `resources/static/videos` 子目录下以便管理。 --- #### 测试运行 启动 SpringBoot 项目后,在浏览器中打开如下地址测试效果: ``` http://localhost:8080/api/video/example.mp4 ``` 同时,也可以通过 Thymeleaf 渲染后的 HTML 页面查看最终的效果。 --- ### 总结 以上介绍了如何在 SpringBoot 和 Layui 的环境中实现基本的视频播放功能。核心在于合理配置前端页面结构以及后端 API 支持,并妥善处理静态资源的位置与加载方式。 ---
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值