Cannot analyse Soundcloud's streaming audio because of the lack of CORS policy

萝らか妹 提交于 2019-12-13 02:38:53

问题


I am working on this visualizer http://trif.it that still works well on Chrome Stable (41.x) but stopped working in Chrome Dev, Beta and Canary (42.x onwards) because of a change in how Chrome (and Firefox before that) handles audio sources to be analysed.

Here is the code that is problematic. It should work well until you remove the comments on the last portion that is handling the audio routing.

var audioElement = new Audio();
var clientId = "xxxxx";

var oReq = new XMLHttpRequest();
//oReq.onload = reqListener;
oReq.open("get", "http://api.soundcloud.com/tracks/194245583.json?client_id=" + clientId, true);
oReq.send();

oReq.onreadystatechange = function() {
  if (oReq.readyState != 4)  { return; }

  var serverResponse = JSON.parse(oReq.responseText);
  console.log(serverResponse);

  audioElement.src = serverResponse.stream_url + "?client_id=" + clientId;
  audioElement.autoplay = true;
  audioElement.preload = true;
};

/*var audioContext = new AudioContext();
var audioSource = audioContext.createMediaElementSource(audioElement);
var audioAnalyser = audioContext.createAnalyser();
var audioGain = audioContext.createGain();
audioGain.gain.value = 1.0;

audioSource.connect(audioAnalyser);
audioAnalyser.connect(audioGain);
audioGain.connect(audioContext.destination);*/

I had initially posted a bug/request on Chromium bug tracker (https://code.google.com/p/chromium/issues/detail?id=462998) but got an answer about the need to have CORS in the header from now on for any media resources to be analysed (or called through createMediaElementSource) and Soundcloud as well as the shoutcast streams I am using on the site at the moment are not providing that. I could always use a CORS proxy but perhaps that you know a workaround or that CORS headers are planned at some point this year as many websites will face the same problem as me soon enough?

Thanks.


回答1:


Add a:

audioElement.crossOrigin = "anonymous";

somewhere in there, before setting the source, that should work fine.




回答2:


This is for cross-domain-security reasons, and is unlikely to change. Not sure if SoundCloud, et al are planning CORS access.



来源:https://stackoverflow.com/questions/28984883/cannot-analyse-soundclouds-streaming-audio-because-of-the-lack-of-cors-policy

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