This project is read-only.
Example

SpotifyConnection connection = new SpotifyConnection(new Sharpotify.Cache.FileCache(), new TimeSpan(0, 1, 0));
Console.Write("Username: ");
string username = Console.ReadLine();
Console.Write("Password: ");
string password = Console.ReadLine();
try
{
    connection.Login(username, password);
}
catch (Sharpotify.Exceptions.AuthenticationException ex)
{
    Console.WriteLine("Unable to login: " + ex.Message);
    connection.Close();
    Console.ReadKey();
    return;
}
catch (Exception ex)
{
    Console.WriteLine("Unspected Exception: " + ex.Message);
    connection.Close();
    Console.ReadKey();
    return;
}



Sharpotify.Media.User user; 
try
{
    user = connection.User();
}
catch (Exception ex)
{
    Console.WriteLine("TimeOut? " + ex.Message);
    connection.Close();
    Console.ReadKey();
    return;
}
if (user != null)
{
    Console.WriteLine("USER INFO");
    Console.Write("Name: ");
    Console.WriteLine(user.Name);
    Console.Write("Country: ");
    Console.WriteLine(user.Country);
    Console.Write("Is premium: ");
    Console.WriteLine(user.IsPremium);
}
Sharpotify.Media.Result result = null;

Console.Write("Search key: ");
string searchKey = Console.ReadLine();

bool success = false;
Exception lastException = null;
for (int i = 0; i < 3; i++)
{
    try
    {
        result = connection.Search(searchKey); //rihanna
        success = true;
        break;
    }
    catch (Exception ex)
    {
        lastException = ex;
        Thread.Sleep(500);
    }
}
if (!success)
{
    Console.WriteLine("Unable to search");
    
    if (lastException != null)
        Console.WriteLine(lastException.Message + "\n" + lastException.StackTrace);

    connection.Close();
    Console.ReadKey();
    return;
}

Console.WriteLine("SEARCH RESULT");
Console.Write("Total no of tracks: ");
Console.WriteLine(result.TotalTracks);

for (int i = 0; i < 10 && i < result.TotalTracks; i++)
{
    System.Console.WriteLine(result.Tracks[i].ToString());
}

int trackNo = 0;
Console.Write("Select one track index: ");
string aux = Console.ReadLine();
if (!string.IsNullOrEmpty(aux))
    trackNo = int.Parse(aux);

var playTrack = result.Tracks[trackNo];
var trackResult = connection.Browse(playTrack);

Console.WriteLine();
Console.WriteLine("Playing track " + trackNo + ": " + trackResult.Artist.Name + " - " + trackResult.Title);

try
{
    var musicStream = connection.GetMusicStream(trackResult, trackResult.Files[0], new TimeSpan(0, 1, 0));



    Console.WriteLine("Wait for stream to fill ... press any key when bored");
    //Console.ReadKey();

    while (musicStream.AvailableLength < musicStream.Length || musicStream.Length == 0)
    {
        Console.WriteLine("Caching data : " + musicStream.AvailableLength + " / " + musicStream.Length);
        System.Threading.Thread.Sleep(1000);
    }
    Console.WriteLine("All cached");
    musicStream.Close();
}
catch (Exception ex)
{
    Console.WriteLine("Unable to download: " + ex.Message);
    Console.WriteLine(ex.StackTrace);
}

connection.Close();
Console.ReadKey();

Last edited Jun 8, 2010 at 11:38 AM by fescolar, version 2

Comments

Venson Aug 11, 2014 at 9:21 PM 
Hey, First i want to thank you for you work this wrapper is exactly what i was looking for so: Thank you a lot.

But its not longer the State of the Art.
There are some Missing values in
ParseImage:
- small
- large
ParseArtists()
- popularity
ParseResult()
- restrictions
- popularity
ParseAlbum()
- cover-small
ParseAblums()
- cover-large

rally25rs Jul 30, 2011 at 12:37 PM 
This sample code does not seem to work. After entering a search word, it throws an exception in the MemoryStream constructor:

at System.IO.MemoryStream..ctor(Byte[] buffer, Int32 index, Int32 count)
at Sharpotify.Media.Parser.XMLMediaParser.Parse(Byte[] xml) in C:\Users\nullex\Desktop\sharpotify\Sharpotify.Lib\Media\Parser\XMLMediaParser.cs:line 873

And:

Unable to search
Object reference not set to an instance of an object.
at Sharpotify.SpotifyConnection.Search(String query) in C:\Users\nullex\Desktop\sharpotify\Sharpotify.Lib\SpotifyConnection.cs:line 239

Then a bunch of bytes (including some bell characters, hence the beeps oabjorka described) are written to the console, and closes the program.

McBainUK Oct 25, 2010 at 6:17 PM 
I get a "Timeout while waiting for user data." exception thrown by the line "user = connection.User();". This is running in a console application on Windows XP SP3.

Does the library and example still work with the latest versions of Spotify?

oabjorka Jun 24, 2010 at 2:04 PM 
I tried the example code in a Console application and it runs successfully, but I don't get any music. Just beeping sounds after connection.Close(); How can I play the musicStream?