myStream.getAudioTracks is not a Function? myStream is not regconized as MediaStream but a String?

73 views Asked by At

I’m facing an issue when muting the microphone using myStream.getAudioTracks. The problem is that I've already assigned a MediaStream to myStream before but it seems that myStream variable is still being treated as a string causing the error: myStream.getAudioTracks is not a function

var myStream='';

function getAndSetUserStream() {
    h.getUserFullMedia().then( ( stream ) => {
        //save my stream
        myStream = stream;
        h.setLocalStream( stream );
    } ).catch( ( e ) => {
        console.error( `stream error: ${ e }` );
    } );
  }

I'm working on the WebRTC and Socket.io video conference app. When one user joins the room, it will call the getAndSetUserStream() first and handle toggleMute whenever the user click on the mic button. So, it might not the timing issue.

 const toggleMute = () => {
    
      const audioTracks = myStream.getAudioTracks();
      if (audioTracks.length > 0) {
        // Toggle the microphone state
        audioTracks[0].enabled = !audioTracks[0].enabled; 
        // Update the state based on the enabled state
        setIsMuted(!audioTracks[0].enabled); 
      }
  };

Any tips to fix this? Thanks

0

There are 0 answers