Advertisement
Guest User

Jonas Larsson

a guest
Jan 14th, 2010
1,905
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.09 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using System.IO;
  7.  
  8. using Spotify;
  9.  
  10.  
  11. namespace SharpTest
  12. {
  13.     class MainClass
  14.     {
  15.         private static Session session = null;
  16.         private static ManualResetEvent allDone = new ManualResetEvent(false);
  17.        
  18.         public static void Main(string[] args)
  19.         {
  20.             #region key
  21.            
  22.            
  23.             // Paste your key here
  24.            
  25.             byte[] key = new Byte[]
  26.             {
  27.                 0x01, 0x67, 0xC0, 0x43, 0xA1, 0x8C, 0xD7, 0x9A, 0x8C, 0x8C, 0x06, 0xD5, 0x51, 0x90, 0x3D, 0x00,            
  28. .....
  29.                 0x39, 0xEA, 0xED, 0xCE, 0xB5, 0x00, 0xAE, 0xD0, 0x4D, 0x9E, 0x56, 0x78, 0x02, 0xF3, 0xEA, 0x84,
  30.                 0xCE
  31.             };
  32.            
  33.             #endregion             
  34.            
  35.             AppDomain.CurrentDomain.UnhandledException += HandleUnhandledException;
  36.            
  37.             // Set cache dir etc. here. Clear these folders to force resync of all playlists
  38.            
  39.             Session s = Session.CreateInstance(key, "/tmp/libspotify", "/tmp/libspotify", "libspotify-sharp-dev");
  40.            
  41.            
  42.             s.OnConnectionError += new SessionEventHandler(HandleOnConnectionError);
  43.             s.OnLoggedOut += HandleOnLoggedOut;
  44.             s.OnLoginComplete += HandleOnLoginComplete;
  45.             s.OnLogMessage += HandleOnLogMessage;
  46.             s.OnMessageToUser += HandleOnMessageToUser;
  47.             s.OnMetaDataUpdated += HandleOnMetaDataUpdated;
  48.                                    
  49.             Console.WriteLine("Logging in...");
  50.            
  51.             // Put your credentials here
  52.             s.LogIn("user", "pass");
  53.            
  54.            
  55.             // Ugly way to terminate this little test app
  56.             allDone.WaitOne(30 * 1000, false);
  57.             Console.WriteLine("Logging out..");
  58.            
  59.             s.LogOut();
  60.         }          
  61.  
  62.         static void HandleOnMessageToUser(Session sender, SessionEventArgs e)
  63.         {          
  64.             Console.WriteLine("Message: " + e.Message);
  65.         }
  66.  
  67.         static void HandleOnLogMessage(Session sender, SessionEventArgs e)
  68.         {
  69.             Console.WriteLine("Log: " + e.Message);
  70.         }
  71.  
  72.         static void HandleOnLoginComplete(Session sender, SessionEventArgs e)
  73.         {
  74.             if(e.Status != sp_error.OK)
  75.             {
  76.                 Console.WriteLine("Error logging in");
  77.                 allDone.Set();
  78.                
  79.                 return;
  80.             }
  81.            
  82.             // Add listener for new playlists (playlist moved and deleted events are also there but are equally untested)
  83.            
  84.             if(sender != null)
  85.                 sender.PlaylistContainer.OnPlaylistAdded += HandleOnPlaylistAdded;
  86.            
  87.             Console.WriteLine("Login: {0}  Playlists: {1} ", e.Status,
  88.                               sender != null && sender.PlaylistContainer != null ? sender.PlaylistContainer.PlaylistCount.ToString() : "null");
  89.            
  90.             if(sender != null && sender.PlaylistContainer != null)
  91.             {
  92.                 Playlist[] current = sender.PlaylistContainer.CurrentLists;
  93.                 for (int i = 0; i < current.Length; i++)
  94.                 {
  95.                     Console.WriteLine("Playlist w index {0}: {1}", i, current[i].IsLoaded ? current[i].ToString() : "data not loaded");
  96.                    
  97.                     // Add change listeners to lists already in container
  98.                     current[i].OnMetadataUpdated += HandleOnMetadataUpdated;
  99.                     current[i].OnStateChanged += HandleOnStateChanged;
  100.                     current[i].OnUpdateInProgress += HandleOnUpdateInProgress;
  101.                     // Remember to set event listeners (tracks added, moved etc.) on each playlist here!
  102.                 }
  103.             }
  104.            
  105.             session = sender;
  106.         }
  107.  
  108.        
  109.         // Dump strings to console so one can see the event flow
  110.        
  111.        
  112.         static void HandleOnMetaDataUpdated(Session sender, SessionEventArgs e)
  113.         {
  114.             Console.WriteLine("Session MetaDataUpdated");
  115.         }      
  116.        
  117.         static void HandleOnPlaylistAdded(PlaylistContainer sender, PlaylistContainerEventArgs e)
  118.         {
  119.             if(session == null)
  120.                 return;
  121.            
  122.             // All current lists are in both sender.CurrentLists and e.CurrentLists (silly me...)
  123.             // Remember to set event listeners (tracks added, moved etc.) on each playlist here!
  124.            
  125.             Console.WriteLine("Playlist w index {0} added: {1}", e.Position, e.Playlist.IsLoaded ? e.Playlist.ToString() : "data is loading");
  126.             e.Playlist.OnMetadataUpdated += HandleOnMetadataUpdated;
  127.             e.Playlist.OnStateChanged += HandleOnStateChanged;
  128.             e.Playlist.OnUpdateInProgress += HandleOnUpdateInProgress;
  129.            
  130.         }
  131.  
  132.         static void HandleOnUpdateInProgress(Playlist sender, PlaylistEventArgs e)
  133.         {
  134.             Console.WriteLine("Playlist UpdateInProgress: Done {0} {1}", e.Done, sender.IsLoaded ? sender.ToString() : "data is loading");
  135.         }
  136.  
  137.         static void HandleOnStateChanged(Playlist sender, PlaylistEventArgs e)
  138.         {
  139.             Console.WriteLine("Playlist OnStateChanged: {0}", sender.IsLoaded ? sender.ToString() : "data is loading");
  140.         }
  141.  
  142.         static void HandleOnMetadataUpdated(Playlist sender, PlaylistEventArgs e)
  143.         {
  144.             Console.WriteLine("Playlist MetadataUpdated: {0}", sender.IsLoaded ? sender.ToString() : "data is loading");
  145.         }
  146.  
  147.         static void HandleOnLoggedOut(Session sender, SessionEventArgs e)
  148.         {  
  149.             Console.WriteLine("Logged out");
  150.             System.Diagnostics.Process.GetCurrentProcess().Kill();
  151.         }
  152.  
  153.         static void HandleOnConnectionError(Session sender, SessionEventArgs e)
  154.         {
  155.             Console.WriteLine("Connection error: " + e.Status);
  156.             allDone.Set();
  157.         }
  158.  
  159.         static void HandleUnhandledException(object sender, UnhandledExceptionEventArgs e)
  160.         {
  161.             Console.WriteLine(e.ExceptionObject.ToString());
  162.             allDone.Set();
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement