无法访问本地和远程参与者的mediaStreamTracks

时间:2020-10-16 21:58:06

标签: twilio twilio-video

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

要重现此错误,请在此repo的“房间”组件中尝试以下代码。

// 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]);
}

1 个答案:

答案 0 :(得分:1)

这里是Twilio开发人员的传播者。

当您在tracks对象上调用Participant时,实际上并没有得到Track对象。相反,您获得了TrackPublication objects的数组。 TrackPublication代表已发布到房间的轨道,但是它不是Track对象(该对象具有您要查找的MediaStreamTrack的引用),并且远程参与者,RemoteTrack对象可能不存在,直到localParticipant订阅了曲目。

要处理此问题,您需要检查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)
      });
    }
  }
}

让我知道是否有帮助。