tags:

views:

517

answers:

2

How do I create a playlist in iTunes from Java, using COM (on windows i.e. no AppleScript)?

I have found a library here that does most of what I need, and I have added some code to ITPlaylist.java, to call the "AddTrack" method (mentioned in the docs) on the iTunes object ITPlaylist :

 public void addTrack(ITTrack track) {
    ITPlaylistKind k = getKind();
    if (k == ITPlaylistKindUser) {
        Dispatch.put(object, "AddTrack", track);
    } else
        throw new UnsupportedOperationException("can only add track to user playlists - kind : " + k);
}

But I get an error :

# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x1000e150, pid=3184, tid=3896
#
# JRE version: 6.0_15-b03
# Java VM: Java HotSpot(TM) Client VM (14.1-b02 mixed mode, sharing windows-x86)
# Problematic frame:
# C [jacob-1.14.3-x86.dll+0xe150]
#
# An error report file with more information is saved as:
# Z:\\hs_err_pid3184.log
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.

when I try and call this method. The log file contains:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x1000e150, pid=3040, tid=3756
#
# JRE version: 6.0_15-b03
# Java VM: Java HotSpot(TM) Client VM (14.1-b02 mixed mode, sharing windows-x86 )
# Problematic frame:
# C  [jacob-1.14.3-x86.dll+0xe150]
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

---------------  T H R E A D  ---------------

Current thread (0x01ed1400):  JavaThread "main" [_thread_in_native, id=3756, stack(0x003b0000,0x00400000)]

siginfo: ExceptionCode=0xc0000005, reading address 0x00000005

Registers:
EAX=0x00000000, EBX=0x0490f6f8, ECX=0x003ffd8c, EDX=0x240b14e8
ESP=0x003ff9d0, EBP=0x003ffa14, ESI=0x01ed1510, EDI=0x00000005
EIP=0x1000e150, EFLAGS=0x00010246

Top of Stack: (sp=0x003ff9d0)
0x003ff9d0:   01ed1400 28404d90 28404d90 01fd9e27
0x003ff9e0:   01ed1510 003ffa28 003ffa24 01ed1400
0x003ff9f0:   2840bbaf 003ff9f4 00000000 003ffa28
0x003ffa00:   28409fb8 00000000 28404d90 00000000
0x003ffa10:   003ffa24 003ffa4c 01fd2da1 00000000
0x003ffa20:   01fd8259 240b14e8 240b39d8 003ffa2c
0x003ffa30:   2840401a 003ffa58 28409fb8 00000000
0x003ffa40:   28404040 003ffa24 003ffa54 003ffa7c 

Instructions: (pc=0x1000e150)
0x1000e140:   1d 53 ff 15 20 01 01 10 66 c7 03 09 00 89 7b 08
0x1000e150:   8b 0f 8b 51 04 57 ff d2 5f 5e 5b c2 0c 00 6a ff 


Stack: [0x003b0000,0x00400000],  sp=0x003ff9d0,  free space=318k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [jacob-1.14.3-x86.dll+0xe150]
j  com.jacob.com.Variant.putObject(Ljava/lang/Object;)V+2
j  com.jacob.com.VariantUtilities.populateVariant(Lcom/jacob/com/Variant;Ljava/lang/Object;Z)V+511
j  com.jacob.com.VariantUtilities.objectToVariant(Ljava/lang/Object;)Lcom/jacob/com/Variant;+248
j  com.jacob.com.VariantUtilities.objectsToVariants([Ljava/lang/Object;)[Lcom/jacob/com/Variant;+19
j  com.jacob.com.Dispatch.invoke(Lcom/jacob/com/Dispatch;Ljava/lang/String;I[Ljava/lang/Object;[I)Lcom/jacob/com/Variant;+8
j  com.jacob.com.Dispatch.put(Lcom/jacob/com/Dispatch;Ljava/lang/String;Ljava/lang/Object;)V+18
j  com.dt.iTunesController.ITPlaylist.addTrack(Lcom/dt/iTunesController/ITTrack;)V+19
j  uk.co.teamnomad.itunes.WindowsLibraryImpl.savePlaylist(Ljava/util/List;Luk/co/teamnomad/itunes/PlayList;)V+46
j  uk.co.teamnomad.bpm.main.Main.<init>(Luk/co/teamnomad/bpm/system/OperatingSystem;)V+309
j  uk.co.teamnomad.bpm.main.Main.getInstance()Luk/co/teamnomad/bpm/main/Main;+16
j  uk.co.teamnomad.bpm.main.Main.main([Ljava/lang/String;)V+8
v  ~StubRoutines::call_stub
V  [jvm.dll+0xecfac]
V  [jvm.dll+0x1741d1]
V  [jvm.dll+0xed02d]
V  [jvm.dll+0xf5bf5]
V  [jvm.dll+0xfd84d]
C  [java.exe+0x2155]
C  [java.exe+0x82ce]
C  [kernel32.dll+0x44911]
C  [ntdll.dll+0x3e4b6]
C  [ntdll.dll+0x3e489]

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  com.jacob.com.Variant.putVariantDispatch(Ljava/lang/Object;)V+0
j  com.jacob.com.Variant.putObject(Ljava/lang/Object;)V+2
j  com.jacob.com.VariantUtilities.populateVariant(Lcom/jacob/com/Variant;Ljava/lang/Object;Z)V+511
j  com.jacob.com.VariantUtilities.objectToVariant(Ljava/lang/Object;)Lcom/jacob/com/Variant;+248
j  com.jacob.com.VariantUtilities.objectsToVariants([Ljava/lang/Object;)[Lcom/jacob/com/Variant;+19
j  com.jacob.com.Dispatch.invoke(Lcom/jacob/com/Dispatch;Ljava/lang/String;I[Ljava/lang/Object;[I)Lcom/jacob/com/Variant;+8
j  com.jacob.com.Dispatch.put(Lcom/jacob/com/Dispatch;Ljava/lang/String;Ljava/lang/Object;)V+18
j  com.dt.iTunesController.ITPlaylist.addTrack(Lcom/dt/iTunesController/ITTrack;)V+19
j  uk.co.teamnomad.itunes.WindowsLibraryImpl.savePlaylist(Ljava/util/List;Luk/co/teamnomad/itunes/PlayList;)V+46
j  uk.co.teamnomad.bpm.main.Main.<init>(Luk/co/teamnomad/bpm/system/OperatingSystem;)V+309
j  uk.co.teamnomad.bpm.main.Main.getInstance()Luk/co/teamnomad/bpm/main/Main;+16
j  uk.co.teamnomad.bpm.main.Main.main([Ljava/lang/String;)V+8
v  ~StubRoutines::call_stub

---------------  P R O C E S S  ---------------

Java Threads: ( => current thread )
  0x043a4800 JavaThread "MerapiBridgeSocket" [_thread_in_native, id=3664, stack(0x04800000,0x04850000)]
  0x01f4c800 JavaThread "Low Memory Detector" daemon [_thread_blocked, id=2440, stack(0x04160000,0x041b0000)]
  0x01f47c00 JavaThread "CompilerThread0" daemon [_thread_blocked, id=3628, stack(0x04110000,0x04160000)]
  0x01f47400 JavaThread "Attach Listener" daemon [_thread_blocked, id=3584, stack(0x040c0000,0x04110000)]
  0x01f3ac00 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=2968, stack(0x04070000,0x040c0000)]
  0x01ef8c00 JavaThread "Finalizer" daemon [_thread_blocked, id=3724, stack(0x04020000,0x04070000)]
  0x01ef7800 JavaThread "Reference Handler" daemon [_thread_blocked, id=3324, stack(0x03fd0000,0x04020000)]
=>0x01ed1400 JavaThread "main" [_thread_in_native, id=3756, stack(0x003b0000,0x00400000)]

Other Threads:
  0x01ef6000 VMThread [stack: 0x01180000,0x011d0000] [id=2836]
  0x01f5f000 WatcherThread [stack: 0x041b0000,0x04200000] [id=3528]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap
 def new generation   total 960K, used 794K [0x24000000, 0x24100000, 0x244e0000)
  eden space 896K,  81% used [0x24000000, 0x240b7510, 0x240e0000)
  from space 64K,  95% used [0x240f0000, 0x240ff450, 0x24100000)
  to   space 64K,   0% used [0x240e0000, 0x240e0000, 0x240f0000)
 tenured generation   total 4096K, used 876K [0x244e0000, 0x248e0000, 0x28000000)
   the space 4096K,  21% used [0x244e0000, 0x245bb318, 0x245bb400, 0x248e0000)
 compacting perm gen  total 12288K, used 4255K [0x28000000, 0x28c00000, 0x2c000000)
   the space 12288K,  34% used [0x28000000, 0x28427cb8, 0x28427e00, 0x28c00000)
    ro space 8192K,  63% used [0x2c000000, 0x2c519920, 0x2c519a00, 0x2c800000)
    rw space 12288K,  53% used [0x2c800000, 0x2ce74dd0, 0x2ce74e00, 0x2d400000)

Dynamic libraries:
0x00400000 - 0x00424000  C:\Windows\system32\java.exe
0x771a0000 - 0x772c7000  C:\Windows\system32\ntdll.dll
0x75a20000 - 0x75afb000  C:\Windows\system32\kernel32.dll
0x75ee0000 - 0x75fa6000  C:\Windows\system32\ADVAPI32.dll
0x772e0000 - 0x773a3000  C:\Windows\system32\RPCRT4.dll
0x7c340000 - 0x7c396000  C:\Program Files\Java\jre6\bin\msvcr71.dll
0x6d800000 - 0x6da8b000  C:\Program Files\Java\jre6\bin\client\jvm.dll
0x77060000 - 0x770fd000  C:\Windows\system32\USER32.dll
0x773b0000 - 0x773fb000  C:\Windows\system32\GDI32.dll
0x744f0000 - 0x74522000  C:\Windows\system32\WINMM.dll
0x76f30000 - 0x76fda000  C:\Windows\system32\msvcrt.dll
0x76d20000 - 0x76e64000  C:\Windows\system32\ole32.dll
0x75d20000 - 0x75dad000  C:\Windows\system32\OLEAUT32.dll
0x744b0000 - 0x744e9000  C:\Windows\system32\OLEACC.dll
0x77100000 - 0x7711e000  C:\Windows\system32\IMM32.DLL
0x76c00000 - 0x76cc8000  C:\Windows\system32\MSCTF.dll
0x75820000 - 0x7584c000  C:\Windows\system32\apphelp.dll
0x772d0000 - 0x772d9000  C:\Windows\system32\LPK.DLL
0x77120000 - 0x7719d000  C:\Windows\system32\USP10.dll
0x6d7b0000 - 0x6d7bc000  C:\Program Files\Java\jre6\bin\verify.dll
0x6d330000 - 0x6d34f000  C:\Program Files\Java\jre6\bin\java.dll
0x6d290000 - 0x6d298000  C:\Program Files\Java\jre6\bin\hpi.dll
0x75a10000 - 0x75a17000  C:\Windows\system32\PSAPI.DLL
0x6d7f0000 - 0x6d7ff000  C:\Program Files\Java\jre6\bin\zip.dll
0x6d610000 - 0x6d623000  C:\Program Files\Java\jre6\bin\net.dll
0x76e70000 - 0x76e9d000  C:\Windows\system32\WS2_32.dll
0x75fb0000 - 0x75fb6000  C:\Windows\system32\NSI.dll
0x75030000 - 0x7506b000  C:\Windows\system32\mswsock.dll
0x75090000 - 0x75095000  C:\Windows\System32\wship6.dll
0x74cf0000 - 0x74cf5000  C:\Windows\System32\wshtcpip.dll
0x10000000 - 0x10019000  C:\Windows\System32\jacob-1.14.3-x86.dll
0x72640000 - 0x726db000  C:\Windows\WinSxS\x86_microsoft.vc80.crt_1fc8b3b9a1e18e3b_8.0.50727.1434_none_d08b6002442c891f\MSVCR80.dll
0x76ea0000 - 0x76f24000  C:\Windows\system32\CLBCatQ.DLL
0x74db0000 - 0x74deb000  C:\Windows\system32\rsaenh.dll

VM Arguments:
java_command: bpm-0.0.13-jar-with-dependencies.jar
Launcher Type: SUN_STANDARD

Environment Variables:
CLASSPATH=.;C:\Program Files\QuickTime\QTSystem\QTJava.zip
PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files\QuickTime\QTSystem\
USERNAME=ed
OS=Windows_NT
PROCESSOR_IDENTIFIER=x86 Family 6 Model 23 Stepping 6, GenuineIntel



---------------  S Y S T E M  ---------------

OS: Windows Vista Build 6001 Service Pack 1

CPU:total 1 (2 cores per cpu, 1 threads per core) family 6 model 23 stepping 6, cmov, cx8, fxsr, mmx, sse, sse2, sse3, ssse3, sse4.1

Memory: 4k page, physical 2097151k(1656488k free), swap 4194303k(4092420k free)

vm_info: Java HotSpot(TM) Client VM (14.1-b02) for windows-x86 JRE (1.6.0_15-b03), built on Jul 25 2009 01:22:46 by "java_re" with MS VC++ 7.1

time: Mon Aug 31 20:14:48 2009
elapsed time: 7 seconds

What am I doing wrong ?

+1  A: 

I havent done any iTunes stuff in Java for a while, but from what experience I have I remember that the interface can be rather fickle.

I don't have any definitive answer, but in attempt to provide at least a shot in the dark, maybe you should try casting the ITPlaylist you're working with to an ITUserPlaylist (if that's the correct type, of course) and then calling its addTrack method. That way you'd be using the function provided by the library, and if that doesn't work you can get in touch with the developer because there may be a more serious problem occurring.

From the dump you provided, it looks as though the underlying C in JACOB is trying to modify the equivalent ITPlaylist via the iTunes COM interface. This could be because of the way the inheritance is working? IE: ITPlaylist doesn't support addTrack (the underlying C is trying to call a nonexistent function) but ITUserPlaylist does? Just a thought.

Suggested correction:

public void addTrack(ITTrack track) {
    ITPlaylistKind k = getKind();
    if (k == ITPlaylistKindUser) {
        Dispatch.put( ((ITUserPlaylist)this).object, "AddTrack", track);
    } else
        throw new UnsupportedOperationException("can only add track to user playlists - kind : " + k);
}

Again I'm certainly not the expert here so if anyone has any better ideas, please go ahead and post over me :)

Let me know if that offers you any success!

Edit After re-reading my post, I realized I forgot this approach, which could arguably be better as it keeps the JACOB COM bridge out of the higher level stuff:

((ITUserPlaylist)this).addTrack(track);
Faxwell Mingleton
I've voted you up cos you seem pretty much right, in your reading of the problem, but I don't really understand your suggested correction. How can the type of java object which is not passed into the COM API (it merely contains the object that is) can affect the behaviour of iTunes.
l0st3d
@l0st3d I don't have much familiarity with that library you're using (I've never used anything on top of JACOB with the iTunes SDK) so I wasn't sure what the library was actually doing when it makes those objects, or where the dispatch for the object comes from. Really, though, the hope was to try to reduce the case down to using ITUserPlaylist's addTrack method to eliminate user error and narrow it down to the API, which you obviously already accomplished. Sorry if I posted anything potentially confusing, just figured I might be able to help since I've been through these hoops before.
Faxwell Mingleton
@lurkingfridge79 - any and all help is appreciated :)
l0st3d
+1  A: 

The answer for me was to contact the developer of the library I was using, getting him to release a new version of the library. If you want to do this then use at least version 0.2, which will correctly differentiate between ITPlaylist objects and ITUserPlaylist ones, which (as mentioned by lurkingfridge79 are the only types of playlist that can have tracks added to them by the COM API.

l0st3d
Did you try doing a cast at all as I mentioned? I'm curious to see if that would work at all or if it would do funny things to the COM bridge...
Faxwell Mingleton
no, sorry, I didn't - I'd already got the updated library before I came back to SO and saw your reply. I'm not really sure that it will work though.
l0st3d
After replying above I'm in agreement thinking that almost certainly would fail. Glad to see there's been a library update though!
Faxwell Mingleton