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

╄→尐↘猪︶ㄣ 提交于 2019-12-03 14:07:10

问题


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 compile it, but instead it failed with 1640 errors.

How do I make it successfully compile - the reason why I ask is taglib allows for the extraction of album artwork from a tag.

If anyone knows an Objective-C based album artwork extraction class it would help - I don't know why Apple don't add a way of doing this in Core Foundation - because there are methods for extracting some of the data from an ID3 tag.

I can't see why there isn't some Objective-C way of doing it.

Any help appreciated.

EDIT:

After hours upon hours of trying to do this, I've found a Java function here that seems to do the job fine, but I haven't got a clue on how to convert it to Objective-C (or let alone C++ for that matter), as its types seem to be completely different from those that are in Objective-C/C++.

If anyone knows of a way to convert this it would really help, as this seems as my last option as I've tried so many others.


回答1:


- (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);
     }];
}



回答2:


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);
         }
     }];
}



回答3:


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?




回答4:


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



来源:https://stackoverflow.com/questions/6607401/get-album-artwork-from-id3-tag-convert-function-from-java-to-objective-c

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