问题
I'd like to get all tracks from an album on iTunes using iTunes.h
.
Right now I get the data of the current track by:
NSInteger trackID = iTunes.currentTrack.databaseID;
NSString *name = iTunes.currentTrack.name;
And the name of the album by:
NSString *trackAlbum = iTunes.currentTrack.album;
But know I don't know how to get all the tracks that are in the same album as the current track.
Any ideas? Thanks
回答1:
The iTunes API is poorly written. You have to filter the array with a predicate.
NSArray *allSongs = [self allSongs];
NSArray *songsOfAlbum = [allSongs filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"album == %@ && artist == %@", albumString, artistString]];
You can get all the songs like this:
// Get all Songs
- (NSArray *)allSongs {
if (_allSongs == nil) {
NSArray *tracksToPlay = [(SBElementArray *)[self.library tracks] get];
// Sort by artist
_allSongs = tracksToPlay;
}
return _allSongs;
}
- (iTunesLibraryPlaylist *)library {
if (_library == nil) {
// Whole Library
iTunesSource *source = [[[[self.iTunes sources] get] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"kind == %i", iTunesESrcLibrary]] objectAtIndex:0];
// Only the Music
_library = [[[[source playlists] get] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"specialKind == %i", iTunesESpKMusic]] objectAtIndex:0];
}
return _library;
}
回答2:
You can use iTunes Library Framework (10.9 osx) for iTunes 11.
#import <iTunesLibrary/ITLibrary.h>
#import <iTunesLibrary/ITLibMediaItem.h>
#import <iTunesLibrary/ITLibAlbum.h>
NSError *error = nil;
ITLibrary *library = [ITLibrary libraryWithAPIVersion:@"1.0" error:&error];
if (library)
{
NSArray *tracks = library.allMediaItems; // <- NSArray of ITLibMediaItem
}
for (ITLibMediaItem *item in tracks) {
NSLog(@"tracks %@",item.album.title);
}
You can fetch album information from ITLibMediaItem
回答3:
As an example to retrieve the artworks I have submitted a github repository: It is an objective c code which uses the above code changed:
#import <Foundation/Foundation.h>
#import <iTunesLibrary/ITLibrary.h>
#import <iTunesLibrary/ITLibMediaItem.h>
#import <iTunesLibrary/ITLibArtwork.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSError *error = nil;
ITLibrary *library = [ITLibrary libraryWithAPIVersion:@"1.0" error:&error];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (library)
{
//NSArray *playlists = library.allPlaylists; // <- NSArray of ITLibPlaylist
NSArray *tracks = library.allMediaItems; // <- NSArray of ITLibMediaItem
[tracks enumerateObjectsUsingBlock:^(ITLibMediaItem* _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.artworkAvailable) {
ITLibArtwork* artWork=obj.artwork;
//NSData* d=artWork.imageData;
switch (artWork.imageDataFormat) {
case ITLibArtworkFormatPNG:{
NSURL* fileURL=obj.location;
NSString* fs=fileURL.lastPathComponent;
NSString* path=[fileURL.path stringByReplacingOccurrencesOfString:fs withString:@"Folder.png"];
if (![fileManager fileExistsAtPath:path]){
[artWork.imageData writeToFile:path atomically:YES];
}
}
break;
case ITLibArtworkFormatJPEG:{
NSURL* fileURL=obj.location;
NSString* fs=fileURL.lastPathComponent;
NSString* path=[fileURL.path stringByReplacingOccurrencesOfString:fs withString:@"Folder.jpg"];
if (![fileManager fileExistsAtPath:path]){
[artWork.imageData writeToFile:path atomically:YES];
}
}
break;
default:
break;
}
}
}];
NSLog(@"End reached");
}
}
return 0;
}
https://github.com/ENees/iTunesGetImages
来源:https://stackoverflow.com/questions/14076113/how-to-get-all-tracks-from-an-album-using-itunes-h-scripting-bridge