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)
    {
        track.Play();
        break;
    }
}

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)
    Techno.PlayFirstTrack();

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

Wednesday, February 23, 2011

Look for Titles in iTunes

After I showed in the previous post how to traverse the library of iTunes, I now want to show a possibility to search specific titles. For that we traverse as described the library or specific playlists and check, if our search criteria are met.
The function shown below looks for artist and title and then returns the desired track. In the case of a complete search of course multiple results can be found, a list might be a good solution for that.

private IITTrack Suche(iTunesApp iTunesPlayer, string artist, string title)
{
    foreach (IITTrack track in iTunesPlayer.LibraryPlaylist.Tracks)
    {
        if (track.Artist == artist && track.Name == title)
            return track;
    }
    return null;
}

The search function could be included as follows, the following lines of code search for the song "Saturday" by "Basshunter" and then play it:

IITTrack Ergebnis = Suche(iTunesPlayer, "Basshunter""Saturday");
if (Ergebnis != null)
    Ergebnis.Play();

Thursday, February 17, 2011

Traverse iTunes Music Library

In this post I want to show, how to traverse the complete iTunes music library or single playlists using C#.
The previous post is required for that (including iTunes in C#).
Now let iTunesPlayer again be an object of the type iTunesApp, referencing iTunes. Then the property iTunesPlayer.LibraryPlaylist.Tracks returns all media files loaded by iTunes as objects of type IITTrack.
The following sample code writes all titles to a textbox (which is textBox1, Multiline has to be true and Scrollbars enabled):

iTunesApp iTunesPlayer = new iTunesApp();
foreach (IITTrack track in iTunesPlayer.LibraryPlaylist.Tracks)
{
    textBox1.Text += track.Artist + " - " + track.Name + Environment.NewLine;
}

But also specific playlists can be addressed. With the property iTunesPlayer.LibrarySource.Playlists all playlists can be accessed. Every playlist again has the property Tracks, which contains the media files in it.
The following sample code output all playlits from iTunes with all their titles  (the first playlist is the complete library):

foreach (IITPlaylist playlist in iTunesPlayer.LibrarySource.Playlists)
{
    textBox1.Text += "Playlist: " + playlist.Name + Environment.NewLine + Environment.NewLine;
    foreach (IITTrack track in playlist.Tracks)
    {
        textBox1.Text += track.Artist + " - " + track.Name + Environment.NewLine;
    }
    textBox1.Text += "--------------" + Environment.NewLine + Environment.NewLine;
}

Include iTunes

The next post series will cover the music program iTunes by Apple.
For that I will publish multiple posts, explaining different functions of the embedding.
In the first post I show, how to include iTunes to a C# program in general - therefor even no external software like the iTunes SDK is needed, already the "normal" iTunes installation will install the needed COM object.
To add such a reference to your C# project, click Project - Add Reference. There switch to the tab COM and select the object iTunes x.xx Type Library. And that is it, iTunes is ready.
For simplicity we import the corresponding library with using:
using iTunesLib;

In the program then an instance of the iTunes player can for example be instanced like this:

iTunesApp iTunesPlayer = new iTunesApp();

If this line is executed, iTunes opens and the player instance is connected to the C# object.
In the next post I describe how to traverse the music library / different playlists.

Include Google Earth

I want to show in this post, how to include Google Earth in a C# application. As for many programs, there is a library provided by Google for that purpose, the Google Earth API.
But for the simple basics used here, we don't even need this API, because Google provides an online plugin, which can be viewed in the browser.
This plugin is available on the URL http://earth-api-samples.googlecode.com/svn/trunk/demos/desktop-embedded/pluginhost.html, we just have to open this page with a Webbrowser control on the form and immediately get the globe known of Google Earth.
A little downer is, that the end users have to install the plugin, luckily though a message with a download link is displayed in the Webbrowser, if it is missing.
The following code calls the plugin:

private void Form1_Load(object sender, EventArgs e)
{
    webBrowser1.Navigate(@"http://earth-api-samples.googlecode.com/svn/trunk/demos/desktop-embedded/pluginhost.html");
}

An exemplary application could look as follows: