前端录音功能,本地麦克风加系统声音

<button id="start">开始录音</button>
<button id="stop">停止录音</button>

<script>

let recorder
let chunks = []

document.getElementById("start").onclick = async () => {

  // 系统声音
  const screenStream = await navigator.mediaDevices.getDisplayMedia({
    video: true,
    audio: true
  })

  // 麦克风
  const micStream = await navigator.mediaDevices.getUserMedia({
    audio: true
  })

  const audioContext = new AudioContext()

  // 创建输入源
  const systemSource = audioContext.createMediaStreamSource(screenStream)
  const micSource = audioContext.createMediaStreamSource(micStream)

  // 创建混音输出
  const destination = audioContext.createMediaStreamDestination()

  // 连接到混音器
  systemSource.connect(destination)
  micSource.connect(destination)

  // 最终流
  const mixedStream = new MediaStream([
    ...destination.stream.getAudioTracks()
  ])

  recorder = new MediaRecorder(mixedStream)

  recorder.ondataavailable = e => chunks.push(e.data)

  recorder.onstop = () => {

    const blob = new Blob(chunks,{type:"audio/webm"})
    const url = URL.createObjectURL(blob)

    const a = document.createElement("a")
    a.href = url
    a.download = "meeting.webm"
    a.click()
  }

  recorder.start()

}

// stop.onclick = ()=>recorder.stop()
document.getElementById("stop").onclick = () => {
  recorder.stop()
}
</script>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容