HTML5音频缓冲区卡住 [英] HTML5 Audio Buffer getting stuck

查看:171
本文介绍了HTML5音频缓冲区卡住的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HTML5 webkitAudioContext通过以下代码获取用户麦克风的实时音量:

I'm using the HTML5 webkitAudioContext to get the realtime levels of a user's microphone using this following code:

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();

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

If you copy and paste this in to your console and click your fingers near your microphone (assuming you've enabled microphone input) you'll see it works for a few seconds, then suddenly stops. It doesn't report any errors. Can anybody explain why this is happening? Thanks

有关级别的示例,请参见 http://labs.dinahmoe.com/dynamicmusicengine/工作正常.

See http://labs.dinahmoe.com/dynamicmusicengine/ for an example where the levels are working correctly.

推荐答案

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

I had the same problem but finally got the solution! The problem is the javascript node cicle. I suggest you to change the createJavaScriptNode() first:

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

垃圾收集器的"levelChecker"变量及其onaudioprocess存在问题,因此您必须将脚本处理器或onaudioProcess回调绑定到窗口.这里是神圣的解决方案:

The garbage collector has a problem with "levelChecker" variable and his onaudioprocess , so you have to bind the scriptprocessor or the onaudioProcess callback to the window. Here the HOLY SOLUTION:

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

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

Just add window.audioProcess in that line and you will never deal with tat problem anymore.

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

Here you can find further info : http://lists.w3.org/Archives/Public/public-audio/2013JanMar/0304.html

希望对您有用!

这篇关于HTML5音频缓冲区卡住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆