gpt4 book ai didi

iphone - 如何获取mp3文件的专辑封面

转载 作者:行者123 更新时间:2023-12-03 20:43:05 35 4
gpt4 key购买 nike

我正在开发一个音频播放器。我成功获取了mp3文件的其他信息。但无法获取 mp3 文件的专辑封面。使用此代码我获取 mp3 文件信息。

- (NSDictionary *)songID3Tags
{
AudioFileID fileID = nil;
OSStatus error = noErr;
error = AudioFileOpenURL((CFURLRef)self.filePath, kAudioFileReadPermission, 0, &fileID);
if (error != noErr) {
NSLog(@"AudioFileOpenURL failed");
}
UInt32 id3DataSize = 0;
char *rawID3Tag = NULL;

error = AudioFileGetPropertyInfo(fileID, kAudioFilePropertyID3Tag, &id3DataSize, NULL);
if (error != noErr)
NSLog(@"AudioFileGetPropertyInfo failed for ID3 tag");

rawID3Tag = (char *)malloc(id3DataSize);
if (rawID3Tag == NULL)
NSLog(@"could not allocate %lu bytes of memory for ID3 tag", id3DataSize);

error = AudioFileGetProperty(fileID, kAudioFilePropertyID3Tag, &id3DataSize, rawID3Tag);
if( error != noErr )
NSLog(@"AudioFileGetPropertyID3Tag failed");

UInt32 id3TagSize = 0;
UInt32 id3TagSizeLength = 0;

error = AudioFormatGetProperty(kAudioFormatProperty_ID3TagSize, id3DataSize, rawID3Tag, &id3TagSizeLength, &id3TagSize);

if (error != noErr) {
NSLog( @"AudioFormatGetProperty_ID3TagSize failed" );
switch(error) {
case kAudioFormatUnspecifiedError:
NSLog( @"Error: audio format unspecified error" );
break;
case kAudioFormatUnsupportedPropertyError:
NSLog( @"Error: audio format unsupported property error" );
break;
case kAudioFormatBadPropertySizeError:
NSLog( @"Error: audio format bad property size error" );
break;
case kAudioFormatBadSpecifierSizeError:
NSLog( @"Error: audio format bad specifier size error" );
break;
case kAudioFormatUnsupportedDataFormatError:
NSLog( @"Error: audio format unsupported data format error" );
break;
case kAudioFormatUnknownFormatError:
NSLog( @"Error: audio format unknown format error" );
break;
default:
NSLog( @"Error: unknown audio format error" );
break;
}
}

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

error = AudioFileGetProperty(fileID, kAudioFilePropertyInfoDictionary, &piDataSize, &piDict);
if (error != noErr)
NSLog(@"AudioFileGetProperty failed for property info dictionary");

free(rawID3Tag);

return (NSDictionary*)piDict;
}

我知道通过 kAudioFilePropertyAlbumArtwork 我可以获取 mp3 文件的专辑封面,但我不知道如何获取它。

最佳答案

    - (NSArray *)artworksForFileAtPath:(NSString *)path {
NSMutableArray *artworkImages = [NSMutableArray array];
NSURL *u = [NSURL fileURLWithPath:path];
AVURLAsset *a = [AVURLAsset URLAssetWithURL:u options:nil];
NSArray *artworks = [AVMetadataItem metadataItemsFromArray:a.commonMetadata withKey:AVMetadataCommonKeyArtwork keySpace:AVMetadataKeySpaceCommon];

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];
}
NSLog(@"array description is %@", [artworkImages description]);
return artworkImages; }

上面的代码返回mp3文件的album_art。其中path是音频文件路径。

关于iphone - 如何获取mp3文件的专辑封面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10040643/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com