1

我想使用 iTunes 从专辑中获取所有曲目iTunes.h
现在我通过以下方式获取当前曲目的数据:

NSInteger trackID = iTunes.currentTrack.databaseID;
NSString *name  = iTunes.currentTrack.name;

以及专辑名称:

NSString *trackAlbum = iTunes.currentTrack.album;

但是知道我不知道如何获取与当前曲目在同一专辑中的所有曲目。
有任何想法吗?谢谢

4

3 回答 3

1

iTunes API 写得不好。您必须使用谓词过滤数组。

NSArray *allSongs = [self allSongs];
NSArray *songsOfAlbum = [allSongs filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"album == %@ && artist == %@", albumString, artistString]];

你可以得到所有这样的歌曲:

// 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;
}
于 2012-12-28T21:54:26.413 回答
1

您可以将iTunes Library Framework (10.9 osx) 用于 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);
}

您可以从ITLibMediaItem获取专辑信息

于 2013-10-31T10:35:02.050 回答
-1

作为检索艺术品的示例,我提交了一个 github 存储库:这是一个客观的 c 代码,使用上述代码更改:

    #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

于 2016-02-18T14:32:39.017 回答