Ios 如何从音乐库导入带有所有id3标签的歌曲

Ios 如何从音乐库导入带有所有id3标签的歌曲,ios,objective-c,Ios,Objective C,我正在使用MPMediaPickerController从音乐库中获取/导入歌曲,效果很好。然而,它并没有提供完整的信息,如专辑艺术,艺术家信息,专辑信息等歌曲。任何人都可以请帮助如何导入歌曲与完整的信息。下面是我的代码self.fullPath是一个NSString,包含指向文档目录的路径 typedef enum { kEDSupportedMediaTypeAAC = 'aac', kEDSupportedMediaTypeMP3 = '.mp3', kEDSupportedMediaTy

我正在使用
MPMediaPickerController
从音乐库中获取/导入歌曲,效果很好。然而,它并没有提供完整的信息,如专辑艺术,艺术家信息,专辑信息等歌曲。任何人都可以请帮助如何导入歌曲与完整的信息。下面是我的代码
self.fullPath
是一个
NSString
,包含指向
文档目录的路径

typedef enum {
kEDSupportedMediaTypeAAC = 'aac',
kEDSupportedMediaTypeMP3 = '.mp3',
kEDSupportedMediaTypeWAV = '.wav',
kEDSupportedMediaTypeAIFF = 'aiff'
} EDSupportedMediaType;

-(void)importIPodMusic {
MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];  

mediaPicker.delegate = self;
mediaPicker.allowsPickingMultipleItems = YES; // this is the default
mediaPicker.prompt = NSLocalizedString(@"Choose your song:", nil);
mediaPicker.navigationController.toolbar.barStyle = UIBarStyleBlackOpaque;

[self presentViewController:mediaPicker animated:YES completion:nil];
}

}

-(void)importSongsForArray:(NSArray*)数组{
@自动释放池{
浮动进度=0.0f;
浮动指数=0.0f;
用于(MPMediaItem*数组中的项){
NSURL*url=[item valueForProperty:MPMediaItemPropertyAsetUrl];
NSString*title=[item valueForProperty:MPMediaItemPropertyTitle];
AVURLAsset*asset=[[AVURLAsset alloc]initWithURL:url选项:nil];
Avassetrader*reader=[[Avassetrader alloc]initWithAsset:asset错误:nil];
NSMutableArray*myoutput=[[NSMutableArray alloc]init];
NSString*fileURL=@;
对于(AvassettTrack*在[资产跟踪]中跟踪)
{
NSMutableDictionary*audioReadSettings=[NSMutableDictionary];
[audioReadSettings设置值:[NSNumber numberWithInt:kAudioFormatLinearPCM]
forKey:AVFormatIDKey];
AVAssetReaderTrackOutput*output=[AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:track outputSettings:nil];
[MyOutput addObject:output];
[读卡器添加输出:输出];
NSArray*格式=track.formatDescriptions;
对于(int i=0;i
-(void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
[mediaPicker dismissViewControllerAnimated:YES completion:nil];

[self importSongsForArray:mediaItemCollection.items];
-(void)importSongsForArray: (NSArray*)array {   
@autoreleasepool {
    float progress = 0.0f;
    float index = 0.0f;

    for (MPMediaItem *item in array) {
        NSURL *url = [item valueForProperty: MPMediaItemPropertyAssetURL];
        NSString *title = [item valueForProperty:MPMediaItemPropertyTitle];
        AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
        AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:asset error:nil];
        NSMutableArray *myOutputs =[[NSMutableArray alloc] init];
        NSString *fileURL = @"";

        for(AVAssetTrack *track in [asset tracks])
        {
            NSMutableDictionary* audioReadSettings = [NSMutableDictionary dictionary];
            [audioReadSettings setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM]
                                 forKey:AVFormatIDKey];

            AVAssetReaderTrackOutput *output=[AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:track outputSettings:nil];
            [myOutputs addObject:output];
            [reader addOutput:output];

            NSArray *formats = track.formatDescriptions;
            for (int i=0; i<[formats count]; i++) {
                CMFormatDescriptionRef format = (__bridge CMFormatDescriptionRef)[formats objectAtIndex:i];

                // Check the format types
                FourCharCode mediaSubType = CMFormatDescriptionGetMediaSubType(format);
                CMMediaType mediaType = CMFormatDescriptionGetMediaType(format);

                NSLog(@"mediaType: %u, mediaSubType: %u", (unsigned int)mediaType, (unsigned int)mediaSubType);

                if (mediaSubType == kEDSupportedMediaTypeMP3) {
                    fileURL = [self.fullPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp3", title]];
                    break;
                }                    
            }
        }
        [reader startReading];
        NSFileHandle *fileHandle ;
        NSFileManager *fm=[NSFileManager defaultManager];
        if(![fm fileExistsAtPath:fileURL])
        {
            NSData *data = [[NSData alloc] init];
            [fm createFileAtPath:fileURL contents:data attributes:nil];
        }else{
            NSData *data = [[NSData alloc] init];
            NSURL *url = [NSURL URLWithString:fileURL];
            [fm removeItemAtURL:url error:nil];
            [fm createFileAtPath:fileURL contents:data attributes:nil];

        }
        fileHandle=[NSFileHandle fileHandleForWritingAtPath:fileURL];
        [fileHandle seekToEndOfFile];

        AVAssetReaderOutput *output=[myOutputs objectAtIndex:0];
        int totalBuff=0;
        int test = 1;
        while(test == 1)
        {
            CMSampleBufferRef ref=[output copyNextSampleBuffer];

            if(ref==NULL)
                test = 0;

            if(ref==NULL)
                break;
            //copy data to file
            //read next one
            AudioBufferList audioBufferList;
            NSMutableData *data;
            CMBlockBufferRef blockBuffer;
            CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(ref, NULL, &audioBufferList, sizeof(audioBufferList), NULL, NULL, 0, &blockBuffer);

            for( int y=0; y<audioBufferList.mNumberBuffers; y++ )
            {
                AudioBuffer audioBuffer = audioBufferList.mBuffers[y];
                void *frame = audioBuffer.mData;

                data = [[NSMutableData alloc] initWithBytes:frame length:audioBuffer.mDataByteSize];
            }
            totalBuff++;
            NSLog(@"\n%d\n",totalBuff);

            //            int time = [self.mediaPlayer durationOfCurrentItem];

            //            if (totalBuff * 2 <= time + 1)
            [fileHandle writeData:data];

        }
        [fileHandle closeFile];

        progress = ++index/[array count];
        NSLog(@"\n%lf\n",progress);
    }
}

[self reloadData];