Get album artwork from ID3 tag/Convert function from Java to Objective-C

后端 未结 4 894
借酒劲吻你
借酒劲吻你 2021-02-06 18:54

I\'ve got the following question to ask:

How do you compile taglib with an iOS application?

I\'m a bit confused as I added the folder into my project, tried to c

相关标签:
4条回答
  • 2021-02-06 19:00

    If you are merely wanting to access the artwork of the music already stored on the device i.e from the iPod application, then check out this article which demonstrates how to do it using existing frameworks provided by Apple, specifically the MediaPlayer framework.

    If not, it may be wise to provide a little more information about what you are trying to achieve and also provide examples of the errors you are getting when trying to compile TagLib.

    EDIT:

    Another solution could be to use an embedded webview and use a JavaScript library such as this one to load, parse and fetch the album artwork?

    0 讨论(0)
  • 2021-02-06 19:03
    - (void)loadArtworksForFileAtPath:(NSString *)path completion:(void (^)(NSArray *))completion
    {
        NSURL *u = [NSURL fileURLWithPath:path];
        AVURLAsset *a = [AVURLAsset URLAssetWithURL:u options:nil];
        NSArray *k = [NSArray arrayWithObjects:@"commonMetadata", nil];
    
        [a loadValuesAsynchronouslyForKeys:k completionHandler: ^{
            NSArray *artworks = [AVMetadataItem metadataItemsFromArray:a.commonMetadata
                 withKey:AVMetadataCommonKeyArtwork keySpace:AVMetadataKeySpaceCommon];
    
             NSMutableArray *artworkImages = [NSMutableArray array];
             for (AVMetadataItem *i in artworks)
             {
                 NSString *keySpace = i.keySpace;
                 UIImage *im = nil;
    
                 if ([keySpace isEqualToString:AVMetadataKeySpaceID3])
                 {
                     NSDictionary *d = [i.value copyWithZone:nil];
                     im = [UIImage imageWithData:[d objectForKey:@"data"]];
                 }
                 else if ([keySpace isEqualToString:AVMetadataKeySpaceiTunes])
                     im = [UIImage imageWithData:[i.value copyWithZone:nil]];
    
                 if (im)
                     [artworkImages addObject:im];
             }
    
             completion(artworkImages);
         }];
    }
    
    0 讨论(0)
  • 2021-02-06 19:14

    This is just the answer of @hatfinch above but fixed and now working.

    - (void)artworksForFileAtPath:(NSString *)path block:(void(^)(NSArray *artworkImages))block
    {
        NSURL *url = [NSURL fileURLWithPath:path];
        AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
        NSArray *keys = [NSArray arrayWithObjects:@"commonMetadata", nil];
        [asset loadValuesAsynchronouslyForKeys:keys completionHandler:^{
             NSArray *artworks = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata
                                                                withKey:AVMetadataCommonKeyArtwork
                                                               keySpace:AVMetadataKeySpaceCommon];
    
             NSMutableArray *artworkImages = [NSMutableArray array];
             for (AVMetadataItem *item in artworks) {
                 NSString *keySpace = item.keySpace;
                 if ([keySpace isEqualToString:AVMetadataKeySpaceID3]) {
                     NSDictionary *d = [item.value copyWithZone:nil];
                     [artworkImages addObject:[UIImage imageWithData:[d objectForKey:@"data"]]];
                 } else if ([keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
                     [artworkImages addObject:[UIImage imageWithData:[item.value copyWithZone:nil]]];
                 }
             }
             if(block) {
                 block(artworkImages);
             }
         }];
    }
    
    0 讨论(0)
  • 2021-02-06 19:14

    You could use this: https://github.com/EliaCereda/TagLib-ObjC

    0 讨论(0)
提交回复
热议问题