2013-02-12 59 views
2

我已經通過下面的代碼導入.mp3文件形式MPMediaPickerController: -轉換MPMediaItemCollection到NSData的

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

在我如何musicfile轉換爲NSData的或進入,通過它我可以把它上傳到server.Can則可以方便地使用在下載同一個文件中的音頻文件後,任何其它格式的主要問題

+0

您好..你能回答自己的問題。 – Rajneesh071 2013-04-24 14:49:42

回答

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;