gpt4 book ai didi

html5-audio - HTML5 音频缓冲区卡住

转载 作者:行者123 更新时间:2023-12-04 02:59:18 25 4
gpt4 key购买 nike

我使用 HTML5 webkitAudioContext 通过以下代码获取用户麦克风的实时电平:

var liveSource;

function getLevel(){
var context = new webkitAudioContext();

navigator.webkitGetUserMedia({audio: true}, function(stream) {

liveSource = context.createMediaStreamSource(stream);
liveSource.connect(context.destination);

var levelChecker = context.createJavaScriptNode(4096, 1 ,1);
liveSource.connect(levelChecker);

levelChecker.connect(context.destination);

levelChecker.onaudioprocess = function(e) {

var buffer = e.inputBuffer.getChannelData(0);


var maxVal = 0;
// Iterate through buffer to check if any of the |values| exceeds 1.
for (var i = 0; i < buffer.length; i++) {
if (maxVal < buffer[i]) {
maxVal = buffer[i];
}
}
if(maxVal <= 0.01){
console.log(0.0);
} else if(maxVal > 1){
console.log(1);
} else if(maxVal > 0.2){
console.log(0.2);
} else if(maxVal > 0.1){
console.log(0.1);
} else if(maxVal > 0.05){
console.log(0.05);
} else if(maxVal > 0.025){
console.log(0.025);
} else if(maxVal > 0.01){
console.log(0.01);
}
};
});

}

getLevel();

如果您将其复制并粘贴到您的控制台,然后用手指在麦克风附近单击(假设您已启用麦克风输入),您会看到它工作了几秒钟,然后突然停止。它不报告任何错误。谁能解释为什么会这样?谢谢

参见 http://labs.dinahmoe.com/dynamicmusicengine/举个关卡正常工作的例子。

最佳答案

我遇到了同样的问题,但终于找到了解决方案!问题是 javascript 节点 cicle。我建议您先更改 createJavaScriptNode():

var levelChecker = context.createScriptProcessor(4096, 1 ,1);

垃圾收集器的“levelChecker”变量和他的onaudioprocess 有问题,因此您必须将脚本处理器或onaudioProcess 回调绑定(bind)到窗口。这是神圣的解决方案:

 levelChecker.onaudioprocess = window.audioProcess = function(e) { ...

只需在该行中添加 window.audioProcess,您将永远不会再处理 tat 问题。

在这里您可以找到更多信息:http://lists.w3.org/Archives/Public/public-audio/2013JanMar/0304.html

希望对你有用!

关于html5-audio - HTML5 音频缓冲区卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15900103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com