Search code examples
webrtccallpeerjs

I get an stream.getTracks is not a function error in PeerJS. How do i fix this?


so I have been using PeerJS to make a p2p chat application and it works when trying to chat, but when i try to call someone with the function below:

function callEm(id){
  call = peer.call(id,
    navigator.mediaDevices.getUserMedia({video: false, audio: true})
    );

  call.on('stream', function(stream) { // B
      window.remoteAudio.srcObject = stream; // C
      window.remoteAudio.autoplay = true; // D
      window.peerStream = stream; //E
      showConnectedContent(); //F    });
  })
  }

That Error

I get an error from PeerJS saying that e.getTracks is not a function e.getTracks is a code in the peerJS library: https://unpkg.com/[email protected]/dist/peerjs.min.js

I have been trying everything i find on the internet and I still get this error. I hope someone will be able to help me out.

Edit: I tried the cleaner version of the library( https://unpkg.com/[email protected]/dist/peerjs.js) and i get the error stream.getTracks() is not a function error


Solution

  • So I found the solution, the thing is my code was wrong :P

    so I created a function that calls a person with an Id. earlier it was:

    function callEm(id){
      console.log(id);
      console.log(navigator.mediaDevices.getUserMedia({video: false, audio: true}));
    
      call = peer.call(id,
        // window.localStream
        navigator.mediaDevices.getUserMedia({video: false, audio: true})
        );
    
      call.on('stream', function(stream) { // B
          window.remoteAudio.srcObject = stream; // C
          window.remoteAudio.autoplay = true; // D
          window.peerStream = stream; //E
          showConnectedContent(); //F    });
      })
      }
    

    and the issue was that I provided a promise as a MediaStream instead of the actual value. dumb mistake I know

    so after a friend in discord pointed that to me and helped me change it, the function became:

    function callEm(id){
      navigator.mediaDevices.getUserMedia({video: false, audio: true})
      .then(function(stream) {
            call = peer.call(id, stream);
            call.on('stream', function(stream) { // B
              window.remoteAudio.srcObject = stream; // C
              window.remoteAudio.autoplay = true; // D
              window.peerStream = stream; //E
              showConnectedContent(); //F    });
            })
        })
        .catch(function(err) {
          console.log("error: " + err);
        })
      );
      }
    

    and that fixed it. I didn't work out on answering the call yet, but this sent a call without any errors!

    I hope someone finds this useful.