将MPMediaItemCollection转换为Nsdata

时间:2013-02-12 10:51:46

标签: ios mpmusicplayercontroller mpmediapickercontroller

我通过以下代码从MPMediaPickerController导入了一个.mp3文件: -

MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeAny];

    mediaPicker.delegate = self;
    mediaPicker.allowsPickingMultipleItems = YES;
    mediaPicker.prompt = @"Select songs to play";

    [self presentModalViewController:mediaPicker animated:YES];
    [mediaPicker release];

现在我想将选定的.mp3文件转换为NSDATA,以便我可以将该nsdata上传到服务器中: -

- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection
{

    MPMusicPlayerController *musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
    [musicPlayer setQueueWithItemCollection: mediaItemCollection];

    NSLog(@"==>%@",musicPlayer);
    [musicPlayer play];
    [self dismissModalViewControllerAnimated: YES];
}

我如何将音乐文件转换为NSDATA或任何其他格式的主要问题,我可以将其上传到服务器。然后可以轻松地使用下载相同的文件作为音频文件

1 个答案:

答案 0 :(得分:0)

您可以使用它来转换.mp3或任何媒体文件以转换为nsdata: -

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSURL *soundFileURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/memo.m4a", documentsDirectory]];;
NSData *myData = [NSData dataWithContentsOfURL:soundFileURL];

return myData;