这是我的代码。
captureUserMedia(mediaConstraints, successCallback, errorCallback) {
navigator.getUserMedia(mediaConstraints, successCallback, errorCallback);
}
captureUserMedia00(callback){
captureUserMedia({
audio: true,
video: true
}, function(stream) {
console.log('user media Callback', stream);
callback(stream);
}, function(error) {
console.log('user media Error ', JSON.stringify(error));
});
}});
}
这里,当用户尚未允许视频约束时,但已经允许音频(由于其他刚刚录制的音频),并且当提示相同时,用户'关闭'提示,则调用successCallback,然后我不会获得VideoStream,只是音频。
如何在调用successCallback时确保允许视频和音频权限?
答案 0 :(得分:2)
这实际上是Chrome和Firefox中的一个错误,因为在这种情况下should会调用errorCallback
。它已在Firefox Developer Edition(49)中修复。
作为解决方法,请检查您是否有两个曲目:
if (stream.getTracks().length < 2) {
throw new Error("Need both");
}
polyfill可能看起来像这样(在Chrome中使用https fiddle):
let getUserMedia = constraints =>
navigator.mediaDevices.getUserMedia(constraints).then(stream => {
if (constraints.audio && !stream.getAudioTracks().length ||
constraints.video && !stream.getVideoTracks().length) {
stream.getTracks().forEach(track => track.stop());
throw new DOMException("The object can not be found here.",
"NotFoundError");
}
return stream;
});
getUserMedia({ video: true, audio: true })
.then(stream => video.srcObject = stream)
.catch(e => console.log(e.name + ": "+ e.message));
<video id="video" width="160" height="120" autoplay></video>