[英]WebRTC change microphone or webcam while in call
I am trying to figure out how to change the microphone or webcam while you are in a videochat with someone.我想弄清楚当您与某人进行视频聊天时如何更换麦克风或网络摄像头。
I have been now trying for a few days and nothing works.我现在已经尝试了几天,但没有任何效果。 I was following this example , but it seems it is much harder to achieve the change while someone is already connected.我正在关注这个例子,但似乎在有人已经连接时实现更改要困难得多。
The issues I have: If I change the mic the sound is lost/the mic doesnt react at all.我遇到的问题:如果我更换麦克风,声音会丢失/麦克风根本没有反应。 I also cannot change it back to the default.我也无法将其更改回默认值。
A similar thing happens if I change the webcam.如果我更换网络摄像头,也会发生类似的事情。 The stream hangs, the last frame is seen. stream 挂起,看到最后一帧。
I get no error message, in fact it tells me that the changes were successful.我没有收到错误消息,实际上它告诉我更改成功。
Changing the webcam/mic WORKS before the call is established在通话建立之前更改网络摄像头/麦克风
Here is the relevant codeblock.这是相关的代码块。 Everywhere I am reading just create new constraints and give the desired deviceId to the audio/video stream.:我正在阅读的任何地方都只是创建新的约束并将所需的 deviceId 提供给音频/视频 stream。:
function ChangeDevice() {
if (localStream) {
localStream.getTracks().forEach(track => {
track.stop();
});
}
var audioSource = audioInputSelect.value;
var videoSource = videoSelect.value;
console.log(videoSource);
console.log(audioSource);
const newConstraints = {
audio: {deviceId: audioSource ? {exact: audioSource} : undefined},
video: {deviceId: videoSource ? {exact: videoSource} : undefined}
};
navigator.mediaDevices.getUserMedia(newConstraints).then(gotStream).then(gotDevices).catch(handleError);
}
function gotStream(stream) {
console.log('Adding local stream.');
localStream = stream;
localVideo.srcObject = stream;
sendMessage(['got user media', room]);
if (isInitiator) {
maybeStart();
}
return navigator.mediaDevices.enumerateDevices(); // I added this
}
I think these two are the relevant functions, ChangeDevice is called when I select a new device from a dropdown.我认为这两个是相关的函数,当我从下拉列表中 select 一个新设备时调用 ChangeDevice。 The id's are correct.身份证是对的。
Here is the whole code I use:这是我使用的整个代码:
Luckily replaceTrack
seems to work now on all browsers, so there is no need to renegotiate.幸运的是, replaceTrack
现在似乎可以在所有浏览器上运行,因此无需重新协商。
I had to edit my gotStream
function like this:我不得不像这样编辑我的gotStream
function:
function gotStream(stream) {
// If already started
// Need this if webcam or mic changes
if (isStarted) {
var videoTrack = stream.getVideoTracks()[0];
var audioTrack = stream.getAudioTracks()[0];
var sender = pc.getSenders().find(function(s) {
return s.track.kind == videoTrack.kind;
});
var sender2 = pc.getSenders().find(function(s) {
return s.track.kind == audioTrack.kind;
});
console.log('found sender:', sender);
sender.replaceTrack(videoTrack);
sender2.replaceTrack(audioTrack);
localStream = stream;
localVideo.srcObject = stream;
} else {
console.log('Adding local stream.');
localStream = stream;
localVideo.srcObject = stream;
sendMessage(['got user media', room]);
if (isInitiator) {
maybeStart();
}
}
return navigator.mediaDevices.enumerateDevices(); // I added this
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.