如何使用所有id3标签从音乐库导入歌曲

时间:2016-01-18 06:24:30

标签: ios objective-c

我正在使用MPMediaPickerController从音乐库中获取/导入歌曲,效果很好。但是它没有提供歌曲的完整信息,如专辑艺术,艺术家信息,专辑信息等。任何人都可以请求帮助如何导入完整信息的歌曲。以下是我的代码。 self.fullPathNSString,其中包含Document Directory的路径。

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)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];

}

0 个答案:

没有答案