【发布时间】:2021-12-29 10:34:59
【问题描述】:
我正在构建一个 Angular 应用程序,它允许 2 个用户使用 Openvidu 通话解决方案互相视频通话。
在这个应用程序中,我开发了一项功能,可以更改您在通话中积极使用的摄像头或麦克风。
一旦选择了新的麦克风,之前的麦克风轨道就会停止并从流中删除,然后再添加新的麦克风。这个过程封装在下面的代码中:
async onMicrophoneSelected(event: any) {
var currentMediaStream: MediaStream = this.localUsersService.getWebcamPublisher().stream.getMediaStream();
var currentAudioTrack: MediaStreamTrack;
var currentVideoTrack: MediaStreamTrack;
var newAudioInfo: MediaDeviceInfo; // type here is MediaDeviceInfo as it will be specified from enumerateDevices()
var newAudioTrack: MediaStreamTrack;
var newVideoTrack: MediaStreamTrack;
// Specifying current video & audio track being used on call
currentMediaStream.getTracks().forEach((track) => {
if (track.kind === 'audio') {
currentAudioTrack = track;
currentAudioTrack.stop(); // stopping old audio track here
}
if (track.kind === 'video') {
currentVideoTrack = track;
}
});
// Looping through available devices
await navigator.mediaDevices.enumerateDevices().then((res) => {
res.forEach((device) => {
// Checking for: the current inactive device
if (device.kind === 'audioinput' && device.deviceId === event.value) {
newAudioInfo = device;
}
});
});
// Passing constraints that contain new deviceId for audio, then using replaceTrack() to replace audio // this also promps user for new device permissions
await navigator.mediaDevices.getUserMedia({ audio: { deviceId: { exact: newAudioInfo.deviceId } } }).then((stream) => {
newAudioTrack = stream.getAudioTracks()[0];
});
// replaceTrack() used here to notify OpenVidu of new devices, where they will then be published and thus changes also seen by the other-end-user
this.localUsersService
.getWebcamPublisher()
.replaceTrack(newAudioTrack)
.then(() => {
console.log(currentMediaStream.getTracks(), '<<<-- checking stream after changes');
});
}
上述代码成功运行后,最终结果应该是我在通话中主动使用的麦克风应该已更改为我选择的那个。
情况确实如此,但我面临的问题是,这种变化也伴随着我自己的非常响亮的回声,这意味着一旦我切换麦克风,活动麦克风就会改变,我也可以通过那个麦克风听到自己的声音。
非常感谢您对此的任何想法,感谢您的阅读。
注意:echoCancellation 没有解决这个问题。
【问题讨论】:
-
您好,您是否已经在不同的浏览器中测试过?如果结果不同?您能否测试一下注释掉 currentAudioTrack.stop() 行,因为 OV 的 Publisher.replaceTrack() 方法具有相同的功能。
-
我可以建议先不要将 async/await 与 then 混合使用吗?它会解开你的代码中很多不清楚的东西(至少对我来说)。也看看developer.mozilla.org/en-US/docs/Web/API/RTCRtpSender/… - 应该非常相似。
标签: angular typescript webrtc openvidu