javascript - 使用 Javascript 仅录制系统音频或麦克风音频
问题描述
有没有办法只用 Javascript 录制系统音频?我似乎找不到任何东西,所以欢迎任何指针。
我知道 UserMedia 和 Recorder.js,但它同时记录系统音频和麦克风音频,并且可以分开吗?有什么配置吗?
对于任何想看看的人:
$(function () {
const upworkRecorder = {
rec: {
context: null,
stream: null,
recorder: null,
},
init: function () {
if (this.rec.context === null) {
this.rec.context = new (AudioContext || webkitAudioContext)();
console.log(this.rec.context);
}
},
start: function () {
var onlyAudio = {
audio: {
sampleRate: 52000,
channelCount: {
ideal: 2,
min: 1,
},
noiseSuppression: true,
autoGainControl: true,
echoCancellation: true,
volume: 1.0,
},
video: false,
};
navigator.mediaDevices
.getUserMedia(onlyAudio)
.then(s => {
console.log(s);
this.rec.stream = s;
this.rec.recorder = new Recorder(
this.rec.context.createMediaStreamSource(s),
{ numChannels: 2 }
);
this.rec.recorder.record();
})
.catch(err => {
console.log(err.message);
});
},
stop: function () {
if (this.rec.stream !== null) {
this.rec.stream.getAudioTracks()[0].stop();
}
if (this.rec.recorder !== null) {
this.rec.recorder.stop();
}
this.rec.recorder.exportWAV(blob => {
let url = (window.URL || window.webkitURL).createObjectURL(blob);
console.log(blob, url);
const audio = document.createElement("div");
audio.innerHTML = `<h1>Your recording</h1><audio controls src=${url}></audio>`;
document.body.append(audio);
});
},
};
$("#start-rec").on("click", e => {
upworkRecorder.init();
upworkRecorder.start();
});
$("#pause-rec").on("click", e => {
upworkRecorder.stop();
});
});
解决方案
推荐阅读
- javascript - 输入字段未更新数组中的“地址”
- vue.js - 如何向 vue 中的 getter 发送参数?
- javascript - 禁用 Angular 2 多选复选框
- c - 单次使用后在宏内部取消定义常量
- javascript - 将 javascript 对象键更改为属性值
- amazon-cloudfront - 如何在 AWS 中部署受 OpenId Connect 保护的网站?
- c - 命令行参数值检查和退出代码
- verilog - Verilog:条件分支
- python - 提高 np.fromfuction 的性能
- jpa - 如何使用自动生成的主键(也是另一个表的外键)使用 JPA 插入数据?