2

我通过以下代码从 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 或任何其他格式,通过它我可以将其上传到服务器。然后可以在下载与音频文件相同的文件后轻松使用它

4

1 回答 1

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;
于 2014-01-07T05:48:03.810 回答