【问题标题】:Problems with WebAudioWebAudio 的问题
【发布时间】:2019-06-04 05:24:37
【问题描述】:

我正在创建一个研究实验,它使用 WebAudio API 来记录用户说出的音频文件。 我想出了一个使用 recorder.js 的解决方案,一切正常......直到我昨天尝试过。

我现在在 Chrome 中收到此错误:

"AudioContext 不允许启动。必须恢复它(或 创建)在页面上的用户手势之后。”

它指的是这个链接:Web Audio API policy。 这似乎是上面链接中概述的 Chrome 新政策的结果。

所以我尝试像这样使用 resume() 来解决问题:

var gumStream; //stream from getUserMedia()
var rec; //Recorder.js object
var input; //MediaStreamAudioSourceNode we'll be recording

// shim for AudioContext when it's not avb. 
var AudioContext = window.AudioContext || window.webkitAudioContext;
var audioContext = new AudioContext; //new audio context to help us record

function startUserMedia() {
    var constraints = { audio: true, video:false };
    audioContext.resume().then(() => { // This is the new part
        console.log('context resumed successfully');
    });
    
    navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {

        console.log("getUserMedia() success, stream created, initializing Recorder.js");
    gumStream = stream;
    input = audioContext.createMediaStreamSource(stream);
    rec = new Recorder(input, {numChannels:1});
    audio_recording_allowed = true;
    }).catch(function(err) {
        console.log("Error");
    });

}

现在在控制台中我得到:

错误

上下文恢复成功

并且流没有初始化。 这在 Firefox 和 Chrome 中都会发生。 我需要做什么?

【问题讨论】:

  • 如何以及何时调用startUserMedia()

标签: web-audio-api recorder.js


【解决方案1】:

我刚刚遇到了同样的问题!从技术上讲,你帮助我找到了这个答案。由于某种原因,我的错误消息不如您的完整,这些政策更改的链接有答案:)

最好在用户与文档交互后创建音频上下文,而不是恢复,(当我说最佳实践时,如果您查看 padenot 2018 年 9 月 28 日的第一条评论this thread,他在第一个要点中提到了原因)

所以不要这样:

var audioContext = new AudioContext; //new audio context to help us record

function startUserMedia() {
    audioContext.resume().then(() => { // This is the new part
        console.log('context resumed successfully');
    });
}

只需像这样设置音频上下文:

var audioContext;

function startUserMedia() {
    if(!audioContext){
      audioContext = new AudioContext;
    } 
}

只要在某种用户手势之后执行startUserMedia(),这应该可以工作。

【讨论】:

    猜你喜欢
    • 2021-06-05
    • 1970-01-01
    • 2016-09-14
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多