Javascript 在webRTC中切换摄像头
如何在多连接中切换摄像头 我去拿离婚名单和他们的身份证Javascript 在webRTC中切换摄像头,javascript,camera,webrtc,rtcmulticonnection,Javascript,Camera,Webrtc,Rtcmulticonnection,如何在多连接中切换摄像头 我去拿离婚名单和他们的身份证 var videoDevices = document.getElementById('video-devices'); var audioDevices = document.getElementById('audio-devices'); connection.DetectRTC.load(function() { connection.DetectRTC.audi
var videoDevices = document.getElementById('video-devices');
var audioDevices = document.getElementById('audio-devices');
connection.DetectRTC.load(function() {
connection.DetectRTC.audioInputDevices.forEach(function(device) {
var option = document.createElement('option');
option.innerHTML = device.label;
option.value = device.id;
document.querySelector('select').appendChild(option);
});
connection.DetectRTC.videoInputDevices.forEach(function(device) {
var option = document.createElement('option');
option.innerHTML = device.label;
option.value = device.id;
videoDevices.appendChild(option);
});
});
并试图通过以下方式改变相机。把这个从
但没用的相机根本不会改变
我尝试了很多方法,但都失败了
下面是一个关于
这个例子可能对我很有帮助,但与RTCMulticonnection的集成让人困惑。如果你想前后切换摄像头,在RTCMulticonnection中有一个解决方案 如果您想切换摄像头(即前/后),这里有一个解决方案 我从GitHub复制了这个。 请不要忘记添加
希望这个答案对您有所帮助。您能提供一个答案吗?感谢you@DanieleRicci谢谢你宝贵的回复。我添加了一个链接到codpen@DanieleRicci我更新了问题,请你检查一下。可能是参考帮助注意:这不会影响后来加入的人,这只会将曲目替换为当前连接的对等方。
document.getElementById('switch-camera').onclick = function() {
var videoSourceId = videoDevices.value;
connection.codecs.video = 'VP8';
connection.bandwidth = { // all values in kbits/per/seconds
audio: 192,
video: 512,
screen: 512
};
connection.stopMediaAccess();
setTimeout(() => {
connection.mediaConstraints.video.optional = [{
sourceId: videoSourceId
}];
connection.addStream({audio: true, video: true});
},2000);
};
function openOrJOinRoom(roomid) {
connection.mediaConstraints = {
audio: true,
video: {
facingMode: {exact : 'environment'}
}
};
connection.openOrJoin(roomid);
}
function selectFrontCameraDuringActiveSession() {
// we need to stop previous video tracks because
// mobile device may not allow us capture
// both front and back camera streams
// at the same time
connection.attachStreams.forEach(function(stream) {
// stop only video tracks
// so that we can recapture video track
stream.getVideoTracks().forEach(function(track) {
track.stop();
});
});
var mediaConstraints = {
audio: false, // NO need to capture audio again
video: {
facingMode: {exact : 'user'}
}
};
navigator.mediaDevices.getUserMedia(mediaConstraints).then(function(frontCamera) {
var frontCameraTrack = frontCamera.getVideoTracks()[0];
connection.getAllParticipants().forEach(function(pid) {
connection.peers[pid].peer.getSenders().forEach(function(sender) {
if (sender.track.kind == 'video') {
sender.replaceTrack(frontCameraTrack);
}
});
});
});
}
function selectBackCameraDuringActiveSession() {
// we need to stop previous video tracks because
// mobile device may not allow us capture
// both front and back camera streams
// at the same time
connection.attachStreams.forEach(function(stream) {
// stop only video tracks
// so that we can recapture video track
stream.getVideoTracks().forEach(function(track) {
track.stop();
});
});
var mediaConstraints = {
audio: false, // NO need to capture audio again
video: {
facingMode: {exact : 'environment'}
}
};
navigator.mediaDevices.getUserMedia(mediaConstraints).then(function(frontCamera) {
var frontCameraTrack = frontCamera.getVideoTracks()[0];
connection.getAllParticipants().forEach(function(pid) {
connection.peers[pid].peer.getSenders().forEach(function(sender) {
if (sender.track.kind == 'video') {
sender.replaceTrack(frontCameraTrack);
}
});
});
});
}