Replace track to switch camera causes audio input to stop working or echo

So i’ve posted this before but wanted to update things. When i replace the video track to the back camera on iphone the camera switches but the audio input stops working.

So i tried to also replace the audio track with the microphone it was originally using (there is only one the phone) but when i do that the input works but echos back through the publisher video.

Baring in mind i am subscribing to the publisher to see themselves, it’s almost as though the audio on the video for the publisher seeing themselves becomes unmuted. I tried another fix to mute the publisher video but it doesn’t work

I am yet. find a solution.

UPDATE

So to replace the track:

this.OVPublisher.replaceTrack(audioTrack).then(() => {
            console.log("AUDIO TRACK REPLACED")
            this.OVPublisher.videoReference.setAttribute('muted',true);
            this.OVPublisher.videoReference.muted = true;

          }).catch(()=>{
            this.toast.error('There was a problem activating your microphone.');
          })

Even with telling the video reference to mute it still echos. If i take this out and just replace the video track to switch camera, there is no sound input at all anymore, even switching the camera back to the original there is then no sound input.

UPDATE 2

I’ve just noticed this on the publisher video tag:

id="local-video-undefined"

Could this be the issue? It can’t find the video reference because it hasn’t got the stream id, it says undefined? Why is it undefined?

So to create the publisher i contact the server (php) and make a new session with vidu via the REST api and return the publisher token, then connect on the front end:

this.OVSession.connect(publisherToken)

This is the full code to switch camera, if i don’t replace the audio track it just goes dead with no mic input, but with it i get the echo, it doesn’t make sense.

this.OV.getDevices().then((devices)=> {

        let videoDevices = devices.filter(device => device.kind === 'videoinput' && device.label !== 'Snap Camera');
        let audioDevices = devices.filter(device => device.kind === 'audioinput');

        let frontDeviceId = videoDevices[0].deviceId;
        let backDeviceId = videoDevices[1].deviceId;


        this.OV.getUserMedia(<any>{
          videoSource: this.OVCurrentCamera === 'front' ? backDeviceId : frontDeviceId,
          audioSource: typeof audioDevices[0] !== 'undefined' ? audioDevices[0].deviceId : true
        }).then((mediaStream) =>  {

          let videoTrack = mediaStream.getVideoTracks()[0];
          let audioTrack = mediaStream.getAudioTracks()[0];

          this.OVPublisher.replaceTrack(videoTrack).then(() => {
            this.OVCurrentCamera = this.OVCurrentCamera === 'front' ? 'back' : 'front';
          }).catch(() => {
            this.toast.error('There was a problem switching cameras.');
          })

          this.OVPublisher.replaceTrack(audioTrack).then(() => {
            this.OVPublisher.videoReference.setAttribute('muted',true);
            this.OVPublisher.videoReference.muted = true;

          }).catch(()=>{
            this.toast.error('There was a problem activating your microphone.');
          })
        }).catch(() => {
          this.toast.error('Could not access devices');
        })

      })