阅读背景:

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:

如此:

https://matthiasdv.org/beta/

https://matthiasdv.org/beta/

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:

我一直在摆弄Chrome的网络工具,在网络标签下,我看到了请求。我发现播放的曲目有一个简短的请求Url,像这样:

https://ec-media.sndcdn.com/vR5ukuOzyLbw.128.mp3?f10880d39085a94a0418a7ef69b03d522cd6dfee9399eeb9a522029f6bfab939b9ae57af14bba24e44e1542924c205ad28a52352010cd0e7dd461e9243ab54dc0f0bba897d

And the ones that don't look like this:

而那些不像这样的

https://cf-media.sndcdn.com/8PCswwlkswOd.128.mp3?Policy=eyJTdGF0ZW1lbnQiOlt7IlJlc291cmNlIjoiKjovL2NmLW1lZGlhLnNuZGNkbi5jb20vOFBDc3d3bGtzd09kLjEyOC5tcDMiLCJDb25kaXRpb24iOnsiRGF0ZUxlc3NUaGFuIjp7IkFXUzpFcG9jaFRpbWUiOjE0MzM0Mjc2MDN9fX1dfQ__&Signature=cD-XVhnvQnIATkfrBDDVy0Q7996C8DymwxRLwBBduab0~L0MynF1ftcMky~21T8Q-gCZ2~dMK8dz7uVxvJTIJgXPxEZvhNtbvescMK6iFMg-xSAty-4OhJYjrIZJ2j8NE4uNA4Ml7MWbWcQw4KtUtpZitOQuguS3DPFDII3VF-dvzb2L~xG-G8Uu3uOnI1WhnAAfhf1QWMO7swwB89HtcCiuVBmfluG28ELrJEq-au8mqIMB3sLTno6nUuTtpHXR2ayXBsYcYLLJVXa3Ul8p1rhLS5XWHKWXY8xug4jwey27~C5PVAomK6Z5lJx-mz-0zYs4riUYtl0zACbZ1OfwTQ__&Key-Pair-Id=APKAJAGZ7VMH2PFPW6UQ

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:

此外,我不知道这些url来自哪里。在我的代码中,我将ajax请求指向,例如:

https://api.soundcloud.com/tracks/140326936/stream?client_id=5c6ceaa17461a1c79d503b345a26a54e

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.

我猜可能会引起问题的是,当url被设置为元素的src属性时,一个evenListener就会在舞蹈者中被触发。js库,用于处理音频Api和回放(https://github.com/jsantell/dancer.js/)。可能需要在库中的某个地方使用encodeURI()。

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.

我决定无论如何都要问这个问题,因为我不理解上面的请求url是如何形成的,为什么两个而不是一个请求被触发,为什么第一个总是被取消。

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

任何我解决回放问题的提示都是非常欢迎的……

1 个解决方案

#1


3  

When you run the request for

运行请求时

 https://api.soundcloud.com/tracks/140326936/stream?client_id=5c6ceaa17461a1c79d503b345a26a54e

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.

我认为,一个可以工作,另一个不能工作的原因是https://ec-media.sndcdn.com设置了访问控制头,而https://cf-media.sndcdn.com没有。这是服务器配置的问题,不幸的是,您无法从客户端控制。不知道这是soundcloud故意做的还是你可以问他们的。


分享到: