Click here to Skip to main content
15,861,168 members
Articles / Programming Languages / XML
Article

Accessing songs and playlists from ITunes using C#

Rate me:
Please Sign up or sign in to vote.
3.80/5 (8 votes)
20 Dec 2005 137.3K   2.5K   37   26
How to get the song list, song information from ITunes using .NET.

Image 1

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:

C#
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


Written By
Web Developer
United States United States
Employed for the last 15 years at Autodesk, in San Francisco, CA.

Currently senior Software Quality Engineer in Autodesk Media Entertainment Division working on 3ds max.

Comments and Discussions

 
QuestionHow do you set Last Skipped Date and Skip Counter? I used .NextTrack() and it doesn't do anything... Pin
Member 113516295-Aug-20 8:06
Member 113516295-Aug-20 8:06 
QuestioniTunes over Network Pin
Humschi12-Jun-12 21:51
Humschi12-Jun-12 21:51 
QuestionTrouble adding tracks to a play Pin
Kombu19-Aug-11 9:24
Kombu19-Aug-11 9:24 
GeneralCom exception Pin
Member 443375531-Aug-08 23:33
Member 443375531-Aug-08 23:33 
Generalapi amazon for search iTunes songs artworks Pin
lupin9829-May-08 13:44
lupin9829-May-08 13:44 
QuestionWindows2000 Pin
Sunshine Always26-Feb-08 22:32
Sunshine Always26-Feb-08 22:32 
QuestionHow to get Smart Info and Smart Criteria Pin
guangstat13-Aug-07 20:03
guangstat13-Aug-07 20:03 
QuestionHow to get only tracks out of "MUSIC" Pin
ahaudi18-Jul-07 7:09
ahaudi18-Jul-07 7:09 
I tried oreto fach the tracks in "music" i found no possibility to sort out podcasts and Films and so on. Any suggestions on this.

some testcode.
iTunesL ib.IITTrackCollection trcol = this.Itunes.LibraryPlaylist.Search(textBox1.Text, iTunesLib.ITPlaylistSearchField.ITPlaylistSearchFieldAll);<br />
           for (int i = 1; i < trcol.Count; i++)<br />
           {<br />
               if (trcol[i].Kind == iTunesLib.ITTrackKind.ITTrackKindFile)<br />
              {<br />
<br />
                  result.AppendLine(((iTunesLib.IITFileOrCDTrack)trcol[i]).Artist.ToString() + ((iTunesLib.IITFileOrCDTrack)trcol[i]).Name.ToString());<br />
              }


i only found
trcol[i].KindAsString == "MPEG-Audiodatei")
but this seem language dependent!

by the way. good job!


servus.
Alex

QuestionHow to get the music folder location of iTunes by iTunes SDK? Pin
pudding3327-Apr-07 16:44
pudding3327-Apr-07 16:44 
Generaladd playlists in itumes Pin
Dinesh Jonnadula23-Apr-07 4:44
Dinesh Jonnadula23-Apr-07 4:44 
GeneralSweet, possible to get the filepath of track Pin
salle7824-Mar-07 3:01
salle7824-Mar-07 3:01 
Generalstreaming track from iPod to PC Pin
Halid Niyaz22-Aug-06 20:23
Halid Niyaz22-Aug-06 20:23 
Questionhow do you get at the artwork? Pin
hillscottc1-Jan-06 21:11
hillscottc1-Jan-06 21:11 
AnswerRe: how do you get at the artwork? Pin
tayspen5-Jan-06 14:32
tayspen5-Jan-06 14:32 
GeneralRe: how do you get at the artwork? Pin
scott.hill6-Jan-06 11:50
scott.hill6-Jan-06 11:50 
GeneralRe: how do you get at the artwork? Pin
tayspen7-Jan-06 6:16
tayspen7-Jan-06 6:16 
GeneralRe: how do you get at the artwork? Pin
tayspen20-Jan-06 11:19
tayspen20-Jan-06 11:19 
GeneralRe: how do you get at the artwork? Pin
yann bertaud20-Jan-06 16:14
yann bertaud20-Jan-06 16:14 
GeneralRe: how do you get at the artwork? Pin
tayspen21-Jan-06 8:48
tayspen21-Jan-06 8:48 
GeneralRe: how do you get at the artwork? Pin
Drew Noakes21-Jan-06 12:30
Drew Noakes21-Jan-06 12:30 
GeneralRe: how do you get at the artwork? Pin
tayspen27-Jan-06 0:49
tayspen27-Jan-06 0:49 
GeneralRe: how do you get at the artwork? Pin
tayspen6-Feb-06 17:09
tayspen6-Feb-06 17:09 
GeneralNice...but.... Pin
tayspen24-Dec-05 11:10
tayspen24-Dec-05 11:10 
GeneralRe: Nice...but.... Pin
robroe27-Dec-05 23:00
robroe27-Dec-05 23:00 
GeneralRe: Nice...but.... Pin
tayspen28-Dec-05 5:45
tayspen28-Dec-05 5:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.