-1

我试图将音频文件上传到服务器。我在列表音频中使用了 MPMedia Picker 视图,当我要选择要上传到服务器(http)的音频时,我该怎么做。任何人都可以提供任何示例教程或示例代码。

我试过以下代码。

- (IBAction)openMediaPicker:(id)sender {
    MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
    mediaPicker.delegate = self;
    mediaPicker.allowsPickingMultipleItems = YES; // this is the default   
    [self presentModalViewController:mediaPicker animated:YES];
    [mediaPicker release];
}

- (IBAction)volumeSliderChanged:(id)sender {
    self.musicPlayer.volume = self.volumeSlider.value;
}

#pragma mark MPMediaPickerController delegate methods

- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
    // We need to dismiss the picker
    [self dismissModalViewControllerAnimated:YES];

    // Assign the selected item(s) to the music player and start playback.
    [self.musicPlayer stop];
    [self.musicPlayer setQueueWithItemCollection:mediaItemCollection];
    [self.musicPlayer play];
}

- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
    // User did not select anything
    // We need to dismiss the picker
    [self dismissModalViewControllerAnimated:YES];
}
4

1 回答 1

0

从 mediaItemCollection.items 数组的某个索引初始化 MPMediaItem 对象:

MPMediaItem *anItem = (MPMediaItem *)[mediaItemCollection.items objectAtIndex: row];

然后调用 -valueForProperty: 方法:

NSURL *assetURL = [anItem valueForProperty: MPMediaItemPropertyAssetURL];

您可以从此处描述的 MPMediaItem 获得其他属性:http: //developer.apple.com/library/ios/#DOCUMENTATION/MediaPlayer/Reference/MPMediaItem_ClassReference/Reference/Reference.html

在“通用媒体项目属性键”下查看

希望这可以帮助!塔姆斯

使用 ASIHTTPRequest 上传如下音频文件,

NSURL *audiourl = [NSURL URLWithString:@"Your Url"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:audiourl];
NSData *postData = [NSData dataWithContentsOfURL:SoundPath];
//SoundPath is your audio url path of NSDocumentDirectory.
[request addData:postData withFileName:@"myAudio.caf" andContentType:@"audio/caf" forKey:@"company_audio"];
[request setDelegate:self];
[request startSynchronous];

这样您就可以轻松地在服务器上上传音频文件。

于 2013-03-01T12:28:47.807 回答