【问题标题】:Found an iTunes Controller API for Java, how can I play a specific song?找到适用于 Java 的 iTunes 控制器 API,我如何播放特定歌曲?
【发布时间】:2013-04-10 02:25:07
【问题描述】:
        //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

更新-----------------------------------

所以我起身......

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

更新---------------------------------------

好的。所以我使用了 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)

呸呸呸呸!所以我在输入项目“名称”的方法上做错了......但是什么?

我想如果我输入:

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

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

com.dt.iTunesController.ITSourceCollection@118278a

【问题讨论】:

  • 我会多阅读一些文档并尝试一些事情。首先查看iTunes#getSelectedTracks。最终,当你深入到ITrack 时,它有一个play 方法
  • getSelectedTracks 似乎只是返回由鼠标左键单击选择的轨道。我试图设置它的方式是用户输入一个歌曲标题,它被存储在一个字符串中。然后,该字符串用于通过我需要弄清楚的某种方法来指定要播放的歌曲。
  • 对不起,我的意思是getSources
  • :) 看看,很好的发现!
  • 我不知道这是否有帮助,但在文档Typically, an ITrack is accessed through an ITTrackCollection. You can retrieve all the tracks defined for a playlist using ITPlaylist.getTracks(); 中我看到ITSourcegetPlaylists() 所以也许你可以用它做点什么?但是我对这个 api 一无所知。

标签: java api com itunes jacob


【解决方案1】:

我最终废弃了 iTunes 控制器 API 并使用 JacobGen 生成了自己的。我发现索引 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 工作,因为互联网上几乎没有任何文档。如果有人有任何问题,我很乐意发帖讨论。

【讨论】:

    猜你喜欢
    • 2013-03-30
    • 2011-04-05
    • 1970-01-01
    • 2013-08-27
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多