从iPod上的iTunes访问当前曲目信息

时间:2010-04-14 10:22:57

标签: iphone cocoa-touch sdk itunes

我是否可以使用iPhone OS的iTunes API从iTunes获取某些信息(当前曲目等)?

我环顾四周,但我能找到的只是一些AppleScript和COM API。

1 个答案:

答案 0 :(得分:6)

您需要在Xcode和#import MediaPlayer / MediaPlayer.h中将MediaPlayer.framework添加到目标

然后

类似

@property (nonatomic, retain) MPMusicPlayerController *musicPlayer;
...
self.musicPlayer = [MPMusicPlayerController iPodMusicPlayer];

然后在viewDidLoad中你需要注册事件

// Register for music player notifications
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self 
                       selector:@selector(handleNowPlayingItemChanged:)
                           name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification 
                         object:self.musicPlayer];

实现handleNowPlayingItemChanged 现在

当正在播放的项目发生变化时,您将通过调用此方法来通知

- (void)handleNowPlayingItemChanged:(id)notification {
    // Ask the music player for the current song.
    MPMediaItem *currentItem = self.musicPlayer.nowPlayingItem;