Click here to Skip to main content
15,867,997 members
Articles / Programming Languages / Visual Basic
Article

iTunes Control Using VB.NET

Rate me:
Please Sign up or sign in to vote.
3.44/5 (8 votes)
28 Nov 2006CPOL2 min read 127.8K   1.7K   38   39
Exploring the iTunes interface using VB.

Image 1

Introduction

Long have I searched for an article on using iTunes through VB; there are many examples in C flavours, even Java, but few using VB. The iTunes SDK details what is available, but has little on usage.

Background

iTunes is a wonderful player, relatively uncomplicated in its use, no adverts or unnecessary gadget like interfaces. I had several reasons for exploring the use of my own interface to it main one being I have an iPod, and want to get information off it and back to the PC. The drive for writing this code was the annoying habit iTunes has of drag and drop of playlists, fine if you are using a mouse as you have to hold down the mouse but I was writing an application that uses a touch screen and drag and drop plays havoc with the playlists. As a result the playlists and tracklists on the form of this application are quite large to make nice landing zones for fingers.

Using the code

You do not need the iTunes SDK to access iTunes, it just helps to start to locate what you want, though I have found that some features are not exposed in the IDE. Also, some of the types do not expose either, which is frustrating as you have to remember to press Escape to prevent the nearest name, usually a collection of the same type, being filled in by the IDE. For example, typing Dim objPlayList as iTunesLib.IITPlayList will produce Dim objPlayList as iTunesLib.IITPlayListCollection, as IITPlayList is not exposed.

To use the code, you must have iTunes installed and add a reference to your project of the COM interface "iTunes  x.x Type Library" where x.x is the version installed with the iTunes version; mine was version 1.8 with version 7.0.1.8 of iTunes, and the COM interface ITDetector.

One item I can not get to work is the iTunes events, running it through the debugger shows that the event subroutines get fired but do not entirely execute for some reason; complete mystery, and if anyone solves, it I would be interested.

Points of Interest

Whilst I enjoyed solving the problem, the lack of documentation and very few examples I could find on the subject for VB did make it frustrating, which is why I decided to share this code. Good Luck.

History

Updated version. Added Try blocks for the iTunes application.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Retired
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionRemove track number from Track name as well as from file name Pin
vivianpinto6-Sep-16 19:49
vivianpinto6-Sep-16 19:49 
AnswerRe: Remove track number from Track name as well as from file name Pin
Michael_Davies8-Sep-16 8:48
Michael_Davies8-Sep-16 8:48 
GeneralRe: Remove track number from Track name as well as from file name Pin
vivianpinto8-Sep-16 21:47
vivianpinto8-Sep-16 21:47 
QuestionHow do i copy music files from selected playlist to USB drive or anywhere ? Pin
vivianpinto9-Jun-16 19:27
vivianpinto9-Jun-16 19:27 
AnswerRe: How do i copy music files from selected playlist to USB drive or anywhere ? Pin
Michael_Davies9-Jun-16 20:51
Michael_Davies9-Jun-16 20:51 
GeneralRe: How do i copy music files from selected playlist to USB drive or anywhere ? Pin
vivianpinto28-Jul-16 22:47
vivianpinto28-Jul-16 22:47 
GeneralRe: Count the music files Pin
vivianpinto17-Aug-16 3:09
vivianpinto17-Aug-16 3:09 
GeneralRe: Count the music files Pin
Michael_Davies17-Aug-16 4:20
Michael_Davies17-Aug-16 4:20 
PraiseRe: Count the music files Pin
vivianpinto18-Aug-16 20:28
vivianpinto18-Aug-16 20:28 
QuestionEditing Pin
Member 880234412-Apr-12 8:54
Member 880234412-Apr-12 8:54 
AnswerRe: Editing Pin
Mike Rabtok12-Apr-12 9:08
Mike Rabtok12-Apr-12 9:08 
GeneralInterop.iTunesLib.dll Pin
sumedhaswamy2-Sep-10 15:39
sumedhaswamy2-Sep-10 15:39 
GeneralRe: Interop.iTunesLib.dll Pin
Michael_Davies2-Sep-10 21:34
Michael_Davies2-Sep-10 21:34 
GeneralGetting the Track Name to update itself Pin
I, the Deceiver27-Aug-10 20:09
I, the Deceiver27-Aug-10 20:09 
GeneralListing all songs Pin
ehrenberg15-Jun-10 12:05
ehrenberg15-Jun-10 12:05 
GeneralRe: Listing all songs Pin
Michael_Davies16-Jun-10 8:01
Michael_Davies16-Jun-10 8:01 
GeneralRe: Listing all songs Pin
ehrenberg17-Jun-10 6:22
ehrenberg17-Jun-10 6:22 
GeneralMy vote of 2 Pin
Dave Kreskowiak28-Aug-09 18:17
mveDave Kreskowiak28-Aug-09 18:17 
GeneralRe: My vote of 2 Pin
Michael_Davies16-Jun-10 7:54
Michael_Davies16-Jun-10 7:54 
GeneralClosing iTunes Window Pin
svalerio4-Jun-08 4:00
svalerio4-Jun-08 4:00 
Hi Michael,
I'm using your example to develop my custom itunes client.

I've only a problem; when I run my example (in VS 2005 using C#), system opens iTunes main windows and I don't know a trick to solve this problem.

In my code I put this line:

app.BrowserWindow.Visible = true;

but I've Exception:

System.InvalidCastException not managed
Message="Cast specified not valid"
Source="Interop.iTunesLib"
StackTrace:
in iTunesLib.IITBrowserWindow.set_Visible(Boolean isVisible)
in iTunesSample.Form1.cmdExit_Click(Object sender, EventArgs e) in C:\Documents and Settings\valeriose\Documenti\Visual Studio 2005\Projects\iTunesSample\iTunesSample\Form1.cs:riga 203
in System.Windows.Forms.Control.OnClick(EventArgs e)
in System.Windows.Forms.Button.OnClick(EventArgs e)
in System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
in System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
in System.Windows.Forms.Control.WndProc(Message& m)
in System.Windows.Forms.ButtonBase.WndProc(Message& m)
in System.Windows.Forms.Button.WndProc(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
in System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
in System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
in System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
in System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
in System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
in System.Windows.Forms.Application.Run(Form mainForm)
in iTunesSample.Program.Main() in C:\Documents and Settings\valeriose\Documenti\Visual Studio 2005\Projects\iTunesSample\iTunesSample\Program.cs:riga 17
in System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
in System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
in Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
in System.Threading.ThreadHelper.ThreadStart_Context(Object state)
in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
in System.Threading.ThreadHelper.ThreadStart()
GeneralRe: Closing iTunes Window Pin
Michael_Davies4-Jun-08 23:39
Michael_Davies4-Jun-08 23:39 
GeneralRe: Closing iTunes Window Pin
Michael_Davies4-Jun-08 23:54
Michael_Davies4-Jun-08 23:54 
QuestionPlaying specific songs Pin
pftburger8-Oct-07 11:55
pftburger8-Oct-07 11:55 
AnswerRe: Playing specific songs Pin
Michael_Davies9-Oct-07 3:18
Michael_Davies9-Oct-07 3:18 
GeneralRe: Playing specific songs Pin
pftburger9-Oct-07 3:31
pftburger9-Oct-07 3:31 

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.