视频播放操作逻辑优化
This commit is contained in:
@@ -19,29 +19,43 @@
|
|||||||
v-for="i in playerNum"
|
v-for="i in playerNum"
|
||||||
:style="playerStyle"
|
:style="playerStyle"
|
||||||
class="player"
|
class="player"
|
||||||
:class="`layer-${i} ${currentSelectPlayerIndex == i ? selected : ''}`"
|
:class="`layer-${i - 1} ${currentSelectPlayerIndex == i - 1 ? selected : ''}`"
|
||||||
@click="playerSelect(i)"
|
@click="playerSelect(i - 1)"
|
||||||
>
|
>
|
||||||
<video
|
<Loading
|
||||||
style="width: 100%; height: 100%"
|
:spinning="playerLoading[i - 1]"
|
||||||
:ref="setItemRef"
|
text="加载中..."
|
||||||
muted
|
class="flex h-full w-full items-center justify-center"
|
||||||
autoplay
|
>
|
||||||
></video>
|
<video
|
||||||
|
style="width: 100%; height: 100%"
|
||||||
|
:ref="(el) => setItemRef(i - 1, el)"
|
||||||
|
muted
|
||||||
|
autoplay
|
||||||
|
></video>
|
||||||
|
</Loading>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="player-area flex h-[30px] gap-[5px]">
|
<div class="player-area flex h-[30px] gap-[5px]">
|
||||||
<div @click="onPlayerNumChanged(1)" class="h-[20px] w-[20px]">
|
<div
|
||||||
<Svg1FrameIcon style="width: 100%; height: 100%" />
|
v-for="key in 4"
|
||||||
</div>
|
@click="onPlayerNumChanged(key)"
|
||||||
<div @click="onPlayerNumChanged(2)" class="h-[20px] w-[20px]">
|
:class="playerSelectItemIndex == key ? selected : ''"
|
||||||
<Svg4FrameIcon style="width: 100%; height: 100%" />
|
class="btn-item h-[20px] w-[20px]"
|
||||||
</div>
|
>
|
||||||
<div @click="onPlayerNumChanged(3)" class="h-[20px] w-[20px]">
|
<Svg1FrameIcon v-if="key == 1" style="width: 100%; height: 100%" />
|
||||||
<Svg9FrameIcon style="width: 100%; height: 100%" />
|
<Svg4FrameIcon
|
||||||
</div>
|
v-else-if="key == 2"
|
||||||
<div @click="onPlayerNumChanged(4)" class="h-[20px] w-[20px]">
|
style="width: 100%; height: 100%"
|
||||||
<Svg16FrameIcon style="width: 100%; height: 100%" />
|
/>
|
||||||
|
<Svg9FrameIcon
|
||||||
|
v-else-if="key == 3"
|
||||||
|
style="width: 100%; height: 100%"
|
||||||
|
/>
|
||||||
|
<Svg16FrameIcon
|
||||||
|
v-else-if="key == 4"
|
||||||
|
style="width: 100%; height: 100%"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -51,7 +65,7 @@
|
|||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { onMounted, onUnmounted, ref, toRaw } from 'vue';
|
import { onMounted, onUnmounted, ref, toRaw } from 'vue';
|
||||||
import { Page } from '@vben/common-ui';
|
import { Loading, Page } from '@vben/common-ui';
|
||||||
import ChannelTree from './channel-tree.vue';
|
import ChannelTree from './channel-tree.vue';
|
||||||
import mpegts from 'mpegts.js';
|
import mpegts from 'mpegts.js';
|
||||||
import { message } from 'ant-design-vue';
|
import { message } from 'ant-design-vue';
|
||||||
@@ -67,30 +81,41 @@ import type { AddStreamProxyResult } from '#/api/sis/stream/model';
|
|||||||
|
|
||||||
const selected = 'selected';
|
const selected = 'selected';
|
||||||
|
|
||||||
const itemRefs = ref<HTMLVideoElement[]>([]);
|
const itemRefs: any[] = [];
|
||||||
const setItemRef = (el: any) => {
|
const setItemRef = (index: number, el: any) => {
|
||||||
if (el) {
|
itemRefs[index] = el;
|
||||||
itemRefs.value.push(el);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
//屏幕播放器样式
|
||||||
* 屏幕播放器数量
|
|
||||||
*/
|
|
||||||
const playerNum = ref(1);
|
|
||||||
/**
|
|
||||||
* 屏幕播放器样式
|
|
||||||
*/
|
|
||||||
const playerStyle = ref({
|
const playerStyle = ref({
|
||||||
width: '100%',
|
width: '100%',
|
||||||
height: '100%',
|
height: '100%',
|
||||||
});
|
});
|
||||||
|
// 播放器配置
|
||||||
|
const playerConfig = {
|
||||||
|
enableErrorRecover: true, // 启用错误恢复
|
||||||
|
autoCleanupMaxBackwardDuration: 30,
|
||||||
|
autoCleanupMinBackwardDuration: 10,
|
||||||
|
};
|
||||||
|
//屏幕播放器数量
|
||||||
|
const playerNum = ref(1);
|
||||||
|
// 播放器数量控制按钮组索引
|
||||||
|
const playerSelectItemIndex = ref(1);
|
||||||
|
// 当前选择的播放器索引,默认选中第一个
|
||||||
|
const currentSelectPlayerIndex = ref(0);
|
||||||
|
// 播放器数据, 每一个位置代表页面上行的一个矩形
|
||||||
|
const playerList: any[] = [];
|
||||||
|
// 播放器loading
|
||||||
|
const playerLoading = ref<boolean[]>([]);
|
||||||
|
|
||||||
const currentSelectPlayerIndex = ref(1);
|
// 当前选择播放器的key
|
||||||
|
const currentSelectKey = ref('');
|
||||||
|
// 当前所有的播放设备key
|
||||||
|
const selectKeys = ref<string[]>([]);
|
||||||
|
|
||||||
function playerSelect(index: number) {
|
function playerSelect(index: number) {
|
||||||
currentSelectPlayerIndex.value = index;
|
currentSelectPlayerIndex.value = index;
|
||||||
const player = playerList[index - 1];
|
const player = playerList[index];
|
||||||
if (player) {
|
if (player) {
|
||||||
currentSelectKey.value = player.key;
|
currentSelectKey.value = player.key;
|
||||||
} else {
|
} else {
|
||||||
@@ -104,8 +129,8 @@ function playerSelect(index: number) {
|
|||||||
*/
|
*/
|
||||||
function onPlayerNumChanged(val: number) {
|
function onPlayerNumChanged(val: number) {
|
||||||
// 1个屏幕
|
// 1个屏幕
|
||||||
const changeBeforeNum = playerNum.value;
|
|
||||||
let changeNum = 1;
|
let changeNum = 1;
|
||||||
|
playerSelectItemIndex.value = val;
|
||||||
if (val === 1) {
|
if (val === 1) {
|
||||||
playerStyle.value = {
|
playerStyle.value = {
|
||||||
width: '100%',
|
width: '100%',
|
||||||
@@ -137,26 +162,18 @@ function onPlayerNumChanged(val: number) {
|
|||||||
changeNum = 16;
|
changeNum = 16;
|
||||||
}
|
}
|
||||||
playerNum.value = changeNum;
|
playerNum.value = changeNum;
|
||||||
// 缩小布局
|
// 缩小布局,超过当前播放器的都关闭
|
||||||
if (changeBeforeNum > changeNum) {
|
for (let i = 0; i < playerList.length; i++) {
|
||||||
const playerArr = [];
|
const playInfo = playerList[i];
|
||||||
for (let i = 0; i < playerList.length; i++) {
|
if (i >= changeNum) {
|
||||||
const playerBox = playerList[i];
|
if (playInfo) {
|
||||||
if (playerBox) {
|
closePlayVieo(playInfo.player);
|
||||||
playerArr.push(playerBox);
|
|
||||||
}
|
|
||||||
playerList[i] = null;
|
|
||||||
}
|
|
||||||
for (let i = 0; i < playerArr.length; i++) {
|
|
||||||
const play = playerArr[i];
|
|
||||||
if (i < changeNum) {
|
|
||||||
// 获取播放元素
|
|
||||||
changeElPlayer(play, i);
|
|
||||||
} else {
|
|
||||||
closePlayVieo(play.player);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理树节点状态
|
||||||
|
treeSelectHandle();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -177,13 +194,6 @@ function handleParentNoe(node: any, newNode: any[] = []) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 播放器数据, 每一个位置代表页面上行的一个矩形
|
|
||||||
const playerList: any[] = [];
|
|
||||||
// 当前选择播放器的key
|
|
||||||
const currentSelectKey = ref('');
|
|
||||||
// 当前所有的播放设备key
|
|
||||||
const selectKeys = ref<string[]>([]);
|
|
||||||
|
|
||||||
function onTreeSelect(_key: any, selectNode: any) {
|
function onTreeSelect(_key: any, selectNode: any) {
|
||||||
const {
|
const {
|
||||||
selected,
|
selected,
|
||||||
@@ -193,7 +203,7 @@ function onTreeSelect(_key: any, selectNode: any) {
|
|||||||
if (level === 2) {
|
if (level === 2) {
|
||||||
// 播放
|
// 播放
|
||||||
if (selected) {
|
if (selected) {
|
||||||
doPlayer(data, currentSelectPlayerIndex.value - 1);
|
doPlayer(data, currentSelectPlayerIndex.value);
|
||||||
}
|
}
|
||||||
// 取消播放
|
// 取消播放
|
||||||
else {
|
else {
|
||||||
@@ -211,7 +221,7 @@ function onTreeSelect(_key: any, selectNode: any) {
|
|||||||
|
|
||||||
function treeSelectHandle() {
|
function treeSelectHandle() {
|
||||||
// 此处player可能已经释放所以不可能在取到只
|
// 此处player可能已经释放所以不可能在取到只
|
||||||
const player = playerList[currentSelectPlayerIndex.value - 1];
|
const player = playerList[currentSelectPlayerIndex.value];
|
||||||
currentSelectKey.value = player ? player.key : '';
|
currentSelectKey.value = player ? player.key : '';
|
||||||
const arr: string[] = [];
|
const arr: string[] = [];
|
||||||
playerList.forEach((item: any) => {
|
playerList.forEach((item: any) => {
|
||||||
@@ -245,7 +255,7 @@ function onNodeChecked(
|
|||||||
* 如果当前页面有选择播放未知,并且播放视频只有一个,则播放到制定位置
|
* 如果当前页面有选择播放未知,并且播放视频只有一个,则播放到制定位置
|
||||||
*/
|
*/
|
||||||
if (currentSelectPlayerIndex.value !== -1 && checkNode.length == 1) {
|
if (currentSelectPlayerIndex.value !== -1 && checkNode.length == 1) {
|
||||||
doPlayer(checkNode[0], currentSelectPlayerIndex.value - 1);
|
doPlayer(checkNode[0], currentSelectPlayerIndex.value);
|
||||||
}
|
}
|
||||||
// 批量播放 currentSelectPlayerIndex 将不再生效
|
// 批量播放 currentSelectPlayerIndex 将不再生效
|
||||||
else {
|
else {
|
||||||
@@ -284,40 +294,33 @@ function onNodeChecked(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function changeElPlayer(playerInfo: any, index: number) {
|
/**
|
||||||
const playerData = playerInfo.data;
|
* 添加媒体播放器事件监听
|
||||||
const oldPlayer = playerInfo.player;
|
* @param index 媒体索引
|
||||||
if (oldPlayer) {
|
* @param player 播放器对象
|
||||||
closePlayVieo(oldPlayer);
|
*/
|
||||||
}
|
function addPlayerListener(index: number, player: any) {
|
||||||
const videoConfig = {
|
// 播放结束。
|
||||||
type: 'flv',
|
player.on(mpegts.Events.LOADING_COMPLETE, (...args: any[]) => {
|
||||||
url: playerData.url,
|
console.log('LOADING_COMPLETE', args);
|
||||||
isLive: true,
|
});
|
||||||
hasAudio: false,
|
// 播放媒体数据
|
||||||
hasVideo: true,
|
player.on(mpegts.Events.MEDIA_INFO, (...args: any[]) => {
|
||||||
enableWorker: true, // 启用分离的线程进行转码
|
console.log('MEDIA_INFO', args);
|
||||||
enableStashBuffer: false, // 关闭IO隐藏缓冲区
|
loading(index, 1);
|
||||||
stashInitialSize: 256, // 减少首帧显示等待时长
|
});
|
||||||
};
|
// 播放媒体元数据到到达
|
||||||
const playerConfig = {
|
player.on(mpegts.Events.METADATA_ARRIVED, (...args: any[]) => {
|
||||||
enableErrorRecover: true, // 启用错误恢复
|
console.log('METADATA_ARRIVED', args);
|
||||||
autoCleanupMaxBackwardDuration: 30,
|
});
|
||||||
autoCleanupMinBackwardDuration: 10,
|
// 统计播放数据
|
||||||
};
|
// player.on(mpegts.Events.STATISTICS_INFO, (...args: any[]) => {
|
||||||
const player = mpegts.createPlayer(videoConfig, playerConfig);
|
// console.log('STATISTICS_INFO', args);
|
||||||
const videoElement = itemRefs.value[index];
|
// });
|
||||||
if (videoElement) {
|
// 播放异常
|
||||||
player.attachMediaElement(videoElement);
|
player.on(mpegts.Events.ERROR, (...args: any[]) => {
|
||||||
player.load();
|
console.log('ERROR', args);
|
||||||
player.play();
|
});
|
||||||
playerList[index] = {
|
|
||||||
player,
|
|
||||||
data: playerData,
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
console.log('视频播放元素获取异常');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function streamProxy(nodeData: any, cb: Function) {
|
function streamProxy(nodeData: any, cb: Function) {
|
||||||
@@ -353,7 +356,6 @@ function streamProxy(nodeData: any, cb: Function) {
|
|||||||
* 开始播放视频流
|
* 开始播放视频流
|
||||||
* @param nodeData 播放的节点数据
|
* @param nodeData 播放的节点数据
|
||||||
* @param index 播放器的索引信息
|
* @param index 播放器的索引信息
|
||||||
* @param callback 视频播放后的回调
|
|
||||||
*/
|
*/
|
||||||
function doPlayer(nodeData: any, index: number = 0) {
|
function doPlayer(nodeData: any, index: number = 0) {
|
||||||
console.log('index=', index);
|
console.log('index=', index);
|
||||||
@@ -375,13 +377,11 @@ function doPlayer(nodeData: any, index: number = 0) {
|
|||||||
enableStashBuffer: false, // 关闭IO隐藏缓冲区
|
enableStashBuffer: false, // 关闭IO隐藏缓冲区
|
||||||
stashInitialSize: 256, // 减少首帧显示等待时长
|
stashInitialSize: 256, // 减少首帧显示等待时长
|
||||||
};
|
};
|
||||||
const playerConfig = {
|
// 开启loading
|
||||||
enableErrorRecover: true, // 启用错误恢复
|
loading(index);
|
||||||
autoCleanupMaxBackwardDuration: 30,
|
|
||||||
autoCleanupMinBackwardDuration: 10,
|
|
||||||
};
|
|
||||||
const player = mpegts.createPlayer(videoConfig, playerConfig);
|
const player = mpegts.createPlayer(videoConfig, playerConfig);
|
||||||
const videoElement = itemRefs.value[index];
|
addPlayerListener(index, player);
|
||||||
|
const videoElement = itemRefs[index];
|
||||||
if (videoElement) {
|
if (videoElement) {
|
||||||
player.attachMediaElement(videoElement);
|
player.attachMediaElement(videoElement);
|
||||||
player.load();
|
player.load();
|
||||||
@@ -407,6 +407,7 @@ function closePlayVieo(plInfo: any) {
|
|||||||
try {
|
try {
|
||||||
plInfo.pause(); // 暂停
|
plInfo.pause(); // 暂停
|
||||||
plInfo.unload(); // 卸载
|
plInfo.unload(); // 卸载
|
||||||
|
plInfo.detachMediaElement();
|
||||||
plInfo.destroy(); // 销毁
|
plInfo.destroy(); // 销毁
|
||||||
treeSelectHandle();
|
treeSelectHandle();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -419,31 +420,19 @@ function closePlayer(index: number) {
|
|||||||
// 如果播放器存在,尝试关闭
|
// 如果播放器存在,尝试关闭
|
||||||
const pData = playerList[index];
|
const pData = playerList[index];
|
||||||
if (pData) {
|
if (pData) {
|
||||||
try {
|
closePlayVieo(pData.player);
|
||||||
const player = pData.player;
|
|
||||||
player.pause(); // 暂停
|
|
||||||
player.unload(); // 卸载
|
|
||||||
player.destroy(); // 销毁
|
|
||||||
playerList[index] = null;
|
|
||||||
treeSelectHandle();
|
|
||||||
} catch (e) {
|
|
||||||
console.log('播放器关闭失败,e=', e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function catchUp() {
|
/**
|
||||||
playerList.forEach((playerData) => {
|
* 开启或关闭播放器loading
|
||||||
if (playerData) {
|
* @param index 播放器索引
|
||||||
const { player, el } = playerData;
|
* @param cmd 0:开启,1:关闭
|
||||||
const end = player.buffered.end(player.buffered.length - 1);
|
*/
|
||||||
const diff = end - el.currentTime;
|
function loading(index: number, cmd: Number = 0) {
|
||||||
if (diff > 2) {
|
const loadinArr = [...playerLoading.value];
|
||||||
// 如果延迟超过2秒
|
loadinArr[index] = cmd === 0;
|
||||||
el.currentTime = end - 0.5; // 跳转到接近直播点
|
playerLoading.value = loadinArr;
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let isSupportH265 = false;
|
let isSupportH265 = false;
|
||||||
@@ -458,6 +447,20 @@ onUnmounted(() => {
|
|||||||
closePlayer(i);
|
closePlayer(i);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function catchUp() {
|
||||||
|
playerList.forEach((playerData) => {
|
||||||
|
if (playerData) {
|
||||||
|
const { player, el } = playerData;
|
||||||
|
const end = player.buffered.end(player.buffered.length - 1);
|
||||||
|
const diff = end - el.currentTime;
|
||||||
|
if (diff > 2) {
|
||||||
|
// 如果延迟超过2秒
|
||||||
|
el.currentTime = end - 0.5; // 跳转到接近直播点
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
@@ -484,5 +487,9 @@ onUnmounted(() => {
|
|||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
|
|
||||||
|
.btn-item.selected {
|
||||||
|
color: #00a8ff;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
@@ -27,7 +27,7 @@ export default defineConfig(async () => {
|
|||||||
changeOrigin: true,
|
changeOrigin: true,
|
||||||
rewrite: (path) => path.replace(/^\/api/, ''),
|
rewrite: (path) => path.replace(/^\/api/, ''),
|
||||||
// mock代理目标地址
|
// mock代理目标地址
|
||||||
target: 'http://localhost:8080',
|
target: 'http://183.230.235.66:11010/api/',
|
||||||
ws: true,
|
ws: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user