views:

97

answers:

1

Is there any iTunes API for the iPhone OS which I can use to access some information (current track, etc.) from iTunes?

I looked around, but all I could find was some AppleScript and COM API.

+2  A: 

You need to add MediaPlayer.framework to your target in Xcode and #import MediaPlayer/MediaPlayer.h

then

something like

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

then in viewDidLoad you need to register for the event

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

implement handleNowPlayingItemChanged now

When the now playing item changes you wll be notified by getting called this method

- (void)handleNowPlayingItemChanged:(id)notification {
    // Ask the music player for the current song.
    MPMediaItem *currentItem = self.musicPlayer.nowPlayingItem;
mihirpmehta
Thanks a lot Mihir!
Baishampayan Ghose