音頻約束參數
- volume 音量約束
- sampleRate: 采樣率
- sampleSize: 采樣大小,采樣的位數
- echoCancellation: 回音消除
- autoGaincontrol: 增加音量
- noiseSuppression: 降噪
- latency : 延遲大小
- channelCount: 切換聲道
- deviceID: 多個音頻輸入輸出設備的進行切換
- groupId: 同一個物理設備,是一個分組,但是輸入和輸出的id不一樣
音頻約束的案例
vim index.html
<html>
<head>
<title>WebRTC 獲取視頻和音頻</title>
</head>
<body>
<div>
<label>audio Source:</label>
<select id="audioSource"> </select>
</div>
<div>
<label>audio Output:</label>
<select id="audioOutput"> </select>
</div>
<div>
<label>video Source:</label>
<select id="videoSource"> </select>
</div>
<video autoplay playsinline id="player"></video>
<script src="http://webrtc.github.io/adapter/adapter-latest.js"></script>
<script src="./js/client.js"></script>
</body>
</html>
vim client.js
"use strict"
// 獲取設備
var audioSource = document.querySelector("select#audioSource");
var audioOutput = document.querySelector("select#audioOutput");
var videoSource = document.querySelector("select#videoSource");
// 查找視頻播放的標簽
var videoplay = document.querySelector("video#player");
function start(){
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia){
console.log("獲取音視頻方法不存在");
}else{
//---------------- 主要進行約束的地方- -----------------
var constraints = {
video : {
// 寬度在300 - 640之間進行自適應
width : {
min: 300,
max: 640,
},
height: 480,
frameRate: 15,
facingMode: enviroment, // 設置為前置攝像頭
deviceId : deviceId ? deviceId : undefined
},
audio : {
// 設置回音消除
noiseSuppression: true,
// 設置降噪
echoCancellation: true,
},
};
navigator.mediaDevices.getUserMedia(constraints)
.then(gotMediaStream).then(gotDevices)
.catch(handleError);
}
function gotMediaStream(stream){
// 復制流到video標簽
videoplay.srcObject = stream;
// 獲取流成功之后還可以繼續操作
return navigator.mediaDevices.enumerateDevices();
}
function handleError(err){
console.log("錯誤啦:", err)
}
function gotDevices(deviceInfos){
deviceInfos.forEach(function(deviceinfo){
var option = document.createElement("option");
option.text = deviceinfo.label;
option.value = deviceinfo.deviceId;
// 音頻輸入
if(deviceinfo.kind === "audioinput"){
audioSource.appendChild(option);
// 音頻輸出
}else if(deviceinfo.kind === "audiooutput"){
audioOutput.appendChild(option);
// 視頻輸入
}else if(deviceinfo.kind === "videoinput"){
videoSource.appendChild(option);
}
})
}
}
start();
videoSource.onchange = start;