Allen Hsu
Allen Hsu

Reputation: 3534

How to get song information from iTunes?

I'm developing an app for Mac OS X to display lyric of the song iTunes is playing. I've done some searches and found only the way using AppleScript to realize it. Is there an API for Objective-C that I can use to fetch information about the song from iTunes? I want to know how Bowtie, CoverSutra and Last.fm did it.

Upvotes: 3

Views: 4388

Answers (3)

Parag Bafna
Parag Bafna

Reputation: 22930

In 10.9 you can use iTunes Library Framework (/Library/Frameworks/iTunesLibrary.framework) for iTunes 11.

#import <iTunesLibrary/ITLibrary.h>

NSError *error = nil;
ITLibrary *library = [ITLibrary libraryWithAPIVersion:@"1.0" error:&error];
if (library)
{
        NSArray *playlists = library.allPlaylists; //  <- NSArray of ITLibPlaylist
        NSArray *tracks = library.allMediaItems; //  <- NSArray of ITLibMediaItem
}

Upvotes: 4

user142019
user142019

Reputation:

See the answer to my question:

Use the Scripting Bridge to ask iTunes. iTunes is even the example that the docs use.

It can be a bit tricky in the beginning, but as always with Cocoa: it's easy after you've done it once.

Upvotes: 1

Daniel Dickison
Daniel Dickison

Reputation: 21882

There's no Apple-supplied API that I know of, other than the AppleScript interface.

But it just so happens that iTunes posts distributed notifications whenever the track changes, so you can listen for those and get the currently playing song info that way.

See Objective-C Mac OS X Distributed notifications iTunes

I've never seen any official documentation of these notifications so I don't know if you can count on them working in the future, but they've worked for like the past decade.

Upvotes: 1

Related Questions