Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Rex

By: a guest on Feb 12th, 2010  |  syntax: None  |  size: 3.53 KB  |  hits: 65  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using iTunesLib;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10.  
  11. /* TODO:
  12.  * - Hide console window
  13.  * - Debug event firing
  14.  * - Change icon
  15.  * - Traverse to genius recommendations?
  16.  */
  17. namespace iTunesPlaylistTrayApp
  18. {
  19.     public class SysTrayApp : Form
  20.     {
  21.         [STAThread]
  22.         public static void Main()
  23.         {
  24.             Application.Run(new SysTrayApp());
  25.         }
  26.  
  27.         static iTunesApp itapp;
  28.         static IITLibraryPlaylist lastPlaylist;
  29.         public NotifyIcon trayIcon;
  30.         private ContextMenu trayMenu;
  31.  
  32.        
  33.         public SysTrayApp()
  34.         {
  35.            
  36.             // Create a simple tray menu with only one item.
  37.             trayMenu = new ContextMenu();
  38.             trayMenu.MenuItems.Add("Exit", OnExit);
  39.  
  40.             // Create a tray icon. In this example we use a
  41.             // standard system icon for simplicity, but you
  42.             // can of course use your own custom icon too.
  43.             trayIcon = new NotifyIcon();
  44.             trayIcon.Text = "Play Genius Playlist";
  45.             trayIcon.Icon = new Icon(SystemIcons.Application, 40, 40);
  46.             trayIcon.Click += new EventHandler(trayIcon_Click);
  47.  
  48.             // Add menu to tray icon and show it.
  49.             trayIcon.ContextMenu = trayMenu;
  50.             trayIcon.Visible = true;
  51.  
  52.            
  53.         }
  54.  
  55.         void trayIcon_Click(object sender, EventArgs e)
  56.         {
  57.             PlayPlaylist();
  58.         }
  59.  
  60.         //private static ManualResetEvent _resetEvent;
  61.         public static void PlayPlaylist()
  62.         {
  63.             //_resetEvent = new ManualResetEvent(false);
  64.             itapp = new iTunesApp();
  65.             itapp.OnPlayerStopEvent += new _IiTunesEvents_OnPlayerStopEventEventHandler(itapp_OnPlayerStopEvent);
  66.  
  67.             lastPlaylist = itapp.LibraryPlaylist;
  68.  
  69.             itapp.Play();
  70.             // Block until the resetEvent has been Set() or
  71.             // give up waiting after 5 minutes
  72.             //_resetEvent.WaitOne(1000 * 5 * 60);
  73.         }
  74.  
  75.         static void itapp_OnPlayerStopEvent(object iTrack)
  76.         {
  77.             Debug.WriteLine("Stop Event fired");
  78.            
  79.             // Determine whether the user fired the event, or whether the song finished on its own.
  80.             // if the song finished on its own (i.e. player reset to 0 seconds)
  81.             if (itapp.PlayerPosition == 0)
  82.             {
  83.                 var curList = itapp.CurrentPlaylist;
  84.  
  85.                 // If it's still in the (genius) playlist it started out in, and it's not the last song in the playlist
  86.                 if (curList == lastPlaylist && itapp.CurrentTrack != curList.Tracks[curList.Tracks.Count - 1])
  87.                 {
  88.                     itapp.NextTrack();
  89.                     itapp.Play();
  90.                 }
  91.             }
  92.         }
  93.  
  94.         protected override void OnLoad(EventArgs e)
  95.         {
  96.             Visible = false; // Hide form window.
  97.             ShowInTaskbar = false; // Remove from taskbar.
  98.  
  99.             base.OnLoad(e);
  100.         }
  101.  
  102.         private void OnExit(object sender, EventArgs e)
  103.         {
  104.             Application.Exit();
  105.         }
  106.  
  107.         protected override void Dispose(bool isDisposing)
  108.         {
  109.             if (isDisposing)
  110.             {
  111.                 // Release the icon resource.
  112.                 trayIcon.Dispose();
  113.             }
  114.  
  115.             base.Dispose(isDisposing);
  116.         }
  117.     }
  118. }