我正试图从用户添加到应用程序中的mp3文件中获取专辑图片。这是我正在使用的代码。
NSURL * fileURL=[NSURL fileURLWithPath:filePath];
NSString *fileExtension = [[fileURL path] pathExtension];
if ([fileExtension isEqual:@"mp3"]||[fileExtension isEqual:@"m4a"])
{
AudioFileID fileID = nil;
OSStatus err = noErr;
err = AudioFileOpenURL( (__bridge CFURLRef) fileURL, kAudioFileReadPermission, 0, &fileID );
if( err != noErr ) {
NSLog( @"AudioFileOpenURL failed" );
}
UInt32 id3DataSize = 0;
err = AudioFileGetPropertyInfo( fileID, kAudioFilePropertyID3Tag, &id3DataSize, NULL );
if( err != noErr ) {
NSLog( @"AudioFileGetPropertyInfo failed for ID3 tag" );
}
NSDictionary *piDict = nil;
UInt32 piDataSize = sizeof( piDict );
err = AudioFileGetProperty( fileID, kAudioFilePropertyInfoDictionary, &piDataSize, &piDict );
if( err != noErr ) {
NSLog( @"AudioFileGetProperty failed for property info dictionary" );
}
CFDataRef AlbumPic= nil;
UInt32 picDataSize = sizeof(picDataSize);
err =AudioFileGetProperty( fileID, kAudioFilePropertyAlbumArtwork, &picDataSize, &AlbumPic);
if( err != noErr ) {
NSLog( @"Get picture failed" );
}
NSData* imagedata= (__bridge NSData*)AlbumPic;
UIImage* image=[[UIImage alloc]initWithData:imagedata];
NSString * Album = [(NSDictionary*)piDict objectForKey:
[NSString stringWithUTF8String: kAFInfoDictionary_Album]];
NSString * Artist = [(NSDictionary*)piDict objectForKey:
[NSString stringWithUTF8String: kAFInfoDictionary_Artist]];
NSString * Title = [(NSDictionary*)piDict objectForKey:
[NSString stringWithUTF8String: kAFInfoDictionary_Title]];
NSLog(@"%@",Title);
NSLog(@"%@",Artist);
NSLog(@"%@",Album);
NSLog(@"Artwork Image%@",image);
以下代码通过mp3
标记从id3
文件中获取信息。返回的所有内容都接受为 Null 的艺术作品图像。我知道有一种方法可以使用kAudioFilePropertyAlbumArtwork
执行此操作,但它永远不会有效。