优化视频显示加载占位

This commit is contained in:
mmc
2026-09-08 16:09:55 +08:00
parent 39e5e80926
commit 7ebba1bbf6
2 changed files with 33 additions and 20 deletions

View File

@@ -1,19 +1,17 @@
import React, { useEffect, useRef, useState } from "react";
import AgoraRTC, { IAgoraRTCClient, IRemoteVideoTrack } from "agora-rtc-sdk-ng";
import { Badge, message } from "antd";
const APP_ID = "875f6494d87548e783fefbf33f598c97";
const TOKEN = "007eJxTYIjPuZr09PC0znd6+z0MMyL6ZHb68OetXW+kO1vHU2ZNtYECg4W5aZqZiaVJCpBhYpFqbmGclpqWlGZsnGZqaZFsaT6pZH5WQyAjA7e+LysjAwSC+JwMxmYlqcUlugZGDAwAbuYeJg==";
export default function AgrtcRoomRobot({ CHANNEL }) {
const [uid] = useState(() => Number(Math.floor(Math.random() * 100000)));
const [connected, setConnected] = useState(false);
const [joined, setJoined] = useState(false);
// 标记是否存在远端视频流
const [hasRemoteVideo, setHasRemoteVideo] = useState(false);
const remoteVideoRef = useRef<HTMLDivElement>(null);
const clientRef = useRef<IAgoraRTCClient | null>(null);
const remoteVideoTrackRef = useRef<IRemoteVideoTrack | null>(null);
const joiningRef = useRef(false);
const reconnectTimerRef = useRef<NodeJS.Timeout | null>(null);
@@ -24,7 +22,6 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
});
clientRef.current = client;
client.setClientRole("audience");
let isUnmounted = false;
// 停止并清空远端视频
@@ -38,6 +35,7 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
if (remoteVideoRef.current) {
remoteVideoRef.current.innerHTML = "";
}
setHasRemoteVideo(false);
};
// 重连逻辑
@@ -46,14 +44,12 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
console.log("执行RTC重连");
clearRemoteVideo();
setJoined(false);
try {
joiningRef.current = true;
await client.join(APP_ID, CHANNEL, TOKEN, uid);
if (isUnmounted) return;
setJoined(true);
message.info("RTC重连成功");
// 重连成功后重新订阅已经在线的上位机
for (const remoteUser of client.remoteUsers) {
if (isUnmounted) break;
@@ -61,6 +57,7 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
await client.subscribe(remoteUser, "video");
remoteVideoTrackRef.current = remoteUser.videoTrack;
remoteUser.videoTrack?.play(remoteVideoRef.current!);
setHasRemoteVideo(true);
}
}
} catch (err: any) {
@@ -77,7 +74,6 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
if (isUnmounted) return;
console.log("连接状态变更", prevState, "→", curState);
setConnected(curState === "CONNECTED");
// 信令断开,延迟重连
if (curState === "DISCONNECTED") {
clearRemoteVideo();
@@ -93,11 +89,11 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
if (isUnmounted) return;
console.log("user-published", user.uid, mediaType);
await client.subscribe(user, mediaType);
if (mediaType === "video" && remoteVideoRef.current) {
clearRemoteVideo();
remoteVideoTrackRef.current = user.videoTrack;
user.videoTrack?.play(remoteVideoRef.current);
setHasRemoteVideo(true);
}
};
@@ -149,7 +145,6 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
console.log("加入频道成功");
setJoined(true);
message.success("RTC已加入频道");
// 上位机已经提前推流,手动订阅
for (const remoteUser of client.remoteUsers) {
if (isUnmounted) break;
@@ -158,6 +153,7 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
await client.subscribe(remoteUser, "video");
remoteVideoTrackRef.current = remoteUser.videoTrack;
remoteUser.videoTrack?.play(remoteVideoRef.current!);
setHasRemoteVideo(true);
}
}
} catch (err: any) {
@@ -181,17 +177,14 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
isUnmounted = true;
if (reconnectTimerRef.current) clearTimeout(reconnectTimerRef.current);
clearRemoteVideo();
const c = clientRef.current;
if (!c) return;
c.off("connection-state-change", onConnStateChange);
c.off("user-published", onUserPublished);
c.off("user-unpublished", onUserUnpublished);
c.off("user-left", onUserLeft);
c.off("user-joined", onUserJoined);
c.off("token-privilege-will-expire", onTokenWillExpire);
c.leave();
clientRef.current = null;
};
@@ -210,10 +203,30 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
text={joined ? "已加入频道" : "未加入频道"}
styles={{ content: { color: '#888' } }}
/>
<span style={{ fontSize: 11, color: "#888" }}>频道:{CHANNEL}</span>
</div>
<div ref={remoteVideoRef} style={{ flex: 1, width: "100%", minHeight: 0 }} />
<div style={{ flex: 1, width: "100%", minHeight: 0, position: "relative" }}>
{/* 无视频时显示加载占位 */}
{!hasRemoteVideo && (
<div style={{
position: "absolute",
inset: 0,
display: "flex",
alignItems: "center",
justifyContent: "center",
color: "#aaa",
fontSize: 14,
background: "#000"
}}>
视频加载中,等待远端推流...
</div>
)}
{/* Agora 视频容器,内部会被SDK填充video dom */}
<div ref={remoteVideoRef} style={{
position: "absolute",
inset: 0
}} />
</div>
</div >
);
}

View File

@@ -1224,7 +1224,7 @@ export default function DeviceStatusPage() {
{/* 视频层:停止时不渲染 / 未播放时为空,不影响状态面板 */}
{isVideoPlaying && (
mowerView === 'all' ? (
robotVideo ? (
(
<div style={{ width: '100%', height: '100%' }}>
<MowerAllView
serialNumber={device?.serialNumber}
@@ -1233,12 +1233,12 @@ export default function DeviceStatusPage() {
ready={ready}
/>
</div>
) : null
)
) : mowerView === 'front' ? (
<div style={{ position: 'relative', width: '100%', height: '100%' }}>
{device?.hasHost ? (
<AgrtcRoomRobot CHANNEL={device?.serialNumber || ''} />
) : robotVideo ? (
) : (
<VideoPlayer
deviceId={device?.serialNumber}
idx={1}
@@ -1247,7 +1247,7 @@ export default function DeviceStatusPage() {
error={error}
ready={ready}
/>
) : null}
)}
</div>
) : (
<div style={{ position: 'relative', width: '100%', height: '100%' }}>
@@ -1268,7 +1268,7 @@ export default function DeviceStatusPage() {
)}
{/* 视频未加载 / 停止播放的占位 */}
{(!robotVideo && !isVideoPlaying) && (
{!isVideoPlaying && (
<div style={{
width: '100%', height: '100%',
display: 'flex', alignItems: 'center', justifyContent: 'center',