找到适用于Java的iTunes Controller API,我该如何播放特定歌曲?

时间:2013-04-10 02:25:07

标签: java api com itunes jacob

        //Using iTunes Controller
        iTunes itc = new iTunes();
        itc.playFile(filePath); // Takes type String

似乎是正确的行动方案。但是,我希望用户能够简单地指定歌曲标题......

我可以使用提示让艺术家和专辑看到要浏览的文件夹,因为这就是iTunes存储文件的方式......(例如C:\ Users \ username \ Music \ iTunes \ iTunes Media \ Artist \专辑\曲)

有没有人知道我可以直接进入指定歌曲的方式?我一直在寻找。

以下是我正在使用的API的文档,如果这有助于http://www.dot-totally.co.uk/software/itunescon/javadoc-0.2/index.html

UPDATE ----------------------------------

所以我开始......

//Using iTunes Controller -- Still doesn't work
iTunes itc = new iTunes();
ITSourceCollection sc = (ITSourceCollection) itc.getSources();
ITSource source = sc.getItemByName(song);
int trackID = source.getTrackID();
// Now what to do with the track id? Look for getTrack by ID, then track.play();
// Found that a TrackCollection can return a Track by ID.
// Need to find out how to get the TrackCollection of the library

我被卡住了...... :(

编辑:

想象我可以根据我从sourcecollection获得的信息手动创建一个轨道。但是对构造函数感到困惑......

ITTrack(com.jacob.com.Dispatch d)

????任何人都可以澄清创建ITTrack对象的正确语法是什么? 这是javadoc,我不明白。

http://www.dot-totally.co.uk/software/itunescon/javadoc-0.2/com/dt/iTunesController/ITTrack.html

UPDATE --------------------------------------

确定。所以我使用fetchDispatch()方法创建了一个ITTrack类。 http://www.dot-totally.co.uk/software/itunescon/javadoc-0.2/com/dt/iTunesController/ITObject.html#fetchDispatch()

//Using iTunes Controller -- work in progress
iTunes itc = new iTunes();
ITSourceCollection libsource = (ITSourceCollection) itc.getSources();
ITSource trackToPlay = libsource.getItemByName(song);
ITTrack track = new ITTrack(trackToPlay.fetchDispatch());
track.play();

我现在得到一个例外:

Exception in thread "main" java.lang.NoSuchMethodError: com.jacob.com.Dispatch.call(Lcom/jacob/com/Dispatch;Ljava/lang/String;Ljava/lang/Object;)Lcom/jacob/com/Variant;
at com.dt.iTunesController.ITSourceCollection.getItemByName(ITSourceCollection.java:49)
at Build.Clapper3.process(-----.java:117)
at Build.Clapper3.main(-----.java:232)
gahhh如此接近!所以我对我的项目“名称”的输入方法做错了什么......但是什么?

我想也许如果我输入:

System.out.println(libsource.toString());

找到源的名称....但我猜它没有toString()方法? 输出是:

com.dt.iTunesController.ITSourceCollection@118278a

1 个答案:

答案 0 :(得分:0)

我最终废弃了iTunes Controller API并使用JacobGen生成了自己的API。我发现索引1是库源&对于IITPlaylistCollection,索引1是库播放列表(所有歌曲),然后在IITTrack对象上调用play()。工作得很漂亮,甚至打开iTunes,如果它尚未打开!

ActiveXComponent iTunesCom = new ActiveXComponent("iTunes.Application");
Dispatch iTunesController = new Dispatch(iTunesCom.getObject());
IiTunes it = new IiTunes(iTunesController);
IITSourceCollection sourceList = it.getSources();
IITSource s = sourceList.getItem(1); // Index 1 is library source
IITPlaylistCollection pc = s.getPlaylists();
IITPlaylist p = pc.getItem(1); // Index 1 is library playlist
IITTrackCollection tracks = p.getTracks();
IITTrack track = tracks.getItemByName(songName);
track.play();

与播放列表类似:

ActiveXComponent iTunesCom = new ActiveXComponent("iTunes.Application");
Dispatch iTunesController = new Dispatch(iTunesCom.getObject());
IiTunes it = new IiTunes(iTunesController);
IITSourceCollection sourceList = it.getSources();
IITSource s = sourceList.getItem(1); // Index 1 is library source
IITPlaylistCollection pc = s.getPlaylists();
IITPlaylist playlist = pc.getItemByName(playlistName);
playlist.playFirstTrack();

感谢您的所有指示,希望这可以帮助任何有类似问题的人。我花了很长时间才弄明白如何让JacobGen工作,因为互联网上几乎没有任何文档。如果有人有疑问,我会很高兴发表一篇关于它的帖子。