在twilio中将其停止后,如何重新启动视频轨道?

时间:2020-09-09 09:22:50

标签: javascript reactjs twilio webrtc twilio-video

我正在使用twilio-video v2.7。

目标:

停止后重新启动视频轨道。

用例:

当视频关闭时,关闭摄像头指示灯,反之亦然,就像谷歌见面一样。

const toggleVideo = () => {
    const isOff = !isVideoOn;
    room.localParticipant.videoTracks.forEach((t) => {
      if (isOff) {
        t.track.enable();
        t.track.attach(screenRef.current)
      } else {
        t.track.disable();
        t.track.stop()
        t.unpublish()
        t.track.detach()
      }
    });
    setVideoToggle(isOff);
  };

2 个答案:

答案 0 :(得分:0)

我相信您需要重新发布视频轨道。只要您为发布的活动处理事件,就足够了。

t.track.publish()

答案 1 :(得分:0)

前一段时间,我使用此代码来静音/取消静音媒体。您可以根据需要更改它。如果有任何不清楚的地方,让我现在在评论中。

/**
 * Mute/unmute your media in a Room.
 * @param {Room} room - The Room you have joined
 * @param {'audio'|'video'} kind - The type of media you want to mute/unmute
 * @param {'mute'|'unmute'} action - Whether you want to mute/unmute
 */
function muteOrUnmuteYourMedia(room, kind, action) {
  const publications = kind === 'audio'
    ? room.localParticipant.audioTracks
    : room.localParticipant.videoTracks;

  publications.forEach(function(publication) {
    if (action === 'mute') {
      publication.track.disable();
    } else {
      publication.track.enable();
    }
  });
}
/**
 * Mute your video in a Room.
 * @param {Room} room - The Room you have joined
 * @returns {void}
 */
function muteYourVideo(room) {
  muteOrUnmuteYourMedia(room, 'video', 'mute');
}
/**
 * UnMute your video in a Room.
 * @param {Room} room - The Room you have joined
 * @returns {void}
 */
function muteYourVideo(room) {
  muteOrUnmuteYourMedia(room, 'video', 'unmute');
}