SoundCloud Api重定向混乱和音频Api流


I am attempting to make a request to the SoundCloud API. Then when I get the response I set the stream_url as the source of an < audio > element.

我正在尝试向SoundCloud API发出请求。然后,当我得到响应时,我将stream_url设置为< audio >元素的源。

This works:




But not always... When you search for 'Bonobo' for example, you can play the first few tracks without any issue. But when you try to play 'London Grammar - Hey Now (Bonobo remix)' - the 7th result - it won't play. It throws no errors whatsoever.

但不总是……例如,当你搜索“Bonobo”时,你可以播放前几首歌曲,没有任何问题。但是当你试着播放“伦敦语法——嘿,现在(Bonobo remix)”——第7个结果——它就不会播放了。它不会抛出任何错误。

I've been tinkering around with Chrome's webdev-tools and under the network tab I see the requests being made. I found that tracks that DO play have a short Request Url, like this:



And the ones that don't look like this:



Now at first glance I figured it was an encoding issue, but wrapping a quick encodeURI() around the ajax url did not work.

乍一看,我认为这是一个编码问题,但是围绕ajax url包装一个quick encodeURI()并没有起作用。

Furthermore I do not understand where these urls come from. In my code I am directing my ajax request towards, for example:



Thus, the request url in the GET request (as found under 'network' in Chrome's webdev tools) makes no sense to me. Is SoundCloud redirecting get requests to a CDN-host? One more thing I've noticed is that each time TWO requests are fired instead of one. The first one is always canceled and contains a 'Provisional headers are shown' warning. I believe this is because I am setting crossOrigin = "anonymous", otherwise certain browsers would not load the content.

因此,GET请求中的请求url(如Chrome的webdev工具中的“network”)对我来说毫无意义。SoundCloud重定向是否向CDN-host发送get请求?我还注意到,每次触发两个请求而不是一个请求。第一个总是被取消,并包含一个“临时标题显示”警告。我认为这是因为我设置了crossOrigin =“匿名”,否则某些浏览器不会加载内容。

What I guess may cause the problem is that when the url is set as the src attribute of the element an evenListener is fired in the dancer.js library, which handles the Audio Api and the playback (https://github.com/jsantell/dancer.js/). It may be that encodeURI() is required somewhere in the library.


I decided to ask the question anyhow because I don't understand how the Request Urls's above are formed and why two, instead of one, requests are being fired and why the first is always cancelled.


Any hints which my solve the playback issue are more than welcome too...


1 个解决方案



When you run the request for



you get a HTTP 302 Found response from the server, which is a URL redirect (https://en.wikipedia.org/wiki/HTTP_302). This will cause your browser to load from the new URL that the server returns, and thus the two requests you see. The server basically says "yeah, I know where to find that file, ask that guy over there".

您将从服务器获得一个HTTP 302找到的响应,这是一个URL重定向(https://en.wikipedia.org/wiki/HTTP_302)。这将导致浏览器从服务器返回的新URL加载,从而加载您看到的两个请求。服务器基本上说"我知道在哪里找到那个文件,问那边那个人"

The reason why one works and the other not, I'd think, is that https://ec-media.sndcdn.com has the Access-Control headers set while https://cf-media.sndcdn.com doesn't. This is an issue with the server configuration and unfortunately nothing you can control from the client side. Dunno if it's a deliberate move by soundcloud or if it's something you could ask them about.