获取MP3文件信息的代码

两个方法都是获取mp3文件信息的,也可以获取m4a文件的信息,自己留着备用,乱码问题还未解决


-(void)getMp3Information
{
    NSURL *fileUrl = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/RudyExpandedScore.mp3",musicDir]];
    AudioFileTypeID fileTypeHint = kAudioFileMP3Type;
    NSString *fileExtension = [[fileUrl path] pathExtension];
    if ([fileExtension isEqualToString:@"mp3"]) {
        AudioFileID fileID = nil;
        OSStatus error = noErr;
        
        error = AudioFileOpenURL((__bridge CFURLRef)fileUrl, kAudioFileReadPermission, 0, &fileID);
        if (error != noErr) {
            DeLog(@"open error");
        }
        
        UInt32 id3DataSize = 0;
        error = AudioFileGetPropertyInfo(fileID, kAudioFilePropertyID3Tag, &id3DataSize, NULL);
        if (error != noErr) {
            DeLog(@"AudioFileGetPropertyInfo Failed!");
        }
        
        NSDictionary *piDict = nil;
        UInt32 piDataSize = sizeof(piDict);
        error = AudioFileGetProperty(fileID, kAudioFilePropertyInfoDictionary, &piDataSize, &piDict);
        if (error != noErr) {
            DeLog(@"AudioFileGetProperty Failed!");
        }
        
        CFDataRef albumPic = nil;
        UInt32 picDataSize = sizeof(albumPic);
        error = AudioFileGetProperty(fileID, kAudioFilePropertyAlbumArtwork, &picDataSize, &albumPic);
        if (error != noErr) {
            DeLog(@"AudioFileGetProperty failed!(album)");
        }
        
        NSData *imageData = (__bridge NSData *)albumPic;
        UIImage *img = [UIImage imageWithData:imageData];
        lrcImageView.image = img;
        
        NSString *album = [(NSDictionary *)piDict objectForKey:[NSString stringWithUTF8String:kAFInfoDictionary_Album]];
        NSString *artlist = [(NSDictionary *)piDict objectForKey:[NSString stringWithUTF8String:kAFInfoDictionary_Artist]];
        NSString *title = [(NSDictionary *)piDict objectForKey:@kAFInfoDictionary_Title];
        
        DeLog(@"album: %@\nartList: %@,Title: %@",album,artlist,title);
        DeLog(@"mp3Dic: %@",piDict);
    }
}

-(void)getMp3InfoWithAVURLAsset
{
    NSURL *fileUrl = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/RudyExpandedScore.mp3",musicDir]];
    AVURLAsset *mp3Asset = [AVURLAsset URLAssetWithURL:fileUrl options:nil];
    for (NSString *format in [mp3Asset availableMetadataFormats]) {
        DeLog(@"formatString: %@",format);
        for (AVMetadataItem *metadataitem in [mp3Asset metadataForFormat:format]) {
            DeLog(@"commonKey = %@",metadataitem.commonKey);
            if ([metadataitem.commonKey isEqualToString:@"artwork"]) {
                NSData *data = [(NSDictionary *)metadataitem.value objectForKey:@"data"];
                NSString *mime = [(NSDictionary *)metadataitem.value objectForKey:@"MIME"];
                lrcImageView.image = [UIImage imageWithData:data];
                DeLog(@"mime: %@",mime);
                break;
            }
            else if([metadataitem.commonKey isEqualToString:@"title"])
            {
                NSString *title = (NSString *)metadataitem.value;
                DeLog(@"title: %@",title);
            }
            else if([metadataitem.commonKey isEqualToString:@"artist"])
            {
                NSString *artist = (NSString *)metadataitem.value;
                DeLog(@"artist: %@",artist);
            }
            else if([metadataitem.commonKey isEqualToString:@"albumName"])
            {
                NSString *albumName = (NSString *)metadataitem.value;
                
                DeLog(@"albumName: %@",albumName);
            }
        }
    }
    
    CMTime durationTime = mp3Asset.duration;
    CGFloat duration = CMTimeGetSeconds(durationTime);
    DeLog(@"总时间:%f",duration);
    
}

你可能感兴趣的:(iOS,snippet)