iphone - 在 iOS 中提取 MP3 专辑插图

标签 iphone ios objective-c avfoundation core-audio

我一直在尝试从 iPhone 应用程序的 .mp3 文件中提取元数据信息。我尝试使用像 this 这样的 AVAsset .它没有用,公共(public)元数据是空的。但是 audacity 和应用商店中的另一个 iOS 应用程序可以检索元数据详细信息。我不知道为什么?

所以,我尝试使用 AudioToolBox 框架从下面的代码中提取相同的内容

    CFDictionaryRef piDict = nil;
    UInt32 piDataSize = sizeof(piDict);

    //  Populates a CFDictionary with the ID3 tag properties
    err = AudioFileGetProperty(fileID, kAudioFilePropertyInfoDictionary, &piDataSize, &piDict);
    if(err != noErr) {
        NSLog(@"AudioFileGetProperty failed for property info dictionary");
        return nil;
    }

    //  Toll free bridge the CFDictionary so that we can interact with it via objc
    NSMutableDictionary* nsDict = [(__bridge NSDictionary*)piDict mutableCopy];

这会返回专辑封面以外的所有内容。当我尝试使用 kAudioFilePropertyAlbumArtwork 提取专辑封面时,出现了 osstatus 错误(操作无法完成)。

所以最后,我尝试使用 libId3 的 ObjC 包装器(找到 here)。它工作得很好。我可以得到艺术品。

我的问题是,为什么 AVAsset 无法检索数据?。我在那里错过了什么?有人设法使它工作吗? sample 将不胜感激。

为什么 kAudioFilePropertyAlbumArtwork 提取无法完成?。这两个问题都发生在我拥有的所有 .mp3 文件上。

解决方案更新:

AVAsset 对我不起作用,因为我使用

 [NSURL URLWithString:[filePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]

而不是

    NSURL *fileURL = [NSURL fileURLWithPath:filePath];

最佳答案

使用AVAsset提取元数据信息,this post很有用。以下代码是您所需要的:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"mp3"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
AVAsset *asset = [AVURLAsset URLAssetWithURL:fileURL options:nil];

NSArray *keys = [NSArray arrayWithObjects:@"commonMetadata", nil];
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^{
    NSArray *artworks = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata
                                                       withKey:AVMetadataCommonKeyArtwork
                                                      keySpace:AVMetadataKeySpaceCommon];

    for (AVMetadataItem *item in artworks) {
        if ([item.keySpace isEqualToString:AVMetadataKeySpaceID3]) {
            NSDictionary *dict = [item.value copyWithZone:nil];
            self.imageView.image = [UIImage imageWithData:[dict objectForKey:@"data"]];
        } else if ([item.keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
            self.imageView.image = [UIImage imageWithData:[item.value copyWithZone:nil]];
        }
    }
}];

关于iphone - 在 iOS 中提取 MP3 专辑插图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16318821/

相关文章:

iPhone : Print all contents of a Scroll View

iphone - 在 GooGle map 上添加 UITextField 和 UiButton

ios - 如何淡出 iOS 12 上的 titleView 标签

iOS Sprite 套件 : How to make Sprite follow Touches

ios - 如何在播放 m3u8 视频流之前获取帧图像? iOS

iphone - BASS_MIDI_StreamEvent在零位置

ios - 在构建应用程序之前从 Info.plist 文件中删除一个简单的 key

ios - 在 SceneKit 中更改 "field of view"

iphone - ASIHTTPRequest调试

ios - 使用NSConditionLock卡住iOS应用