基本格式
var promise = navigator.medisDevices.getUserMedia(constraints);
MediaStreamConstraints
dictionary MediaStreamConstraints{
(boolean or MediaTrackConstraints)video = false;
(boolean or MediaTrackConstraints)audio = false;
}
实战案例
vim index.html
<html>
<head>
<title>WebRTC 获取视频和音频</title>
</head>
<body>
<video autoplay playsinline id="player"></video>
<script src="./js/client.js"></script>
</body>
</html>
cd js
vim client.js
"use strict"
var videoplay = document.querySelector("video#player");
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia){
console.log("获取音视频方法不存在");
}else{
var constraints = {
video : true,
audio : true
};
navigator.mediaDevices.getUserMedia(constraints)
.then(gotMediaStream)
.catch(handleError);
}
function gotMediaStream(stream){
// 复制流到video标签
videoplay.srcObject = stream;
}
function handleError(err){
console.log("错误啦:", err)
}