带有 React 问题的 Agora RTC

Posted

技术标签:

【中文标题】带有 React 问题的 Agora RTC【英文标题】:Agora RTC with React issue 【发布时间】:2020-10-09 08:01:44 【问题描述】:

我是第一次使用 Agora,在添加远程流时遇到问题。当第一个用户加入一个频道时,它会正确显示它的流,但是当另一个用户加入同一个频道时,第二个用户的流会出现在第一个用户的屏幕上,但第一个用户的流不会出现在第二个用户流上。即用户只能看到在它之后加入的人的流,而不是在它之前加入的人的流。下面是代码。

import React,  useEffect, useState, useRef  from "react";
import ReactDOM from "react-dom";
import "./App.css";
import  options, rtc  from "./constants";
import AgoraRTC from "agora-rtc-sdk-ng";

function App() 
  const RemoteUser = [];
  async function handleSubmit(e) 
    try 
      if (channelRef.current.value === "") 
        return console.log("Please Enter Channel Name");
      

      setJoined(true);

      rtc.client = AgoraRTC.createClient( mode: "rtc", codec: "h264" );
      const uid = await rtc.client.join(
        options.appId,
        channelRef.current.value,
        options.token,
        null
      );

      // Create an audio track from the audio captured by a microphone
      rtc.localAudioTrack = await AgoraRTC.createMicrophoneAudioTrack();
      // Create a video track from the video captured by a camera
      rtc.localVideoTrack = await AgoraRTC.createCameraVideoTrack();

      rtc.localVideoTrack.play("local-stream");



      rtc.client.on("user-published", async (user, mediaType) => 
        // Subscribe to a remote user
    
     
        await rtc.client.subscribe(user);
        console.log("subscribe success");
        // console.log(user);

        if (mediaType === "video" || mediaType === "all") 
          // Get `RemoteVideoTrack` in the `user` object.
          const remoteVideoTrack = user.videoTrack;
          console.log(remoteVideoTrack);

          // Dynamically create a container in the form of a DIV element for playing the remote video track.
          const PlayerContainer = React.createElement("div", 
            id: user.uid,
            className: "stream",
          );
          ReactDOM.render(
            PlayerContainer,
            document.getElementById("remote-stream")
          );

          user.videoTrack.play(`$user.uid`);
        

        if (mediaType === "audio" || mediaType === "all") 
          // Get `RemoteAudioTrack` in the `user` object.
          const remoteAudioTrack = user.audioTrack;
          // Play the audio track. Do not need to pass any DOM element
          remoteAudioTrack.play();
        
      );


     

      rtc.client.on("user-unpublished", (user) => 
        // Get the dynamically created DIV container
    
        const playerContainer = document.getElementById(user.uid);
        console.log(playerContainer);
        // Destroy the container
        playerContainer.remove();
      );

      // Publish the local audio and video tracks to the channel
      await rtc.client.publish([rtc.localAudioTrack, rtc.localVideoTrack]);

      console.log("publish success!");
     catch (error) 
      console.error(error);
    
  

  async function handleLeave() 
    try 
      const localContainer = document.getElementById("local-stream");

      rtc.localAudioTrack.close();
      rtc.localVideoTrack.close();

      setJoined(false);
      localContainer.textContent = "";

      // Traverse all remote users
      rtc.client.remoteUsers.forEach((user) => 
        // Destroy the dynamically created DIV container
        const playerContainer = document.getElementById(user.uid);
        playerContainer && playerContainer.remove();
      );

      // Leave the channel
      await rtc.client.leave();
     catch (err) 
      console.error(err);
    
  
  const [joined, setJoined] = useState(false);
  const channelRef = useRef("");
  const remoteRef = useRef("");
  const leaveRef = useRef("");

  return (
    <>
      <div className="container">
        <input
          type="text"
          ref=channelRef
          id="channel"
          placeholder="Enter Channel name"
        />
        <input
          type="submit"
          value="Join"
          onClick=handleSubmit
          disabled=joined ? true : false
        />
        <input
          type="button"
          ref=leaveRef
          value="Leave"
          onClick=handleLeave
          disabled=joined ? false : true
        />
      </div>
      joined ? (
        <>
          <div id="local-stream" className="stream local-stream"></div>
          <div
            id="remote-stream"
            ref=remoteRef
            className="stream remote-stream"
          ></div>
        </>
      ) : null
    </>
  );


export default App;

【问题讨论】:

您是否尝试添加“流订阅”侦听器? @Ankit,您找到解决方案了吗?我面临着类似的问题。请帮帮我! 【参考方案1】:

在函数handleSubmit之外创建rtcClient。

当文档/页面准备好时也注册事件。

rtc.client = AgoraRTC.createClient( mode: "rtc", codec: "h264" );
rtc.client.on("user-published", async (user, mediaType) => 
     //Your code

            
rtc.client.on("user-unpublished", (user) => 
     //Your code

它将准确地显示用户列表。

【讨论】:

以上是关于带有 React 问题的 Agora RTC的主要内容,如果未能解决你的问题,请参考以下文章

Agora RTC - Agora-SDK [DEBUG]:忽略未定义的事件

在 android agora rtc sdk 中启用音频方法问题

angular agora rtc 如何进行音频混合?

我可以使用 agora_rtc 在 Flutter 中实现一对一的语音通话(VOIP)吗?

Agora.io DYNAMIC_USE_STATIC_KEY 生成的 RTC Token (php)

Agora RTC SDK 的并发流限制是取决于视频质量还是仅取决于音频?