由于缺少 CORS 策略,无法分析 Soundcloud 的流音频

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

我正在开发这个可视化工具 http://trif.it,它在 Chrome 稳定版 (41.x) 上仍然运行良好,但在 Chrome Dev、Beta 和 Canary (42.x 之后)因为 Chrome(以及之前的 Firefox)处理要分析的音频源的方式发生了变化。

这是有问题的代码。在您删除处理音频路由的最后一部分的评论之前,它应该可以正常工作。

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);*/

我最初在 Chromium 错误跟踪器 (https://code.google.com/p/chromium/issues/detail?id=462998) 上发布了一个 bug/request,但得到了一个关于从现在开始任何媒体资源都需要在 header 中使用 CORS 的答案要分析(或通过 createMediaElementSource 调用)和 Soundcloud 以及我目前在网站上使用的 shoutcast 流都没有提供。我总是可以使用 CORS 代理,但也许你知道一个解决方法,或者 CORS headers 计划在今年某个时候出现,因为许多网站很快就会面临与我相同的问题?

谢谢。

这是出于跨域安全原因,不太可能更改。不确定 SoundCloud 等是否正在计划 CORS 访问。

添加一个:

audioElement.crossOrigin = "anonymous";

在那里的某个地方,在设置源之前,应该可以正常工作。