【发布时间】:2021-10-30 01:42:05
【问题描述】:
我正在尝试让我的笔记本电脑的扬声器音量显示在我的应用程序中。我是 WebRTC 和 Web Audio API 的新手,所以只是想确认一个功能的可能性。该应用程序是一个电子应用程序并具有呼叫功能,因此当呼叫另一端的用户说话时,该应用程序应显示一个输出电平,该电平会根据声音而变化。我尝试过使用 WebRTC 和 Web Audio API,甚至看到了sample。我可以记录值,但是当我对着麦克风讲话时,情况会发生变化,而我只需要扬声器的值而不是麦克风。
export class OutputLevelsComponent implements OnInit {
constructor() { }
ngOnInit(): void {
this.getAudioLevel()
}
getAudioLevel() {
try {
navigator.mediaDevices.enumerateDevices().then(devices => {
console.log("device:", devices);
let constraints = {
audio : {
deviceId: devices[3].deviceId
}
}
navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
console.log("stream test: ", stream);
this.handleSuccess(stream)
});
});
} catch(e) {
console.log("error getting media devices: ", e);
}
}
handleSuccess(stream: any) {
console.log("stream: ", stream);
var context = new AudioContext();
var analyser = context.createScriptProcessor(1024, 1, 1);
var source = context.createMediaStreamSource(stream);
source.connect(analyser);
// source.connect(context.destination);
analyser.connect(context.destination);
opacify();
function opacify() {
analyser.onaudioprocess = function(e) {
// no need to get the output buffer anymore
var int = e.inputBuffer.getChannelData(0);
var max = 0;
for (var i = 0; i < int.length; i++) {
max = int[i] > max ? int[i] : max;
}
if (max > 0.01) {
console.log("max: ", max);
}
}
}
}
}
我已经尝试了上面的代码,我使用 enumerateDevices() 和 getUserMedia() 这将提供一组设备,出于演示目的,我将使用最后一个具有“音频输出”作为 kind 属性和访问设备流的值的设备。
请让我知道这是否可以通过 Web Audio API 实现。如果没有,是否有任何其他工具可以帮助我实现此功能?
提前致谢。
【问题讨论】:
标签: javascript angular electron webrtc web-audio-api