# 2.12 Publisher.replaceTrack()

**URL:** <https://openvidu.discourse.group/t/2-12-publisher-replacetrack/320>\
**Category:** How to implement?\
**Tags:** v2\
**Created:** [March 27, 2020, 7:47am UTC](https://openvidu.discourse.group/t/2-12-publisher-replacetrack/320 "2020-03-27T07:47:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jan](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@Jan](https://openvidu.discourse.group/u/Jan)\
**Post date:** [March 27, 2020, 7:47am UTC](https://openvidu.discourse.group/t/2-12-publisher-replacetrack/320/1 "2020-03-27T07:47:37Z")

</div>

Hello,  
When using replaceTrack() in 2.12 to switch camera or share screen, after disconnecting the session, the camera and/or microphone is still in use.  
Is this intended, and if so, how to properly clean up?  
It works fine with the old solution of creating a new publisher and unpublish/publish, but I understood replaceTrack() was the way to go with 2.12.

---

<div class="post-metadata">

**Author:** ![CSantosM](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/csantosm/32/16_2.png) [@CSantosM](https://openvidu.discourse.group/u/CSantosM)\
**Post date:** [March 27, 2020, 9:06am UTC](https://openvidu.discourse.group/t/2-12-publisher-replacetrack/320/3 "2020-03-27T09:06:23Z")

</div>

Hello @Jan,

You’re right, When replaceTrack comes into play, the microphone and camera don’t release leaving the session.

We’ll fix it.

Thank you 🙂

---

<div class="post-metadata">

**Author:** ![pabloFuente](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/pablofuente/32/2536_2.png) [@pabloFuente](https://openvidu.discourse.group/u/pabloFuente)\
**Post date:** [March 28, 2020, 12:13pm UTC](https://openvidu.discourse.group/t/2-12-publisher-replacetrack/320/4 "2020-03-28T12:13:00Z")

</div>

Actually the behavior is fine. When calling replaceTrack, you must have obtained a MediaStreamTrack object somehow. Probably by using native Web API getUserMedia or using [OpenVidu.getUserMedia](https://openvidu.io/api/openvidu-browser/classes/openvidu.html#getusermedia). Developers are responsible of those MediaStreams and their inner MediaStreamTracks. So you must store it and properly dispose it whenever you want. If you don’t dispose your own MediaStreamTracks, then the camera light of the device won’t shut down.

Regards.

---

<div class="post-metadata">

**Author:** ![Jan](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@Jan](https://openvidu.discourse.group/u/Jan)\
**Post date:** [March 30, 2020, 6:03am UTC](https://openvidu.discourse.group/t/2-12-publisher-replacetrack/320/5 "2020-03-30T06:03:07Z")

</div>

Hey again @pabloFuente, I was struggling with disposing the tracks. I found that when replacing the video track, the audio tracks returned from getUserMedia were not handled. Thought I’d share my solution:

```
        .getUserMedia(constraints)
        .then(stream => {
            const track = stream.getVideoTracks()[0];
            publisher.replaceTrack(track);
            stream.getAudioTracks().forEach(t => t.stop()); 
        })
```

---

<div class="post-metadata">

**Author:** ![pabloFuente](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/pablofuente/32/2536_2.png) [@pabloFuente](https://openvidu.discourse.group/u/pabloFuente)\
**Post date:** [March 30, 2020, 6:55am UTC](https://openvidu.discourse.group/t/2-12-publisher-replacetrack/320/6 "2020-03-30T06:55:43Z")

</div>

If you are not gonna use the audio tracks, then you shouldn’t ask for them in the constraints. That should be the real solution.

Regards.
