Java and the iTunes COM interface

This entry has now been superseded by the Using the iTunes COM Interface with Java and Swing article, which contains a fuller explanation of how to use the iTunes COM interface with Java

I’ve spent the entire afternoon playing with the COM interface for iTunes in Windows. I’m currently trying to write a little application in Java that will allow me to control iTunes remotely, and in order to do this, I needed to make use of the COM interface that Apple have so kindly provided and the JCOM package which was kindly written by some lovely chaps on SourceForge. I’d previously tried using JACOB, but I was getting errors that just wouldn’t go away for several hours. I tried JCOM, and things “just worked”. I can now change the volume in iTunes and toggle the PlayPause state. Huzzah!

import jp.ne.so_net.ga2.no_ji.jcom.*;

class JComPrototype
{
  public static void main(String args[]) throws Exception
  {
    ReleaseManager rm = new ReleaseManager();
    try {

      IDispatch iTunes = new IDispatch(rm, "iTunes.Application");
      iTunes.put("SoundVolume", args[0]);
      iTunes.method("PlayPause", null);

    } catch(Exception e) {
      e.printStackTrace();

    } finally {
      rm.release();
    }
  }
}

Okay, I know it’s not much, but I wasn’t able to find anything at all on the web about controlling iTunes using Java under Windows, so I thought it might be useful to one or two people.

UPDATE:

I’ve gone back to using JACOB, because JCOM doesn’t seem to be able to deal with events generated by COM components. Magically, JACOB seems to be working now, and I’m not entirely sure why - I don’t think I did anything different this time than I had been doing before… I’ll post a new code segment tomorrow showing how to grab iTunes events using JACOB.

permalink | View blog reactions

TrackBacks

TrackBack URL for this entry: http://www.workingwith.me.uk/cgi-bin/type/mt-tb.cgi/683

other relevant pages

about wwm

workingwith.me.uk is a resource for web developers created by Neil Crosby, a web developer who lives and works in London, England. More about the site.

Neil Crosby now blogs at The Code Train and also runs NeilCrosby.com, The Ten Word Review and Everything is Rubbish.