通过客户端XHR打开SoundCloud轨道的stream_url? [英] Open stream_url of a Soundcloud Track via Client-Side XHR?

查看:296
本文介绍了通过客户端XHR打开SoundCloud轨道的stream_url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于您可以通过XHR
调用Soundcloud API(因为CORS头部会发送 http://backstage.soundcloud.com/2010/08/of-cors-we-do/ ,对吗?)
我想知道如果这是可能的



当使用Web Audio API尝试使用XHR(从客户端端)打开stream_url时,我得到一个原因不允许Access-Control-Allow-Origin。错误。
有一种方法通过XHttpRequest从客户端JavaScript加载音频资源,或者是不可能的(流媒体 - 不去>>音频数据API和流式传输 - 不用?



非常酷的东西与Web Audio API。



谢谢!



UPDATE / p>

问题可能与您在调用时获得的302重定向有关:

  http://api.soundcloud.com/tracks/5646378/stream.json?client_id= [client_id] 

或某些其他呼叫请求实际媒体文件(见评论)。

  HTTP / 1.1 302 Moved Temporarily 
服务器:nginx
日期:Sun,24 Jun 2012 09:58:01 GMT
Content-Type:application / json; charset = utf-8
连接:keep-alive
缓存控制:no-cache
位置:http://ec-media.soundcloud.com/ [...]
X-Runtime:41
Content-Length:339
访问控制允许方法:GET,PUT,POST,DELETE
访问控制允许标题:接受,授权,Content-Type,Origin
Access-Control-Allow-Origin:*
X-Cacheable:NO:Cache-Control = no-cache
X-Varnish:2433107209
:0
Via:1.1 varnish
X-Cache:MISS

HTTP / 1.1 200 OK
Accept-Ranges:bytes
Cache-Control:max -age = 252460800
Content-Type:audio / mpeg
日期:Sun,24 Jun 2012 09:58:01 GMT
ETag:c2241e [...] 41bbcf7c0d32f09
最后修改:Tue,2010年9月28日17:57:26 GMT
服务器:ECAcc(fra / D484)
x-amz-id-2:oB0XzQcc /[...+ a
x-amz-meta-bitrate:128
x-amz-meta-duration:152900
x-amz-meta-job:t405jWkfNvx7
x-amz-request- id:D65 [...] E1355
X-Cache:HIT
Content-Length:2445478


b $ b

原始的JSON响应具有CORS头,而重定向的位置不是。

解决方案

好吧,只要音频文件本身不带有CORS头(参见curl -IL输出的问题),唯一的解决方案,为我工作是打开音频文件,自己添加标题。



从我的理解,这是其他应用程序,如 https://github.com/tsenart/audiojedit 也。 (在node.js)
这有巨大的缺点,将二进制文件的流量转移到您的服务器,否则由soundcloud.com服务。



您可以使用PHP打开文件,添加标题并用以下内容重新生成:

  $ fp = fopen ('http://api.soundcloud.com/tracks/[track_id]/stream.json?client_id=[client_id]','rb'); 

header(Content-Type:audio / mpeg);
header(Access-Control-Allow-Methods:GET,PUT,POST,DELETE);
header(Access-Control-Allow-Headers:Accept,Authorization,Content-Type,Origin);
header(Access-Control-Allow-Origin:*);
fpassthru($ fp);

exit();

正如我所说,这只是一个解决方法,不是一个生产环境的好解决方案
,但帮助我继续使用我的Web音频应用程序。



Soundcloud,任何机会将CORS标题添加到音频文件? :)


Since you can call the the Soundcloud API via XHR (because of the CORS headers it sends http://backstage.soundcloud.com/2010/08/of-cors-we-do/, right?) I was wondering if this was possible with the audio data itself, like a tracks' stream_url for example.

When trying to open the stream_url with a XHR (from the Client Side) using the Web Audio API, i get a Origin is not allowed by Access-Control-Allow-Origin. error. Is there a way to load Audio resources via XHttpRequest from Client-Side-Javascript, or is it impossible ( Audio Data API and streaming - a no-go?) ?

Would be great as this would allow some really cool things with the Web Audio API.

Thanks!

UPDATE

The problem might have to do with the 302 redirect you get when calling:

http://api.soundcloud.com/tracks/5646378/stream.json?client_id=[client_id]

or some other call requesting the actual media file (see comments).

HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Sun, 24 Jun 2012 09:58:01 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Cache-Control: no-cache
Location: http://ec-media.soundcloud.com/[...]
X-Runtime: 41
Content-Length: 339
Access-Control-Allow-Methods: GET, PUT, POST, DELETE
Access-Control-Allow-Headers: Accept, Authorization, Content-Type, Origin
Access-Control-Allow-Origin: *
X-Cacheable: NO:Cache-Control=no-cache
X-Varnish: 2433107209
Age: 0
Via: 1.1 varnish
X-Cache: MISS

HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: max-age=252460800
Content-Type: audio/mpeg
Date: Sun, 24 Jun 2012 09:58:01 GMT
ETag: "c2241e[...]41bbcf7c0d32f09"
Last-Modified: Tue, 28 Sep 2010 17:57:26 GMT
Server: ECAcc (fra/D484)
x-amz-id-2: oB0XzQcc/[...]+mAr/a
x-amz-meta-bitrate: 128
x-amz-meta-duration: 152900
x-amz-meta-job: t405jWkfNvx7
x-amz-request-id: D65[...]E1355
X-Cache: HIT
Content-Length: 2445478

The original JSON response has the CORS headers, while the location of the redirect don't.

解决方案

Well, as long as the Audio Files themselves don't come with the CORS headers (see curl -IL output in the question), the only solution that worked for me is open the audio files and add the headers yourself.

From my understanding, this is what other apps like https://github.com/tsenart/audiojedit do too. (in node.js) This has the the huge downside of shifting the traffic of the binary files to your server, which would be otherwise served by soundcloud.com.

You can use PHP to open the file, add the headers and restream it with something like:

$fp = fopen('http://api.soundcloud.com/tracks/[track_id]/stream.json?client_id=[client_id]', 'rb');

header("Content-Type: audio/mpeg");
header("Access-Control-Allow-Methods: GET, PUT, POST, DELETE");
header("Access-Control-Allow-Headers: Accept, Authorization, Content-Type, Origin");
header("Access-Control-Allow-Origin: *");
fpassthru( $fp );

exit();

As I said, this is just a workaround, not a nice solution for a production environment, but helped me keep going with my Web Audio App.

Soundcloud, any chance of adding CORS headers to the audiofiles? :)

这篇关于通过客户端XHR打开SoundCloud轨道的stream_url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆