help: how to implement a timer/thread/runloop?
i am writing a basic app to display the name of current song playing in iTunes.
I have an NSTextField to output to a Label, and a function that gets the name from iTunes.
I am looking for a way to automate the process. I want the app to fetch the name every 1 sec or when the song changes.
Whats the way to go here?
thanks
You need to set up an observation. NSNotificationCenter is what you're looking for.
[[NSNotificationCenter defaultCenter] addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender]
Look in the iTunes.h file to get the correct name of the observation. Have the selector change the NSTextField.
I managed to try some notifications, and they worked.
Should I have the object of the notification point to itunes current track?
Because I tried that but didn't work.
Here is the itunes.h in .h and .txt.
http://dl.dropbox.com/u/349788/iTunes.h
http://dl.dropbox.com/u/349788/iTunes.txt
What should I point?
In your header declare iTunes as iTunesApplication and trackInfo as iTunesTrack. (iTunesApplication *iTunes, etc.)
iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(updateData) name:@"com.apple.iTunes.playerInfo" object:nil];
In my updateData, I have this.
trackInfo = [iTunes currentTrack];
This takes the current track info and stores it in a variable. From that, I can use trackInfo for all the information.
[trackInfo name]
[trackInfo album]
[trackInfo artist]