开发者

How do I get just the audio files from the iTunes main library (returned by Scripting Bridge)?

开发者 https://www.devze.com 2023-02-19 11:43 出处:网络
I have quite a simple problem. I want to put the audiofiles into my table view. How do I distinguish them from pdfs and movies, etc?

I have quite a simple problem. I want to put the audiofiles into my table view. How do I distinguish them from pdfs and movies, etc?

I get them from iTunes over Scripting Bridge:

    iTunesSource *source = [[[self iTunes] sources] objectAtIndex:0];
    iTunesPlaylist *mainPlaylist = [[source libraryPlaylists] 开发者_高级运维objectAtIndex:0] ;
    library_ = [[NSArray arrayWithArray:[mainPlaylist tracks]] retain ] ;

This gives me an error saying the class iTunesFileTrack could not be found ( at linking time) :

[track get];
if(![track isKindOfClass:[iTunesFileTrack class]]) {
    DLog1(@"SKIPPING kind: %@", [track kind]);
}

I'm sure I'm missing something simple :)

On a related note: Is there a faster way to read the iTunes library? I just advice on loading it from an xml file but that seems unsafe to me. If apple changes anything in the next release I'm screwed.

Thank you

EDIT: With sdef /Applications/iTunes.app | sdp -fhm --basename iTunes I can generate the .m file I need to check for the class. But it does not seem to work:

[track get];         
if(![[track className] isEqualToString:@"ITunesFileTrack"]) {
    DLog1(@"SKIPPING kind: %@", [track kind]);
    continue;
}

Skipps just my streams :P Not the movies. (Even when I add (track.videoKind != iTunesEVdKNone)). Even the PDF's are iTunesFileTracks. But the .h states:

// a track representing an audio file (MP3, AIFF, etc.)
@interface iTunesFileTrack : iTunesTrack


I use something like this in my code so it should work (app is the iTunes SBApplication):

1.) First get the library source

- (ITunesSource *)librarySource {
  NSArray *sources = [[app sources] get];       
  NSArray *libs = [sources filteredArrayUsingPredicate:[NSPredicate
    predicateWithFormat:@"kind == %i",
    ITunesESrcLibrary]];
  if ([libs count]) {
    return [libs objectAtIndex:0];
  }
  return nil;
}

2.) Iterate through the library playlist(s)

NSArray *libraryLists = [[[self librarySource] libraryPlaylists] get];
for (ITunesLibraryPlaylist *list in libraryLists) {
  NSArray *listTracks = [[list fileTracks] get];
  for (ITunesTrack *listTrack in listTracks) {
    // do stuff...
  }
  [listTracks release];
}

3.) You can check for other track types like so

if (track.videoKind != ITunesEVdKNone || track.podcast) {
  // track is not of type music
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号