admin管理员组文章数量:1290232
I am playing around with HTML Media Capture and the getUserMedia
method. It is not working with Chrome and I get the alert included on failure.
Here is the sample code I used:
if (navigator.getUserMedia) {
navigator.getUserMedia(
// constraints
{
video: true,
audio: true
},
// successCallback
function (localMediaStream) {
var video = document.querySelector('video');
video.src = window.URL.createObjectURL(localMediaStream);
// Do something with the video
video.play();
},
// errorCallback
function (err) {
console.log("The following error occured: " + err);
}
);
} else {
alert("getUserMedia not supported by your web browser or Operating system version");
}
I am playing around with HTML Media Capture and the getUserMedia
method. It is not working with Chrome and I get the alert included on failure.
Here is the sample code I used:
if (navigator.getUserMedia) {
navigator.getUserMedia(
// constraints
{
video: true,
audio: true
},
// successCallback
function (localMediaStream) {
var video = document.querySelector('video');
video.src = window.URL.createObjectURL(localMediaStream);
// Do something with the video
video.play();
},
// errorCallback
function (err) {
console.log("The following error occured: " + err);
}
);
} else {
alert("getUserMedia not supported by your web browser or Operating system version");
}
Share
Improve this question
edited Nov 9, 2016 at 3:56
octavn
3,28535 silver badges51 bronze badges
asked Nov 29, 2015 at 17:36
csoueidicsoueidi
4182 gold badges5 silver badges17 bronze badges
2
- For the correct HTML Media Capture syntax (there were several revisions) see addpipe./blog/correct-syntax-html-media-capture – octavn Commented May 25, 2017 at 11:28
- Use the latest version... navigator.getUserMedia() is now replaced by navigator.mediaDevices.getUserMedia webrtc.github.io/samples – BLoB Commented Feb 27, 2019 at 15:23
4 Answers
Reset to default 4The standard navigator.getUserMedia is not recognized on Chrome. it works with Microsoft Edge. You will need to add vendor prefixes.
for Chrome: navigator.webkitGetUserMedia
Here is a working code on JSFiddle https://jsfiddle/RamiSarieddine/t9d3hpyr/
//browser support check "ms" vendor function is for IE8
navigator.getUserMedia = ( navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia );
if (navigator.getUserMedia) {
navigator.getUserMedia(
// constraints
{
video: true,
audio: true
},
// successCallback
function (localMediaStream) {
var video = document.querySelector('video');
video.src = window.URL.createObjectURL(localMediaStream);
// Do something with the video
video.play();
},
// errorCallback
function (err) {
console.log("The following error occured: " + err);
}
);
} else {
alert("getUserMedia not supported by your web browser or Operating system version");
}
navigator.getUserMedia
has been superseded by navigator.mediaDevices.getUserMedia
.
The latter uses modern promises and is available natively in Edge, Firefox, and Chrome. There is also adapter.js, the official WebRTC polyfill that helps them catch up to the standard (e.g. srcObject
).
Here is a fiddle that works in all three: https://jsfiddle/srn9db4h/
var constraints = { video: true, audio: true };
navigator.mediaDevices.getUserMedia(constraints)
.then(stream => video.srcObject = stream)
.catch(e => console.error(e));
It is not working with Chrome
Try using webkit
, moz
prefixes , see Navigator.getUserMedia()
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia;
if (navigator.getUserMedia) {
//do stuff
}
What about this little validation?
async getMediaStream(constraints): Promise<MediaStream> {
return new Promise(function (resolve, reject) {
if (navigator.mediaDevices
&& navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia(constraints)
.then((stream) => resolve(stream))
.catch(err => reject(err));
} else {
const getUserMedia = navigator.getUserMedia
|| navigator['webkitGetUserMedia']
|| navigator['mozGetUserMedia']
|| navigator['msGetUserMedia'];
getUserMedia(
constraints,
(stream) => resolve(stream),
(err) => reject(err)
);
}
});
}
const isEdge = navigator.userAgent.indexOf('Edge') !== -1
&& (!!navigator.msSaveOrOpenBlob || !!navigator.msSaveBlob);
getMediaStream({
audio: isEdge ? true : {
echoCancellation: false
}
})
.then(stream => console.log(stream))
.catch(err => console.error(err))
Regards, Nicholls :)
本文标签: javascriptgetUserMedia not working on new browsersStack Overflow
版权声明:本文标题:javascript - getUserMedia not working on new browsers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741494421a2381770.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论