YaSh Chaudhary
YaSh Chaudhary

Reputation: 2725

How to restart video track after stopping it in twilio?

I am using twilio-video v2.7.

Objective:

Restart the video track after stopping it.

Use case:

Turn off the camera led indicator when video is off and vice-versa just like google meet.

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

Upvotes: 0

Views: 894

Answers (2)

Bilal Mehrban
Bilal Mehrban

Reputation: 637

A while ago I used this code to mute/unmute media. You can change it as you like. If there is anything not clear let me now in the comments.

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

Upvotes: 1

ek_frontend_dev
ek_frontend_dev

Reputation: 11

I believe you need to re-publish the video track. As long as you are handling the event for on track published, this should suffice.

t.track.publish()

Upvotes: 0

Related Questions