navigator.getUserMedia({ 'video': { 'optional': [{ 'sourceId': 0 //0为前置摄像头,1为后置 }] }, 'audio': true }, successFunc, errorFunc),还是获取失败报错:NotSupported:Only secure origins are allowed(see:https://goo.gl/YOZkNV)
是在android还是ios上?webrtc的兼容问题可以看看这个项目 https://github.com/webrtc/adapter
用下面的代码测试一下
<video id="video" width="200" height="200" autoplay playsinline></video>
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
var constraints = {
audio: true,
video: true
};
navigator.mediaDevices.getUserMedia(constraints)
.then(function(stream) {
var video = document.querySelector('video');
video.srcObject = stream;
video.onloadedmetadata = function(e) {
video.play();
};
})
.catch(function(err) {
console.log (err);
});
}
else {
console.log ("navigator.mediaDevices not supported")
}