是否可以使用适当的 Access-Control-Allow-Origin headers 为 Cross-Origin 资源启用 WebAudio 处理?

Is it possible to enable WebAudio processing for Cross-Origin Resources with appropriate Access-Control-Allow-Origin headers?

我正在构建一个涉及两个服务器的音频应用程序。服务器 A 专用于音频流,而 B 服务器是 HTML 从 A 加载音频源的页面。

音频可以播放。但是,当我尝试使用 WebAudio API 施展魔法时,我收到消息说

"MediaElementAudioSource outputs zeroes due to CORS access restrictions for {{URL of audio src}}"

这很公平,因为 WebAudio spec

HTMLMediaElement allows the playback of cross-origin resources. Because Web Audio allows inspection of the content of the resource (e.g. using a MediaElementAudioSourceNode, and a ScriptProcessorNode to read the samples), information leakage can occur if scripts from one origin inspect the content of a resource from another origin.

To prevent this, a MediaElementAudioSourceNode MUST output silence instead of the normal output of the HTMLMediaElement if it has been created using an HTMLMediaElement for which the execution of the fetch algorithm labeled the resource as CORS-cross-origin.

问题是 如果我确实想要这个 "Cross-origin" 检查 怎么办?

我完全控制了两台服务器的输出,所以我想出了两个主意。

  1. 添加Access-Control-Allow-Originheaders。这就是有用的 CORS XMLHttpRequest。但是,它不适用于 WebAudio API。做 我需要一些其他的 headers?
  2. 在域映射上做一些技巧或headers,让浏览器认为A 和 B 是(逻辑上)同一个域。到目前为止我不知道它是否 其实是可行的

我应该有更多选择吗?任何建议表示赞赏。

注:

使用服务器 B 代理对我来说不是一个选项,因为 A 的目的是从 B 卸载此类工作。

自己找到答案。

解决办法是给相应的音频元素添加crossOrigin="anonymous"属性。