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

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-16 18:40:50

问题


I am building an audio application that have two servers involved. Server A is dedicated for audio streaming, while B servers a HTML page that loads audio sources from A.

The audios are OK to play. However, when I try to do some magic with WebAudio API, I got message saying

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

This is fair because WebAudio spec said

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.

The question is what if I do want to have this "Cross-origin" inspection?

I have full control on the outputs of both servers, so I came up with two ideas.

  1. Add Access-Control-Allow-Origin headers. This is what worked for CORS XMLHttpRequest. However, it does not work for WebAudio API. Do I need some other headers?
  2. Do some trick on domain mapping or headers, to let browser think A and B are (logically) the same domain. So far I have no idea if it is actually feasible.

Should I have more options? Any suggestion is appreciated.

Note:

Proxying with server B is not an option for me, because the purpose of A is to offload such work from B.


回答1:


Found answer by myself.

The solution is to add crossOrigin="anonymous" attribute to corresponding audio element.



来源:https://stackoverflow.com/questions/51906591/is-it-possible-to-enable-webaudio-processing-for-cross-origin-resources-with-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!