获取用户音频时出现Google Chrome Javascript问题-不允许启动AudioContext

syqv5f0l  于 2023-05-05  发布在  Java
关注(0)|答案(3)|浏览(345)

我有这个Javascript代码,我用它来捕捉用户的音频输入,当他们点击麦克风按钮。此代码在Mozilla Firefox中工作,但当我在Google Chrome中使用它时,它不工作,并在控制台中显示此警告/错误-The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.

var r = function() {
            var e = {}
              , t = void 0
              , n = getBotConfig()
              , r = new Audio("data:audio/wav;base64,")
              , o = !1;
            if (!n.isIE()) {
                window.AudioContext = window.AudioContext || window.webkitAudioContext;
                var i = new AudioContext;
                e.toggleRecording = function(e, t, n, r, s, a, c) {
                    e.classList.contains("recording") ? (e.classList.remove("recording"),
                    o = !1,
                    t.emit("end-recording", {
                        session_id: a,
                        bot_id: c
                    }),
                    document.getElementById("btnToggle").setAttribute("style", "background-color:transparent"),
                    document.getElementsByClassName("fa-microphone")[0] && document.getElementsByClassName("fa-microphone")[0].setAttribute("style", "color:" + s)) : (e.classList.add("recording"),
                    o = !0,
                    t.emit("start-recording", {
                        numChannels: 1,
                        bps: 16,
                        fps: parseInt(i.sampleRate),
                        session_id: a,
                        bot_id: c
                    }),
                    document.getElementById("btnToggle").setAttribute("style", "background-color:" + n),
                    document.getElementsByClassName("fa-microphone")[0] && document.getElementsByClassName("fa-microphone")[0].setAttribute("style", "color:" + r))
                }
                ,
                e.onAudioTTS = function(e) {
                    try {
                        r.pause(),
                        c(e)
                    } catch (t) {
                        c(e)
                    }
                }
                ,
                e.initAudio = function(e, n, r) {
                    console.log("audio initiated"),
                    t = e,
                    navigator.getUserMedia || (navigator.getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia),
                    navigator.cancelAnimationFrame || (navigator.cancelAnimationFrame = navigator.webkitCancelAnimationFrame || navigator.mozCancelAnimationFrame),
                    navigator.requestAnimationFrame || (navigator.requestAnimationFrame = navigator.webkitRequestAnimationFrame || navigator.mozRequestAnimationFrame),
                    navigator.getUserMedia({
                        audio: !0
                    }, a, function(e) {
                        alert("Error getting audio"),
                        console.log(e)
                    })
                }
                ;
                var s = function(e) {
                    var t = i.createChannelSplitter(2)
                      , n = i.createChannelMerger(2);
                    return e.connect(t),
                    t.connect(n, 0, 0),
                    t.connect(n, 0, 1),
                    n
                }
                  , a = function(e) {
                    var n = i.createGain()
                      , r = i.createMediaStreamSource(e)
                      , a = r;
                    a = s(a),
                    a.connect(n);
                    var c = (i.createScriptProcessor || i.createJavaScriptNode).call(i, 1024, 1, 1);
                    c.onaudioprocess = function(e) {
                        if (o) {
                            for (var n = e.inputBuffer.getChannelData(0), r = new ArrayBuffer(2 * n.length), i = new DataView(r), s = 0, a = 0; s < n.length; s++,
                            a += 2) {
                                var c = Math.max(-1, Math.min(1, n[s]));
                                i.setInt16(a, c < 0 ? 32768 * c : 32767 * c, !0)
                            }
                            t.emit("write-audio", r)
                        }
                    }
                    ,
                    n.connect(c),
                    c.connect(i.destination);
                    var u = i.createGain();
                    u.gain.value = 0,
                    n.connect(u),
                    u.connect(i.destination)
                }
                  , c = function(e) {
                    r.src = "data:audio/wav;base64," + e,
                    r.play()
                };
                return e
            }
        };

警告/错误出现在var i = new AudioContext;行。它以前在谷歌Chrome浏览器上也可以工作,但现在不工作了。Google开发者页面上的描述说必须使用resume(),但我不确定我应该如何以及在哪里这样做。

xu3bshqb

xu3bshqb1#

你应该可以在调用play()之前调用resume()。重要的是在用户操作/事件中调用它-就像点击麦克风按钮一样。
要点:如果AudioContext是在文档接收到用户手势之前创建的,它将以“挂起”状态创建,并且您需要在接收到用户手势之后调用resume()。
https://developers.google.com/web/updates/2017/09/autoplay-policy-changes
它以前在谷歌Chrome浏览器上也可以工作,但现在不工作了。
新的政策已经在最近的chrome更新中实施。

byqmnocz

byqmnocz2#

你应该打电话给getAudioContext().resume();
如果您的问题与p5.js有关,请在setup中访问麦克风

function setup() {
  mic = new p5.AudioIn();
  mic.start();
  getAudioContext().resume();
}

或者在文档中添加touchStarted函数。你必须点击网页才能触发这个功能。

function touchStarted() {
  getAudioContext().resume();
}
jogvjijk

jogvjijk3#

很久以后才发现这个,只是想为后代添加一些东西。以上答案使用getAudioContext().resume()是正确的,但如果在getUserMedia内部创建AudioContext,也可以避免此错误

navigator.getUserMedia(constraints)
.then((stream) => {
    var audioContext = new AudioContext();
    // ... rest of code
})

这样,AudioContext就不会进入挂起状态。

相关问题