声网组件优化
This commit is contained in:
@@ -5,271 +5,111 @@ import React, {
|
|||||||
} from "react";
|
} from "react";
|
||||||
|
|
||||||
import AgoraRTC from "agora-rtc-sdk-ng";
|
import AgoraRTC from "agora-rtc-sdk-ng";
|
||||||
|
import { message } from "antd";
|
||||||
|
|
||||||
import {
|
// 工具:解析URL参数
|
||||||
Button,
|
|
||||||
Card,
|
|
||||||
Badge,
|
|
||||||
message,
|
|
||||||
Space,
|
|
||||||
} from "antd";
|
|
||||||
|
|
||||||
const APP_ID =
|
|
||||||
"62a87b833b7e48888aca06b625f88c14";
|
|
||||||
|
|
||||||
const CHANNEL =
|
|
||||||
"test_rtc_channel";
|
|
||||||
|
|
||||||
const TOKEN = "007eJxTYGhZyLP1Ekt5av/jV1mPpi98fG75FqcXvDl7/wuUJjNeEVukwGBmlGhhnmRhbJxknmpiAQSJyYkGZklmRqZpFhbJhiYfZjJlNQQyMrDxcrIwMkAgiC/AUJJaXBJfVJIcn5yRmJeXmsPAAABcCyR4";
|
|
||||||
|
|
||||||
const client = AgoraRTC.createClient({
|
|
||||||
mode: "rtc",
|
|
||||||
codec: "vp8",
|
|
||||||
});
|
|
||||||
|
|
||||||
const parseUrlParams = (url) => {
|
const parseUrlParams = (url) => {
|
||||||
|
if (!url) return {};
|
||||||
const params = new URLSearchParams(url);
|
const params = new URLSearchParams(url);
|
||||||
return {
|
return {
|
||||||
appId: params.get("app_id") || "",
|
appId: params.get("app_id") || "",
|
||||||
channel: params.get("channel") || "",
|
channel: params.get("channel") || "",
|
||||||
token: params.get("token") || "",
|
token: params.get("token") || "",
|
||||||
uid: params.get("uid") || String(Math.floor(Math.random() * 100000)),
|
uid: params.get("uid") || String(Math.floor(Math.random() * 100000)),
|
||||||
sn: params.get("sn") || "",
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function AgoraRtcRoom({ url = "", key }) {
|
export default function AgoraRtcRoom({ url = "" }) {
|
||||||
const { appId, channel, token, uid } = parseUrlParams(url);
|
const { appId, channel, token, uid } = parseUrlParams(url);
|
||||||
|
|
||||||
|
// 关键修复:client 改成组件内 ref,不再全局!
|
||||||
|
const clientRef = useRef(null);
|
||||||
|
|
||||||
//const [uid] = useState(() =>
|
const [connected, setConnected] = useState(false);
|
||||||
// String(
|
const [joined, setJoined] = useState(false);
|
||||||
// Math.floor(
|
|
||||||
// Math.random() * 100000
|
|
||||||
// )
|
|
||||||
// )
|
|
||||||
//);
|
|
||||||
|
|
||||||
const [connected, setConnected] =
|
// 只显示远端画面(设备画面)
|
||||||
useState(false);
|
const remoteVideoRef = useRef(null);
|
||||||
|
|
||||||
const [published, setPublished] =
|
// 真正的销毁函数
|
||||||
useState(false);
|
const destroy = async () => {
|
||||||
|
const client = clientRef.current;
|
||||||
const [joined, setJoined] =
|
if (client) {
|
||||||
useState(false);
|
client.removeAllListeners();
|
||||||
|
await client.leave().catch(() => { });
|
||||||
const localVideoRef =
|
clientRef.current = null;
|
||||||
useRef<HTMLDivElement>(null);
|
}
|
||||||
|
setJoined(false);
|
||||||
const remoteVideoRef =
|
setConnected(false);
|
||||||
useRef<HTMLDivElement>(null);
|
if (remoteVideoRef.current) {
|
||||||
|
remoteVideoRef.current.innerHTML = "";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
// 必须参数检查
|
||||||
|
if (!appId || !channel || !token || !uid) {
|
||||||
|
console.log("RTC参数不全,无法播放");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 每次都创建新实例,彻底杜绝复用导致黑屏
|
||||||
|
const client = AgoraRTC.createClient({ mode: "rtc", codec: "vp8" });
|
||||||
|
clientRef.current = client;
|
||||||
|
|
||||||
const init = async () => {
|
const init = async () => {
|
||||||
|
await destroy(); // 先清理上一次
|
||||||
|
|
||||||
|
// 状态监听
|
||||||
|
client.on("connection-state-change", (state) => {
|
||||||
|
setConnected(state === "CONNECTED");
|
||||||
|
});
|
||||||
|
|
||||||
|
// 远端视频来了 → 自动播放
|
||||||
|
client.on("user-published", async (user, mediaType) => {
|
||||||
|
try {
|
||||||
|
await client.subscribe(user, mediaType);
|
||||||
|
if (mediaType === "video" && remoteVideoRef.current) {
|
||||||
|
user.videoTrack.play(remoteVideoRef.current);
|
||||||
|
}
|
||||||
|
if (mediaType === "audio") {
|
||||||
|
user.audioTrack.play();
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error("订阅失败", err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 远端离开 → 清空画面
|
||||||
|
client.on("user-left", () => {
|
||||||
|
if (remoteVideoRef.current) {
|
||||||
|
remoteVideoRef.current.innerHTML = "";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
await client.join(appId, channel, token, uid);
|
||||||
// RTC 状态监听
|
|
||||||
client.on(
|
|
||||||
"connection-state-change",
|
|
||||||
(state) => {
|
|
||||||
|
|
||||||
console.log(
|
|
||||||
"RTC状态:",
|
|
||||||
state
|
|
||||||
);
|
|
||||||
|
|
||||||
if (
|
|
||||||
state ===
|
|
||||||
"CONNECTED"
|
|
||||||
) {
|
|
||||||
|
|
||||||
setConnected(
|
|
||||||
true
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
|
|
||||||
setConnected(
|
|
||||||
false
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
// 远端用户发布流
|
|
||||||
client.on(
|
|
||||||
"user-published",
|
|
||||||
async (
|
|
||||||
user,
|
|
||||||
mediaType
|
|
||||||
) => {
|
|
||||||
|
|
||||||
console.log(
|
|
||||||
"远端用户发布:",
|
|
||||||
user.uid,
|
|
||||||
mediaType
|
|
||||||
);
|
|
||||||
|
|
||||||
// 订阅远端流
|
|
||||||
await client.subscribe(
|
|
||||||
user,
|
|
||||||
mediaType
|
|
||||||
);
|
|
||||||
|
|
||||||
// 视频
|
|
||||||
if (
|
|
||||||
mediaType ===
|
|
||||||
"video"
|
|
||||||
) {
|
|
||||||
|
|
||||||
user.videoTrack?.play(
|
|
||||||
remoteVideoRef.current!
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 音频
|
|
||||||
if (
|
|
||||||
mediaType ===
|
|
||||||
"audio"
|
|
||||||
) {
|
|
||||||
|
|
||||||
user.audioTrack?.play();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
// 用户离开
|
|
||||||
client.on(
|
|
||||||
"user-left",
|
|
||||||
(user) => {
|
|
||||||
|
|
||||||
console.log(
|
|
||||||
"用户离开:",
|
|
||||||
user.uid
|
|
||||||
);
|
|
||||||
|
|
||||||
if (
|
|
||||||
remoteVideoRef.current
|
|
||||||
) {
|
|
||||||
|
|
||||||
remoteVideoRef.current.innerHTML =
|
|
||||||
"";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
// 加入频道
|
|
||||||
//await client.join(
|
|
||||||
// APP_ID,
|
|
||||||
// CHANNEL,
|
|
||||||
// TOKEN,
|
|
||||||
// uid
|
|
||||||
//);
|
|
||||||
|
|
||||||
await client.join(
|
|
||||||
appId,
|
|
||||||
channel,
|
|
||||||
token,
|
|
||||||
uid
|
|
||||||
);
|
|
||||||
|
|
||||||
console.log(
|
|
||||||
"加入频道成功"
|
|
||||||
);
|
|
||||||
|
|
||||||
setJoined(true);
|
setJoined(true);
|
||||||
|
console.log("加入频道成功");
|
||||||
message.success(
|
|
||||||
"RTC 已加入频道"
|
|
||||||
);
|
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.error("加入失败", err);
|
||||||
console.error(
|
message.error("视频连接失败");
|
||||||
"初始化失败:",
|
|
||||||
err
|
|
||||||
);
|
|
||||||
|
|
||||||
message.error(
|
|
||||||
"初始化失败"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
init();
|
init();
|
||||||
|
|
||||||
|
// 组件销毁/URL变化 → 彻底清理
|
||||||
return () => {
|
return () => {
|
||||||
|
destroy();
|
||||||
client.leave();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}, [uid]);
|
// 关键:URL变化必须重连
|
||||||
|
}, [appId, channel, token, uid]);
|
||||||
|
|
||||||
// 开始推流
|
// 你只看设备视频 → 不需要推流,我直接删掉了
|
||||||
const startPublish =
|
|
||||||
async () => {
|
|
||||||
|
|
||||||
if (!joined) {
|
|
||||||
|
|
||||||
message.warning(
|
|
||||||
"RTC 还未加入频道"
|
|
||||||
);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
|
|
||||||
// 麦克风轨道
|
|
||||||
const audioTrack =
|
|
||||||
await AgoraRTC.createMicrophoneAudioTrack();
|
|
||||||
|
|
||||||
// 摄像头轨道
|
|
||||||
const videoTrack =
|
|
||||||
await AgoraRTC.createCameraVideoTrack();
|
|
||||||
|
|
||||||
// 本地播放
|
|
||||||
videoTrack.play(
|
|
||||||
localVideoRef.current!
|
|
||||||
);
|
|
||||||
|
|
||||||
// 发布流
|
|
||||||
await client.publish([
|
|
||||||
audioTrack,
|
|
||||||
videoTrack,
|
|
||||||
]);
|
|
||||||
|
|
||||||
console.log(
|
|
||||||
"推流成功"
|
|
||||||
);
|
|
||||||
|
|
||||||
setPublished(true);
|
|
||||||
|
|
||||||
message.success(
|
|
||||||
"开始推流"
|
|
||||||
);
|
|
||||||
|
|
||||||
} catch (err) {
|
|
||||||
|
|
||||||
console.error(
|
|
||||||
"推流失败:",
|
|
||||||
err
|
|
||||||
);
|
|
||||||
|
|
||||||
message.error(
|
|
||||||
"推流失败"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div
|
<div
|
||||||
style={{
|
style={{
|
||||||
display: "flex",
|
display: "flex",
|
||||||
@@ -277,30 +117,17 @@ export default function AgoraRtcRoom({ url = "", key }) {
|
|||||||
width: "100%",
|
width: "100%",
|
||||||
height: '100%'
|
height: '100%'
|
||||||
}}
|
}}
|
||||||
key={key}
|
|
||||||
>
|
>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div
|
<div
|
||||||
ref={
|
ref={remoteVideoRef}
|
||||||
remoteVideoRef
|
|
||||||
}
|
|
||||||
style={{
|
style={{
|
||||||
width: "100%",
|
width: "100%",
|
||||||
height: '100%',
|
height: '100%',
|
||||||
background:
|
background: "#000",
|
||||||
"#000",
|
|
||||||
borderRadius: 8,
|
borderRadius: 8,
|
||||||
overflow:
|
overflow: "hidden",
|
||||||
"hidden",
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user