How to get all tracks from an album using iTunes.h/Scripting Bridge

人走茶凉 提交于 2019-12-24 13:26:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!