Safari:TypeError:代理的“目标”应为对象

问题描述 投票:0回答:1

我正在使用网络音频,并且下面的代码在Chrome中可以正常运行,但在Safari中不能正常工作。

const audioContextList = [];
(function () {
    self.AudioContext = new Proxy(self.AudioContext, {
        construct(target, args) {
            const result = new target(...args);
            audioContextList.push(result);
            return result;
        }   
    });
})();

在Safari中,出现以下错误:

TypeError:代理的“目标”应为对象

我该如何解决?

javascript safari web-audio-api
1个回答
1
投票

带有供应商前缀:webkit的Safari 13.1supportsAudioContext。>>

因此您应该使用self.webkitAudioContext访问它。或者您可以制作一种跨浏览器的解决方案

let AudioContext = self.AudioContext || self.webkitAudioContext;
self.AudioContext = new Proxy(AudioContext, {
  construct(target, args) {
    const result = new target(...args);
    audioContextList.push(result);
    return result;
  }
});  

© www.soinside.com 2019 - 2024. All rights reserved.