How to use SoundManager2 to stream from SoundCloud

2019-07-29 08:42发布

问题:

SoundManager2 gets a data error and I cannot visualize anything?

or

I cannot access the song, permission denied?

or

It works when I first play it, but if I pause it and play again, I get a data error?

回答1:

This has recently been fixed, as it was partly due to half of the needed files being there. Now it is fixed however, it still might not work off the bat.

The obvious first step is you use the api to get the track stream_url, which looks like http://api.soundcloud.com/tracks/69322564/stream?client_id=CLIENT_ID

If you use this as the media url in SoundCloud, you will find that you press play, and if you have visualizations they will work, and everything is nice. However if you now pause the track, and press play again, you will get a data error, metadata will cease to be accessible, and your visualizations will break. This is because api.soundcloud.com has a crossdomain file, and when you access it you get a 3XX redirect to ec-media.soundcloud.com. This site now also has a crossdomain.xml file, however that pesky 3XX redirect ruins both permissions, so you hit an error.

The answer to this is you make the redirect leap first, outside of soundmanager2, so that there is no redirect that will break it. For instance in Python:

import urllib2
starturl = 'http://api.soundcloud.com/tracks/69322564/stream?client_id=CLIENT_ID'
res = urllib2.urlopen(starturl)
finalurl = res.geturl()
print finalurl

This could be more elegant, but it will print the url that the api redirects to. This url will look something like http://ec-media.soundcloud.com/2j0lNF81jv9m.128.mp3?LONG_STRING&AWSAccessKeyId=ACCESS_KEY&Expires=1355864871&Signature=SIGNATURE

This domain has the crossdomain.xml file, and due to the fact that there is no redirect, things will run smoothly, data will be accessed, all will be well.

"I did this and it worked, but now it says the file is forbidden"

Now we draw your attention to the previous url, in particular &Expires=1355864871. The file you are redirected to is not permanent, so you need to grab it each time. For me this is easy, I work in django so I can simply run the python above in my views scripts. You'll have to find a way to implement this in your code of choice. (Should work in javascript too).

After all this is done, you should be able to pause and play as much as you want, and retrieve the waveform data, the EQ data, and the peak data. With these things, some fun things can be done. Hope this helped.