Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
twilio video code leaving video tag behind when I detach track

So Im a bit confused.

The way this works, is each person in my video call already has space in the ui when they join. When they turn on their video the code attaches their track to their respective div space on the ui.

Just for clarity here is the start video code. It is unrelated to this question, but good for context.

startvideo(){
    navigator.mediaDevices.getUserMedia({video: true}).then((stream)=>{
      createLocalVideoTrack({
        deviceId: stream.id
      }).then(localVideoTrack =>{
        return this.videoclient.localParticipant.publishTrack((localVideoTrack)).then(publication =>{
          this.localvideotrack = localVideoTrack;
          let div = document.getElementById( this.videoclient.localParticipant.sid + 'vid');
          div.appendChild(localVideoTrack.attach());
          this.removenovideohtml(this.videoclient.localParticipant.sid);
          this.mms.getDevicesOnComputer();
          this.devicessubscription = this.mms.receiveDevices()
            .subscribe(
              (req: any)=>{
                this.devicesoptions = req;
              }
            );
        });
      });
    });
  }

and here is the end video code, this is in context:

endvideo(){
    if(this.localvideotrack != null){
      let div = document.getElementById( this.videoclient.localParticipant.sid + 'vid');
      this.localvideotrack.detach(div);
      this.localvideotrack.stop();
      this.videoclient.localParticipant.unpublishTrack(this.localvideotrack);
      this.addremovevideohtml(this.videoclient.localParticipant.sid);
    }
  }

now look what happens when I start and stop the video three times

2 blank chat boxes when there should only be the video

and the html of the picture above

the html

I have tried to add code to then manually remove the video elements after detaching the track but I have had no success. Wondering if you guys can help me. Thank you.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Twilio developer evangelist here.

The detach method actually returns the media element that the track was previously attached to, which allows you to then remove the media element from the page if you no longer need it.

You don't need to pass div to detach either, the video track is aware of the element it is attached to. This means you don't need to do the getElementById lookup either.

I'd recommend you update the endvideo function to the following:

endvideo(){
    if(this.localvideotrack != null){
      const mediaElements = this.localvideotrack.detach();
      mediaElements.forEach(mediaElement => mediaElement.remove());
      this.localvideotrack.stop();
      this.videoclient.localParticipant.unpublishTrack(this.localvideotrack);
      this.addremovevideohtml(this.videoclient.localParticipant.sid);
    }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Its amazing and yet really painful how easy it was to fix this.

Here is the endvideo() code unchaged:

endvideo(){
    if(this.localvideotrack != null){
      let div = document.getElementById( this.videoclient.localParticipant.sid + 'vid');
      this.localvideotrack.detach(div);
      this.localvideotrack.stop();
      
      this.videoclient.localParticipant.unpublishTrack(this.localvideotrack);
      this.addremovevideohtml(this.videoclient.localParticipant.sid);
    }

now here is the new endvideo() code:

endvideo(){
    if(this.localvideotrack != null){
      let div = document.getElementById( this.videoclient.localParticipant.sid + 'vid');
      this.localvideotrack.detach(div);
      this.localvideotrack.stop();
      div.innerHTML = '';
      this.videoclient.localParticipant.unpublishTrack(this.localvideotrack);
      this.addremovevideohtml(this.videoclient.localParticipant.sid);
    }
  }

this is the difference:

div.innerHTML = '';
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda