Search code examples
javascriptfileaudiomp3

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. 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


Solution

  • 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.