perminder-klair / angular-soundmanager2

SoundManager2 Music Player for AngularJs
http://perminder-klair.github.io/angular-soundmanager2/
MIT License
168 stars 107 forks source link

Can't resolve SoundCloud stream url.. #9

Closed denitto closed 9 years ago

denitto commented 9 years ago

Great job with the port! Works great so far with mp3 urls. I have not had the same success with SoundCloud stream urls (which I believe do a 3xx redirect), unfortunately.

So basically , if you put the below url complete with client_id in a browser (I use Chrome), the browser should resolve the final url for a SoundCloud stream_url. When I try to put the same url into SoundManager2 (and I've included a type hint as well) I get the console error: 'invalid song url'

Here is the song I'm trying to load in

$scope.songs = [
        {
            id: 'one',
            artist: 'Chymera',
            url: 'http://api.soundcloud.com/tracks/132994010/stream?client_id=f18fec54bc96c697f0caf6aedc92f666',
            type: 'audio/mp3'
         }
    ];

Has anyone had any luck resolving a SoundCloud streaming track? Any advice here?

denitto commented 9 years ago

I was actually able to get this to work with the SoundCloud-SDK. Happy to post the code if someone else is interested.

Leaving the issue open for now, though, because it doesn't make sense to me why the final url is not resolving.

perminder-klair commented 9 years ago

@denitto that's great news! It will be amazing if you can create pull request of code to this repo or simply send me code via email.

Thanks again!

denitto commented 9 years ago

Will do, bud. Still working out a couple of things but at least got tracks playing!

perminder-klair commented 9 years ago

@denitto any updates with soundcloud sdk?

denitto commented 9 years ago

Hey, bud! So funny. was literally just looking at this thread. I thought I had your port working with SoundCloud, but something was off. I realized I could play files multiple times simultaneously, meaning the objects were not correctly going to the angular-soundmanager2 player.

So where I am now, I have SoundCloud SDK pulling in SoundManager2 Objects, but when I try to feed them into SoundManager, I get the 'invalid song url' error from before. The url on the SoundManger2 object returned by the SDK looks like this (here's the object, abbreviated):

{ id: "T/tracks/186869334-0.8958889318164438", sID: "T/tracks/186869334-0.8958889318164438", url: "http://api.soundcloud.com/tracks/186869334/stream?client_id=f18fec54bc96c697f0caf6aedc92f666", options: Object, instanceOptions: Object… }

Put that url in the browser (or just click it above), and it resolves to the mp3 file. Try and feed that to the music player with: <i music-player="play" add-song="song"></i>

And you get: 'adding song to playlist' followed by: 'invalid song url'

I have some time right now, so I'm looking into your code to see if anything jumps out at me. Let me know if you'd like to see my SDK code.

perminder-klair commented 9 years ago

@denitto great! I just updated whole module, try to pull in latest code and see if things works.

I will look into soundcloud in next few days (wanted to optimise this module first).

denitto commented 9 years ago

Sick. Thanks, bud. I'll download and right now then report back. Oh, man. If we can get this working, I'll kiss your feet.

perminder-klair commented 9 years ago

@denitto haha I don't want to promise, but I will try to make it work with SoundCloud tomorrow. ;)

denitto commented 9 years ago

Oh, hey, what? got it working! Changed this line (in angular-soundmanager.js (not sure of the line number, because it's compressed):

if("undefined"==typeof a||soundManager.canPlayURL(a.url)!==!0)return console.log("invalid song url")

to

if("undefined"==typeof a)return console.log("invalid song url")

and it resolves! Just can't use the canPlayUrl() function to check the item url! whoo!

perminder-klair commented 9 years ago

good find @denitto. This does makes sense as method canPlayUrl() is not working with soundcloud.

I will try to override this method and push a working update tomorrow.

Thanks!

denitto commented 9 years ago

Hellz yes. Thanks, dude!

Here's the SoundCloud code I used (assuming you've included the SDK javascript):

    var the_client_id = '[your_client_id_without_these_brackets]';

    $scope.tracks = [];

    SC.initialize({
        client_id: the_client_id
    });

    $scope.streamTrack = function( track_url ){
        var formatted_url = 'http://soundcloud.com' + track_url;

        SC.get( '/resolve', { url: formatted_url }, function( track ){
            SC.stream( '/tracks/' + track.id, function( sm_object ){
                $scope.tracks.push( sm_object );  
                console.log( sm_object.url );
                return( sm_object.url );
            });      
        });
    }

HTML (with an ng-repeat to output several play buttons, hence $index, using ng-int to run the track function):

<ul class="inline-list track-small" ng-repeat="song in songs" ng-init="streamTrack( song.meta.link_for_player )">
    <i music-player="play" add-song="tracks[$index]"></i>
</ul>
naz commented 9 years ago

Hi guys. Have the same problem as @denitto described, got it working by substituting track validation function in angularPlayer factory. Will be doing a PR later on today. And here is the code I changed:

isTrackValid: function(track){
  if(track == 'undefined') return false;

  if(track.options && track.options.type === 'audio/mp3') return true;

  return soundManager.canPlayURL(track.url);
}

and this method is used in addTrack like so:

addTrack: function(track) {
    //check if track itself is valid and if its url is playable
    if(!this.isTrackValid(track)) {
        console.log('invalid song url');
        return null;
    }
// rest of the method here...

I havent found any good way to check the instance of track object as SMSound is private.. that's why I am checking type for now. Any suggestions how this could be done in a better way?

perminder-klair commented 9 years ago

thanks @Gargol ! @denitto here it is, soundcloud support added, working example here: https://github.com/perminder-klair/angular-soundmanager2/tree/master/example-soundcloud

naz commented 9 years ago

Hi guys. Was going to do a PR the other day as promissed but looks like @pinku1 did all the work for me :smile: . Opened couple of other issues and PRs instead :smirk: