Reputation: 2332
I'm trying to add audio chat to a game using WebRTC via PeerJS. I am successfully generating a unique ID for both peers, then passing these through a socket.io server and establishing a call. Once each client has each other's audio stream, I'm attaching this to a new <audio>
tag and appending it to the body. But there is no audible sound once the connection is made.
My code is as follows:
Creating peers, unique ids:
import Peer from 'peerjs';
import getUserMedia from 'getusermedia';
// ...
getUserMedia({ audio: true, video: false }, (err, stream) => {
if (err) return console.error(err);
console.log('Got audio stream!');
this.peer = new Peer();
this.peer.on('open', (id) => {
this.p2pAddress = id;
this.audioStream = stream;
joinBtn.setInteractive();
joinBtn.setAlpha(1);
});
});
Note this is all within a Phaser 3 game, so I'm assigning the p2pAddress
and audioStream
to the current scene, then passing them into the subsequent scene when the user clicks the join button (I've console logged them in the next scene though, and they are indeed being passed through).
Listening for incoming calls:
// Audio chat
this.peer.on('call', (call) => {
call.answer(this.audioStream);
console.log('Answered incoming call');
call.on('stream', (remoteStream) => {
console.log('Got stream', remoteStream);
const audio = document.createElement('audio');
audio.style.display = "none";
document.body.appendChild(audio);
audio.srcObject = remoteStream;
audio.play();
});
});
Initiating an audio stream:
// Initate audio stream
const call = this.peer.call(player.p2pAddress, this.audioStream);
console.log('Calling', player.p2pAddress);
console.log(this.audioStream);
call.on('stream', (remoteStream) => {
const audio = document.createElement('audio');
audio.style.display = "none";
document.body.appendChild(audio);
audio.srcObject = remoteStream;
audio.play();
});
In this case, player.p2pAddress
represents the id of the other peer. Again, I've console logged this and it is being transferred through no problem.
So what's actually happening is as follows:
p2pAddress
and initiates a call with their audioStream
.audioStream
.The console logs in the above code are firing off as if it's working, but I don't hear anything on either client device.
Things I've tried:
audioStream
to an <audio>
element on their own device, to verify that the stream is working. I heard feedback and sound, so the audio stream is good.MediaStream
.audio.play()
and audio.autoplay = true
on the <audio>
element.<audio> element was being attached to the
` with the inspector.https://
.Any insight would be greatly appreciated.
Upvotes: 1
Views: 1282
Reputation: 2332
NVM IT WORKS!
I was testing an old version on the https://
server, when I pushed the updates it works on the remote server. I guess it just doesn't work on localhost.
Upvotes: 1