Thursday, February 24, 2011

Play Songs in iTunes, Navigate in Playlists

In the previous post was shown how to search songs in iTunes, today I want to describe explictely one time, how to play songs and navigate in playlists, switch to the next song etc ...
For playing a song the function Play() is needed, which is called from an object of the type IITTrack. As described in the previous post, we now know how to look for a song and then play it with Play().
The following code plays the 10th song from the music library:

foreach (IITTrack track in iTunesPlayer.LibraryPlaylist.Tracks)
    if (counter++ > 10)

A complete playlist can be played via the function PlayFirstTrack(), which is called from a playlist (an object of the type IITPlaylist).
The following function looks for a playlist matching the name given as the parameter, and then returns it:

private IITPlaylist SuchePlayList(iTunesApp iTunesPlayer, string name)
    foreach (IITPlaylist playlist in iTunesPlayer.LibrarySource.Playlists)
        if (playlist.Name == name)
            return playlist;
    return null;

If we now want to play a playlist, we call the search function with its name and then call the function PlayFirstTrack():

IITPlaylist Techno = SuchePlayList(iTunesPlayer, "Techno");
if (Techno != null)

For navigating to the next / previous song we use the functions NextTrack() and PreviousTrack(), which are called from the iTunes player object:


  1. Good code brother. You forgot a line

    iTunesApp iTunesPlayer = new iTunesApp();

    1. Thanks man! And yes you're right, that's missing. I just did not notice it, because this post is supposed to read after the previous ones about iTunes where this is mentioned.