Click here to Skip to main content
Licence 
First Posted 31 Oct 2006
Views 71,508
Bookmarked 37 times

iTunes Control Using VB.NET

By | 28 Nov 2006 | Article
Exploring the iTunes interface using VB.

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

About the Author

Michael_Davies



United Kingdom United Kingdom

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionEditing PinmemberMember 88023448:54 12 Apr '12  
AnswerRe: Editing Pinmembersamyeling9:08 12 Apr '12  
GeneralInterop.iTunesLib.dll Pinmembersumedhaswamy15:39 2 Sep '10  
GeneralRe: Interop.iTunesLib.dll PinmemberMichael_Davies21:34 2 Sep '10  
GeneralGetting the Track Name to update itself PinmemberI, the Deceiver20:09 27 Aug '10  
GeneralListing all songs Pinmemberehrenberg12:05 15 Jun '10  
GeneralRe: Listing all songs PinmemberMichael_Davies8:01 16 Jun '10  
GeneralRe: Listing all songs Pinmemberehrenberg6:22 17 Jun '10  
GeneralMy vote of 2 PinmvpDave Kreskowiak18:17 28 Aug '09  
GeneralRe: My vote of 2 PinmemberMichael_Davies7:54 16 Jun '10  
GeneralClosing iTunes Window Pinmembersvalerio4:00 4 Jun '08  
GeneralRe: Closing iTunes Window PinmemberMichael_Davies23:39 4 Jun '08  
GeneralRe: Closing iTunes Window PinmemberMichael_Davies23:54 4 Jun '08  
QuestionPlaying specific songs Pinmemberpftburger11:55 8 Oct '07  
AnswerRe: Playing specific songs PinmemberMichael_Davies3:18 9 Oct '07  
Hi,
 
I have not used it before but you can get an object using an ID. So your source machine would get the ID of the next track to play and send it to the target, the target can then find the track object using the ID information.
 
So long as your two libraries were the same it should work, otherwise a different track would play on the target.
 
I do not know if you have it or not but the iTunes SDK might help, a little...
 
For the Source use the following function:
 
HRESULT IITObject::GetITObjectIDs ( [out] long * sourceID,
[out] long * playlistID,
[out] long * trackID,
[out] long * databaseID
)

Returns the four IDs that uniquely identify this object.
 
This method is not usable from scripting clients, since it returns multiple output parameters. Scripting clients must use the IITObject::SourceID(), IITObject::PlaylistID(), IITObject::TrackID(), and IITObject::TrackDatabaseID() properties instead.
 
These are runtime IDs, they are only valid while the current instance of iTunes is running.
 
Parameters:
sourceID Returns the ID that identifies the source. Valid for a source, playlist, or track.
 
playlistID Returns the ID that identifies the playlist. Valid for a playlist or track. Will be zero for a source.
 
trackID Returns the ID that identifies the track within the playlist. Valid for a track. Will be zero for a source or playlist.
 
databaseID Returns the ID that identifies the track, independent of its playlist. Valid for a track. Will be zero for a source or playlist.
 
Return values:
S_OK The operation was successful.
E_POINTER sourceID, playlistID, trackID, or databaseID is NULL.
ITUNES_E_OBJECTDELETED The source, playlist, or track corresponding to this object has been deleted.
E_FAIL An unexpected error occurred.

 
For the target use to following function:
 
HRESULT IiTunes::GetITObjectByID ( [in] long sourceID,
[in] long playlistID,
[in] long trackID,
[in] long databaseID,
[out, retval] IITObject ** iObject
)

Returns an IITObject corresponding to the specified IDs.
 
The object may be a source, playlist, or track.
 

Parameters:
 
sourceID The ID that identifies the source. Valid for a source, playlist, or track.
 
playlistID The ID that identifies the playlist. Valid for a playlist or track. Must be zero for a source.
 
trackID The ID that identifies the track within the playlist. Valid for a track. Must be zero for a source or playlist.
 
databaseID The ID that identifies the track, independent of its playlist. Valid for a track. Must be zero for a source or playlist.
 
iObject Returns an IITObject object corresponding to the specified IDs. Will be set to NULL if no object could be retrieved.
 
Return values:
S_OK The operation was successful.
S_FALSE An object with the specified IDs could not be found.
E_INVALIDARG Invalid combination of IDs.
E_POINTER iObject is NULL.
E_FAIL An unexpected error occurred.

Cast the iObject to an IITTrack and execute the play method.
 
Good luck.
M.
GeneralRe: Playing specific songs Pinmemberpftburger3:31 9 Oct '07  
GeneralRe: Playing specific songs PinmemberJoeRip18:13 10 Nov '07  
Generalnice PinmemberTollyver21:25 9 Apr '07  
GeneralRe: nice PinmemberMichael_Davies3:42 13 Apr '07  
GeneralNice! PinmemberLeonardo Paneque15:55 29 Nov '06  
GeneralRe: Nice! Pinmembercrossreferance0:35 3 Feb '07  
GeneralRe: Nice! PinmemberMichael_Davies3:39 13 Apr '07  
GeneralITDETECTORLib Pinmembersheepy8988:38 26 Nov '06  
GeneralRe: ITDETECTORLib [modified] PinmemberMichael_Davies21:44 26 Nov '06  
GeneralRe: ITDETECTORLib Pinmembersheepy89818:17 17 Dec '06  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 28 Nov 2006
Article Copyright 2006 by Michael_Davies
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid