获取MP3和M4A音乐文件的歌曲信息以及专辑图片

NSBundle* bundle = [NSBundle mainBundle];
    NSString* path = [bundle bundlePath];
    NSURL * fileURL=[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/akon、be - you - with.mp3", path]];
    AudioFileTypeID fileTypeHint = kAudioFileMP3Type;
    NSString *fileExtension = [[fileURL path] pathExtension];
    if ([fileExtension isEqual:@"mp3"]||[fileExtension isEqual:@"m4a"])
    {
        AudioFileID fileID  = nil;
        OSStatus err        = noErr;
        
        err = AudioFileOpenURL( (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 ) {
            [piDict release];
            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= (NSData*)AlbumPic; 
        UIImage* image=[[UIImage alloc]initWithData:imagedata];
        imageView.image=image;
        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);
        
    }

你可能感兴趣的:(获取MP3和M4A音乐文件的歌曲信息以及专辑图片)