0

mediaStreamTracks即使roomState更改为连接后,我也无法获取本地和远程参与者的信息。但是,我注意到它们在重新连接后变得可用。请告知我在连接到房间后如何访问mediaStreamTracks本地和远程参与者。

要重现该错误,请尝试在此repo的 Room 组件中使用以下代码。

// Below function is from room component of this repo
import useRoomState from '../../hooks/useRoomState/useRoomState';
import useVideoContext from '../useVideoContext/useVideoContext';

export default function Room() {
const { room } = useVideoContext();
  const {
    room: { localParticipant },
  } = useVideoContext();
  const [participants, setParticipants] = useState(Array.from(room.participants.values()));
  const roomState = useRoomState();

  useEffect(() => {
    if(roomState === 'connected'){
          localParticipant.tracks.forEach(track => {
          if (track.kind === 'data' ) {
                      return;
            }
          console.log(track.mediaStreamTrack)  // Getting undefined!!
          }

        participants.forEach(participant => {
            participant.tracks.forEach(track => {
                  if (track.kind === 'data') {
                      return;
                   }
                   console.log(track.mediaStreamTrack)  // Getting undefined!!
            }
       }
  }, [roomState]);
}
4

1 回答 1

1

Twilio 开发人员布道者在这里。

当你调用tracks一个Participant对象时,你实际上并没有得到Track对象。TrackPublication相反,您会得到一个对象数组。ATrackPublication表示已发布到房间的轨道,但它不是Track对象(它引用了MediaStreamTrack您要查找的 ),并且对于远程参与者,RemoteTracklocalParticipant订阅追踪。

要处理这个问题,您需要检查是否TrackPublication有可用的轨道,如果有,请使用它,如果没有,请侦听“订阅”事件。

例如,对于远程参与者:

participants.forEach(participant => {
  participant.tracks.forEach(trackPublication => {
    if (track.kind === 'data') {
      return;
    }
    if (trackPublication.track) {
      console.log(trackPublication.track.mediaStreamTrack);
    } else {
      trackPublication.on("subscribed", track => {
        console.log(track.mediaStreamTrack)
      });
    }
  }
}

让我知道这是否有帮助。

于 2020-10-17T04:02:05.917 回答