Alxandr / SpotiFire

A project to make a SpotifyClient in C#
http://nudoc.azurewebsites.net/SpotiFire
40 stars 19 forks source link

Create a song from spotify URI #19

Closed dannybisby closed 12 years ago

dannybisby commented 12 years ago

How would i go about using SpotiFire and creating a song from a spotify URI? i.e. spotify:track:0zMtWCvlVx3qvvLxBRl36t

Thanks Danny

Alxandr commented 12 years ago

With the newest patch you can do:

var link = spotify.ParseLink("spotify:track:0zMtWCvlVx3qvvLxBRl36t");
var track = track = link.As<ITrack>();

Nice song btw :).

dannybisby commented 12 years ago

Ah excellent, thanks! I have been using songs that i have on my starred list i just tested it with a song that someone had sent me as an inbox and it's erroring similar to how i had it with using someone elses code i think im getting the IS_LOADING error with spotify

I get it throwing the error albumPtr can't be zero.

Im using Console.WriteLine("Login result: " + e.Status); var link = SpotiFire.SpotifyLib.LinkExtensions.ParseLink(sender, "spotify:track:22KGOUFbfFXhffR6ROEKAw"); currentTrack = link.As();

        Console.WriteLine("Playing " + currentTrack.Name.ToString() + " - " + currentTrack.Album.Artist.Name.ToString());
        sender.PlayerLoad(currentTrack);
        sender.PlayerPlay();

to play the song

Thanks for your help Danny :D

Alxandr commented 12 years ago

The problem herein lies in the fact that you are either not understanding or simply ignoring spotify's async way of doing things. Simply put; even though you have a ITrack object, that does not necessarily mean the track in question is loaded, it simply means that you have a ITrack object. If that object is loaded, it's name and Artist etc will be available; however; if it is not, both the artist and album and whatever will be null, thus you'd get a null-pointer exception.

dannybisby commented 12 years ago

What should i use to ensure that the track is loaded before i attempt to play it?

Thanks again for your help, Danny

Alxandr commented 12 years ago

The obious answer is to wait. Just to show an example; I made the following utility method:

static void OnceLoaded(ITrack track, Action<ITrack> callback)
{
    if (track.IsLoaded)
    {
        callback(track);
        return;
    }
    ThreadPool.QueueUserWorkItem(obj =>
    {
        Thread.Sleep(100);
        OnceLoaded(track, callback);
    });
}

I'm thinking of implementing a more general "waiter" in SpotiFire; but that will have to wait for another time.