let audioPlayer = document.querySelector('audio#audioPlayer');
let gotMediaStream = (stream) => {
// stream 流里面只包含 音频轨
audioPlayer.srcObject = stream
}
let handleError = (err) => {
console.log(`getUserMedia errpr:${err}`)
}
let start = () => {
if(!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
console.log('getUserMedia is not supported!');
}else {
// 仅有音频数据
let constrants = {
video: false,
audio: true,
}
navigator.mediaDevices.getUserMedia(constrants)
.then(gotMediaStream)
.catch(handleError)
}
}
start()
<html>
<head>
<title>WebRTC capture video and audio</title>
</head>
<body>
<audio autoplay controls id="audioPlayer"></audio>
<script src='./client.js'></script>
</body>
</html>
打开index.html页面显示结果
点击暂停,再次点击可继续录制音频
生成录制的音频文件,以后再更新