var getUser_media = function (constraints, success, errFunc) {
if (navigator.mediaDevices.getUserMedia) {
//最新的标准API
navigator.mediaDevices.getUserMedia(constraints).then(success).catch(errFunc);
} else if (navigator.webkitGetUserMedia) {
//webkit核心浏览器
navigator.webkitGetUserMedia(constraints,success, errFunc)
} else if (navigator.mozGetUserMedia) {
//firfox浏览器
navigator.mozGetUserMedia(constraints, success, errFunc);
} else if (navigator.getUserMedia) {
//旧版API
navigator.getUserMedia(constraints, success, errFunc);
}
};
await getUser_media(constraints, gotLocalMediaStream, function(err) {
console.log('getUserMedia 错误', err);
//创建点对点连接对象
});
获取到媒体文件流后
// getUserMedia 获得流后,将音视频流展示并保存到 localStream
function gotLocalMediaStream(mediaStream) {
console.log('mediaStream', mediaStream);
localStream = mediaStream;
//https://blog.csdn.net/chendongpu/article/details/123105777
// 旧的浏览器可能没有srcObject
if ("srcObject" in videoPlayer) {
videoPlayer.srcObject = mediaStream;
} else {
//避免在新的浏览器中使用它,因为它正在被弃用。
videoPlayer.src = window.URL.createObjectURL(mediaStream);
}
videoPlayer.onloadedmetadata = function (e) {
videoPlayer.play();
};
videoPlayer.play();
lrBox.noLoading();
if(funcAfterDevice) {
funcAfterDevice();
}
}