使用网络音频 API 和 navigator.getUserMedia 在 Android 版 Chrome 中录制音频 [英] Recording audio in Chrome for Android using web audio API and navigator.getUserMedia

查看:43
本文介绍了使用网络音频 API 和 navigator.getUserMedia 在 Android 版 Chrome 中录制音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 4.1 上的 Android 版本 30 和 31 Beta 版 Chrome 似乎无法使用 HTML5 网络音频和 navigator.webkitGetUserMedia 正确录制音频.(Android 上的 Chrome 30+ 应该支持这些 API.)

Chrome for Android versions 30 and 31 beta on Android 4.1 do not appear to correctly record audio using HTML5 web audio and navigator.webkitGetUserMedia. (Chrome 30+ on Android is supposed to support these APIs.)

症状是代码看起来工作正常,包括显示是否允许麦克风访问的提示,但记录的数据只包含零.

The symptom is that the code appears to work correctly, including displaying the prompt for whether or not to allow microphone access, but recorded data contains nothing but zeros.

我创建了一个简化的测试用例(转到 http://jsfiddle.net/JCFtK/,然后单击录制按钮,然后选择适当的选项以允许它访问您的麦克风).代码的关键部分如下('record'函数是入口点).

I created a simplified testcase (go to http://jsfiddle.net/JCFtK/, and click Record button, then choose the appropriate option to allow it to access your microphone). The key part of the code is below ('record' function is the entry point).

function processAudioBuffer(e) {
    var floats = e.inputBuffer.getChannelData(0);
    var min = 0, max = 0;
    for (var i = 0; i < floats.length; i++) {
        var current = floats[i];
        min = Math.min(current, min);
        max = Math.max(current, max);
    }
    log('Block min/max: ' + min.toFixed(3) + ', ' + max.toFixed(3));
}

function record() {
    if (!window.AudioContext) {
        window.AudioContext = window.webkitAudioContext;
    }
    if (!navigator.getUserMedia) {
        navigator.getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
    }
    data.context = new AudioContext();
    navigator.getUserMedia({audio: true}, function(stream) {
        data.mediaStream = stream;
        startAudio();
    }, function(error) {
        log('Stream get error: ' + error);
    });
}

function startAudio() {
    // Get input media stream.
    data.audioStream = data.context.createMediaStreamSource(data.mediaStream);

    // Set up JS processor.
    data.processor = data.context.createScriptProcessor(2048, 1, 1);
    data.processor.onaudioprocess = processAudioBuffer;

    // You need to connect the output of processor to the audio output or it
    // doesn't work. (In Firefox, connecting it just to a gain node works;
    // in Chrome you have to connect to output like this.)
    data.processor.connect(data.context.destination);

    // Connect input stream to processor, then we're done.
    data.audioStream.connect(data.processor);
    log('Stream connected OK');
}

该测试显示了每个记录块的最小/最大音频值.当它工作时——例如它在桌面 Chrome 中的工作——你会看到最小值/最大值根据背景噪音而变化.当它不工作时,这些值总是 0.000.

The test shows the min/max audio value from each recorded block. When it is working - as it does in desktop Chrome, for instance - you'll see the min/max values vary depending on background noise. When it is not working, these values are always 0.000.

此测试用例适用于以下情况:

This testcase WORKS in the following:

  • Chrome 30 (Windows)
  • Firefox 25 (Windows)
  • 索尼 Xperia M 上的 Firefox 25 (Android 4.1)
  • 三星平板电脑上的 Firefox 25 (Android 4.1)

此测试用例不适用于以下情况:

This test case DOES NOT WORK in the following:

  • 索尼 Xperia M 上的 Chrome 30 (Android 4.1)
  • 索尼 Xperia M 上的 Chrome beta 31 (Android 4.1)
  • 三星平板电脑上的 Chrome 30 (Android 4.1)

该测试用例也不适用于其他浏览器,例如 Safari 或 IE,但这是意料之中的,因为它们不支持必要的 API.我希望只有 Android 版 Chrome 可以实现这项功能,但事实并非如此.

The testcase also doesn't work in other browsers such as Safari or IE, but that's expected as they do not support the necessary APIs. It's only Chrome for Android that I was hoping to make this work on, but it doesn't.

我已使用 Chrome 问题报告流程(这是私人的,可能是一个黑洞)将此作为问题提交,但我想我也想在这里问一下:是否有人知道可以修复此代码的解决方法,以便我可以真的让它在 Android 版 Chrome 上运行吗?

I've filed this as an issue using the Chrome issue reporting process (which is private and possibly a black hole) but I thought I'd also ask here: is anyone aware of workarounds that would fix this code so I can actually make it work on Chrome for Android?

(我的要求是录制的音频必须转到 JavaScript 事件处理程序,例如此处的事件处理程序,而不是作为表单上传的一部分直接发送到服务器,这很可能已经可以工作了.)

(A requirement for my purposes is that the recorded audio must go to a JavaScript event handler such as the one here, and e.g. not directly to the server as part of a form upload, which very possibly might work already.)

我觉得这是最前沿的东西,没有太多人尝试使用它,但我认为这可能值得一问.:)

I get the impression that this is bleeding-edge stuff and not too many people are trying to use it, but I thought it might be worth asking. :)

推荐答案

我们仍在研究 Android 中的音频输入;它在版本 31 中启用,但尚未在所有设备上启用.它确实适用于 Nexus 设备,至少 - N4、N5、N7.

We're still working on audio input in Android; it's enabled in version 31, but it's not turned on across all devices yet. It does work on Nexus devices, at least - N4, N5, N7.

这篇关于使用网络音频 API 和 navigator.getUserMedia 在 Android 版 Chrome 中录制音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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