Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Accessing songs and playlists from ITunes using C#

0.00/5 (No votes)
20 Dec 2005 2  
How to get the song list, song information from ITunes using .NET.

Introduction

This is a simple example showing how to interact with ITunes version 6. The sample shows how to connect to Itunes version 6 using C# and how to get the list of songs in the itunes library or in the saved playlists.

The project was built in VS 2005. A reference to the COM object ITunes 1.6 needs to be added to your project. You can get the itunes SDK from here. The help file included in the zip was fairly useful.

Digging around on the internet, I found this site which helped me get started on this little project.

Using the code

I think the comments within the code are self explanatory:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using iTunesLib;
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
             InitializeComponent();
        }
        // initialize Itunes application connection

        iTunesApp itunes = new iTunesLib.iTunesApp();
        private void Form1_Load(object sender, EventArgs e)
        {
             // get main library playlist

             IITLibraryPlaylist mainLibrary = itunes.LibraryPlaylist;
             // get list of all playlists defined in 

             // Itunes and add them to the combobox

             foreach (IITPlaylist pl in itunes.LibrarySource.Playlists)
             {
                   comboBox1.Items.Add(pl.Name);
             }
             // get the tracks within the mainlibrary

             GetTracks((IITPlaylist)mainLibrary);
             // set the datagridview1 datasource to the datatable.

             dataGridView1.DataSource = dataTable1;
        }

        // getthe tracks from the the specified playlist

        private void GetTracks(IITPlaylist playlist)
        {
             long totalfilesize = 0;
             dataTable1.Rows.Clear();
             // get the collection of tracks from the playlist

             IITTrackCollection tracks = playlist.Tracks;
             int numTracks = tracks.Count;
             for (int currTrackIndex = 1; 
                 currTrackIndex <= numTracks; currTrackIndex++)             
             {
                  DataRow drnew = dataTable1.NewRow(); 
                  // get the track from the current tracklist                 

                  IITTrack currTrack = tracks[currTrackIndex];
                  drnew["artist"] = currTrack.Artist;
                  drnew["song name"] = currTrack.Name;
                  drnew["album"] = currTrack.Album;
                  drnew["genre"] = currTrack.Genre;
                  // if track is a fiile, then get the file 

                  // location on the drive. 

                  if (currTrack.Kind == ITTrackKind.ITTrackKindFile)
                  {
                      IITFileOrCDTrack file = (IITFileOrCDTrack)currTrack;
                      if (file.Location != null)
                      {
                           FileInfo fi = new FileInfo(file.Location);
                               if (fi.Exists)
                               {
                                    drnew["FileLocation"] = file.Location;
                                    totalfilesize += fi.Length;
                               }
                               else
                                    drnew["FileLocation"] = 
                                             "not found " + file.Location;
                      }
                  }
                  dataTable1.Rows.Add(drnew);
              }
              lbl_numsongs.Text = 
                  "number of songs: " + dataTable1.Rows.Count.ToString() + 
                  ", total file size: " + 
                  (totalfilesize / 1024.00 / 1024.00).ToString("#,### mb");
          }
          private void comboBox1_SelectedIndexChanged(object sender, 
                                                              EventArgs e)
          {
              // get list of tracks from selected playlist

              string playlist = comboBox1.SelectedItem.ToString();
              foreach (IITPlaylist pl in itunes.LibrarySource.Playlists)
              {
                   if (pl.Name == playlist)
                   {
                        GetTracks(pl);
                        break;
                   }
              }
          }
     }
}

Hope you find this useful, any comments/feedback appreciated.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here