简体   繁体   中英

playing a mp3 link as a file with javascript

I'm trying to get a mp3 file from SoundCloud and use that file with SoundJS (from CreateJS) to make a music visualizer. But there is one problem. When I assign the file path to the stream mp3 link from SoundCloud, the music doesn't get loaded and the code gets stuck at createjs.Sound.registerSound(src); // register sound, which preloads by default createjs.Sound.registerSound(src); // register sound, which preloads by default . But when I assign a file on my server to it, it works.

This is the error I get:

[Error] TypeError: 'undefined' is not an object (evaluating 'a.toString')
    _parsePath (createjs-2013.12.12.min.js, line 15)
    registerSound (createjs-2013.12.12.min.js, line 15)
    init (MusicVisualizer.html, line 108)
    onload (MusicVisualizer.html, line 9)

This works:

    var assetsPath = "assets/"; // Path were mp3 is stored
    var src = assetsPath + "Underground.mp3";  // set up the source

But this doesn't:

    var src = 'http://api.soundcloud.com/tracks/'+track_id+'/stream?client_id='+client_id';    

I enter the good track_id and client_id. Because when I paste the link in my webbrowser, the link redirects to the mp3 file from SoundCloud with an access code and signature etc. and then I can see the file and play it. The link above immediately redirects to a link like http://soundcloud.vo.llnwd.net/PEPEnVEvnxwb.128.mp3?AWSAccessKeyId=AKIAJNIAGLK7XA7YZSNQ&Expires=1323978179&Signature=yxVIOjsdOiwhlLOtCnk3aI1YN4M%3D&e=1298204179&h=7103a8645083f828f21e6d41aa0b96b5

So, in short, I can't play the mp3 file via the link, but I can play it right of the server. Do you have a solution for this problem?

Thanks

I see solution in next way:

SoundCloud server that redirects you to the .mp3 file is not as stupid as you can think. It reads headers from your requests and makes decision to allow you to listen to music or not. Also there can be difficulties with redirects. So the first thing you should try is to write a proxy. Simple proxy written in PHP should work fine. Or you can use Node.js for that purpose. The only thing it must do is setting correct headers to your request and if there is a problem with redirections then make a direct link as a response to that proxy.

there are 2 issues with this approach that will prevent it from working.

Currently, SoundJS only handles loading files (Underground.mp3), it cannot handle redirects or proxies or anything that does not have both a music file type (.mp3, .ogg, etc) and returns a loadable file.

The second issue is that SoundJS does not handle streaming files, so a streaming source will not work.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM