Click here to Skip to main content
11,429,399 members (67,512 online)
Click here to Skip to main content

Accessing songs and playlists from ITunes using C#

, 20 Dec 2005
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

yann bertaud
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

 
QuestioniTunes over Network Pin
Humschi12-Jun-12 22:51
memberHumschi12-Jun-12 22:51 
QuestionTrouble adding tracks to a play Pin
Kombu19-Aug-11 10:24
memberKombu19-Aug-11 10:24 
GeneralCom exception Pin
Member 44337551-Sep-08 0:33
memberMember 44337551-Sep-08 0:33 
Generalapi amazon for search iTunes songs artworks Pin
lupin9829-May-08 14:44
memberlupin9829-May-08 14:44 
QuestionWindows2000 Pin
Sunshine Always26-Feb-08 23:32
memberSunshine Always26-Feb-08 23:32 
QuestionHow to get Smart Info and Smart Criteria Pin
guangstat13-Aug-07 21:03
memberguangstat13-Aug-07 21:03 
QuestionHow to get only tracks out of "MUSIC" Pin
ahaudi18-Jul-07 8:09
memberahaudi18-Jul-07 8:09 
QuestionHow to get the music folder location of iTunes by iTunes SDK? Pin
pudding3327-Apr-07 17:44
memberpudding3327-Apr-07 17:44 
Generaladd playlists in itumes Pin
trxrst23-Apr-07 5:44
membertrxrst23-Apr-07 5:44 
GeneralSweet, possible to get the filepath of track Pin
salle7824-Mar-07 4:01
membersalle7824-Mar-07 4:01 
Generalstreaming track from iPod to PC Pin
Halid Niyaz22-Aug-06 21:23
memberHalid Niyaz22-Aug-06 21:23 
Questionhow do you get at the artwork? Pin
hillscottc1-Jan-06 22:11
memberhillscottc1-Jan-06 22:11 
AnswerRe: how do you get at the artwork? Pin
tayspen5-Jan-06 15:32
membertayspen5-Jan-06 15:32 
Well if by "get at" you mean download and put into iTunes. I deided i wanted to attempt this. I think i have most if it....

This is what i used...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WindowsApplication1.Amazon;
using iTunesLib;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
iTunesApp tunes = new iTunesApp();

public Form1()
{
InitializeComponent();
}
private void Begin()
{
IITLibraryPlaylist mainLibrary = tunes.LibraryPlaylist;
GetTracks((IITPlaylist)mainLibrary);
}
private void GetTracks(IITPlaylist playlist)
{
// get the collection of tracks from the playlist
IITTrackCollection tracks = playlist.Tracks;
int numTracks = tracks.Count;
progressBar1.Maximum = numTracks;
for (int currTrackIndex = 1; currTrackIndex <= numTracks; currTrackIndex++)
{
// get the track from the current tracklist
IITTrack currTrack = tracks[currTrackIndex];
// if track is a fiile, then get the file location on the drive.
GetArt(currTrack.Artist, currTrack.Album);
System.Threading.Thread.Sleep(1000);
//currTrack.AddArtworkFromFile("C:\\" + " " + ".jpg");
progressBar1.Value += 1;
Application.DoEvents();
}

}
private static void GetArt(string artistname,string albumtitle)
{
string request = artistname + " " + albumtitle;

KeywordRequest req = new KeywordRequest();
req.keyword = request;
req.devtag = "MyDevTag :)";
req.mode = "music";
req.type = "lite";
req.page = "1";

AmazonSearchService search = new AmazonSearchService();

try
{
ProductInfo productInfo = search.KeywordSearchRequest(req);

if (productInfo.Details.Length != 0)
{
string result;
result = productInfo.Details[0].ImageUrlLarge;
System.Net.WebClient c = new System.Net.WebClient();
c.DownloadFile(result, "C:\\" + albumtitle + ".jpg");
}
}
catch
{
//do nothing..
}

Application.DoEvents();
}

private void button1_Click(object sender, EventArgs e)
{
Begin();
}
}
}


This line right here is waht causing the problem.

currTrack.AddArtworkFromFile("C:\\" + currTrack.Album + ".jpg");

it does Download the art to the C:\ drive pretty flawlessly. But perhaps somebody could tell me why setting the artwork in itunes doest work? It sayst here is a COM error...


Thanks for any help!

-T
GeneralRe: how do you get at the artwork? Pin
scott.hill6-Jan-06 12:50
memberscott.hill6-Jan-06 12:50 
GeneralRe: how do you get at the artwork? Pin
tayspen7-Jan-06 7:16
membertayspen7-Jan-06 7:16 
GeneralRe: how do you get at the artwork? Pin
tayspen20-Jan-06 12:19
membertayspen20-Jan-06 12:19 
GeneralRe: how do you get at the artwork? Pin
yann bertaud20-Jan-06 17:14
memberyann bertaud20-Jan-06 17:14 
GeneralRe: how do you get at the artwork? Pin
tayspen21-Jan-06 9:48
membertayspen21-Jan-06 9:48 
GeneralRe: how do you get at the artwork? Pin
Drew Noakes21-Jan-06 13:30
memberDrew Noakes21-Jan-06 13:30 
GeneralRe: how do you get at the artwork? Pin
tayspen27-Jan-06 1:49
membertayspen27-Jan-06 1:49 
GeneralRe: how do you get at the artwork? Pin
tayspen6-Feb-06 18:09
membertayspen6-Feb-06 18:09 
GeneralNice...but.... Pin
tayspen24-Dec-05 12:10
membertayspen24-Dec-05 12:10 
GeneralRe: Nice...but.... Pin
robroe28-Dec-05 0:00
memberrobroe28-Dec-05 0:00 
GeneralRe: Nice...but.... Pin
tayspen28-Dec-05 6:45
membertayspen28-Dec-05 6:45 
GeneralRe: Nice...but.... Pin
Neil Lamka28-Dec-05 8:57
memberNeil Lamka28-Dec-05 8:57 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150428.2 | Last Updated 21 Dec 2005
Article Copyright 2005 by yann bertaud
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid