大屏启动获取视频url,未线上测试
This commit is contained in:
@@ -377,29 +377,99 @@
|
||||
|
||||
<script src="static/video.min.js"></script>
|
||||
<script>
|
||||
// 配置直播流地址 - 请根据实际情况修改这些URL
|
||||
// 配置直播流地址 - 通过接口动态获取
|
||||
const streamConfigs = {
|
||||
1: {
|
||||
url: 'https://demo.unified-streaming.com/k8s/live/scte35.isml/.m3u8', // 请替换为实际的m3u8地址
|
||||
title: '监控点 1 - 主入口'
|
||||
url: '', // 将通过接口获取
|
||||
title: '监控点 1 - 主入口',
|
||||
videoId: 'cam001'
|
||||
},
|
||||
2: {
|
||||
url: 'https://stream-akamai.castr.com/5b9352dbda7b8c769937e459/live_2361c920455111ea85db6911fe397b9e/index.fmp4.m3u8', // 请替换为实际的m3u8地址
|
||||
title: '监控点 2 - 走廊'
|
||||
url: '', // 将通过接口获取
|
||||
title: '监控点 2 - 走廊',
|
||||
videoId: 'cam002'
|
||||
},
|
||||
3: {
|
||||
url: '', // 请替换为实际的m3u8地址
|
||||
title: '监控点 3 - 大厅'
|
||||
url: '', // 将通过接口获取
|
||||
title: '监控点 3 - 大厅',
|
||||
videoId: 'cam003'
|
||||
},
|
||||
4: {
|
||||
url: '', // 请替换为实际的m3u8地址
|
||||
title: '监控点 4 - 出口'
|
||||
url: '', // 将通过接口获取
|
||||
title: '监控点 4 - 出口',
|
||||
videoId: 'cam004'
|
||||
}
|
||||
};
|
||||
|
||||
// 视频播放器实例
|
||||
const players = {};
|
||||
|
||||
// 获取视频流URL的接口地址(通过Nginx代理)
|
||||
const VIDEO_API_URL = '/api/v1/video-urls'; // 使用相对路径,通过Nginx代理到后端
|
||||
|
||||
// 获取视频流URL的接口
|
||||
async function fetchVideoUrls() {
|
||||
try {
|
||||
const videoIds = Object.values(streamConfigs).map(config => config.videoId);
|
||||
|
||||
console.log('发送请求到:', VIDEO_API_URL);
|
||||
console.log('请求体:', JSON.stringify({
|
||||
"videoId": videoIds
|
||||
}));
|
||||
|
||||
const response = await fetch(VIDEO_API_URL, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
"videoId": videoIds
|
||||
})
|
||||
});
|
||||
|
||||
console.log('响应状态:', response.status, response.statusText);
|
||||
|
||||
if (!response.ok) {
|
||||
let errorDetails = '';
|
||||
try {
|
||||
const errorData = await response.json();
|
||||
errorDetails = JSON.stringify(errorData);
|
||||
} catch (e) {
|
||||
errorDetails = await response.text();
|
||||
}
|
||||
throw new Error(`HTTP error! status: ${response.status}, details: ${errorDetails}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
console.log('响应数据:', data);
|
||||
|
||||
// 更新streamConfigs中的URL
|
||||
if (data.videoUrl && Array.isArray(data.videoUrl)) {
|
||||
Object.keys(streamConfigs).forEach((screenId, index) => {
|
||||
if (data.videoUrl[index]) {
|
||||
streamConfigs[screenId].url = data.videoUrl[index];
|
||||
console.log(`屏幕 ${screenId} 获取到URL: ${data.videoUrl[index]}`);
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
} else {
|
||||
throw new Error('返回数据格式错误');
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('获取视频流URL失败:', error);
|
||||
|
||||
// 在所有屏幕显示错误
|
||||
for (let i = 1; i <= 4; i++) {
|
||||
showError(i);
|
||||
updateStatus(i, '获取流地址失败: ' + errorMessage);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化所有视频播放器
|
||||
function initializePlayers() {
|
||||
for (let i = 1; i <= 4; i++) {
|
||||
@@ -633,7 +703,7 @@
|
||||
}
|
||||
|
||||
// 页面加载完成后初始化
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
document.addEventListener('DOMContentLoaded', async function() {
|
||||
// 初始化视频播放器
|
||||
initializePlayers();
|
||||
|
||||
@@ -641,21 +711,40 @@
|
||||
updateTime();
|
||||
setInterval(updateTime, 1000);
|
||||
|
||||
// todo 后期改为页面加载后,调用获取视频url接口,取得视频url后,再调用下面代码,延迟时间可以设为更短的时间例如300ms
|
||||
// 页面加载3秒后,调用重新加载逻辑
|
||||
setTimeout(() => {
|
||||
console.log('页面加载3秒后,开始重新加载所有直播流');
|
||||
for (let i = 1; i <= 4; i++) {
|
||||
const config = streamConfigs[i];
|
||||
// 检查URL是否为空,只有配置了有效URL才重新加载
|
||||
if (config && config.url) {
|
||||
console.log(`重新加载屏幕 ${i} 的直播流`);
|
||||
refreshStream(i);
|
||||
} else {
|
||||
console.log(`屏幕 ${i} 未配置有效URL,跳过重新加载`);
|
||||
// 首先获取视频流URL
|
||||
console.log('开始获取视频流URL...');
|
||||
|
||||
// 显示加载状态
|
||||
for (let i = 1; i <= 4; i++) {
|
||||
showLoading(i);
|
||||
updateStatus(i, '获取流地址中...');
|
||||
}
|
||||
|
||||
// 调用接口获取视频URL
|
||||
const success = await fetchVideoUrls();
|
||||
|
||||
if (success) {
|
||||
console.log('视频流URL获取成功,开始加载直播流');
|
||||
|
||||
// 延迟300ms后开始加载所有直播流
|
||||
setTimeout(() => {
|
||||
console.log('开始加载所有直播流');
|
||||
for (let i = 1; i <= 4; i++) {
|
||||
const config = streamConfigs[i];
|
||||
// 检查URL是否为空,只有配置了有效URL才重新加载
|
||||
if (config && config.url) {
|
||||
console.log(`加载屏幕 ${i} 的直播流`);
|
||||
refreshStream(i);
|
||||
} else {
|
||||
console.log(`屏幕 ${i} 未获取到有效URL,跳过加载`);
|
||||
showError(i);
|
||||
updateStatus(i, '无可用流地址');
|
||||
}
|
||||
}
|
||||
}
|
||||
}, 1500);
|
||||
}, 300);
|
||||
} else {
|
||||
console.error('视频流URL获取失败,无法加载直播流');
|
||||
}
|
||||
|
||||
// 添加键盘快捷键
|
||||
document.addEventListener('keydown', function(e) {
|
||||
|
||||
Reference in New Issue
Block a user