1

我是初学者 iOS 应用程序开发人员。我尝试了以下链接代码。它播放得很好,但我需要选择音频文件路径以及如何以选定的音频上传到服务器,请帮忙。音频代码参考网址

    -(IBAction)clickFileuploadbtn:(id)sender;
{
    NSLog(@"2");
    picker1 =
    [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];

    picker1.delegate      = self;
    picker1.allowsPickingMultipleItems = YES;
    picker1.prompt      = NSLocalizedString (@"Add songs to play", "Prompt in media item picker");

    // The media item picker uses the default UI style, so it needs a default-style
    //  status bar to match it visually
    [[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleDefault animated: YES];

    [self presentModalViewController: picker1 animated: YES];
    [picker1 release];
    // [Uploads FileUpload];
}
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{


    [self dismissModalViewControllerAnimated: YES];

    NSLog(@"%@ %d",mediaItemCollection,mediaItemCollection.count);

    NSArray *newMediaItem= [mediaItemCollection items];


    MPMediaItem *item=[[newMediaItem objectAtIndex:0] retain];

    [self uploadMusicFile:item];
}
- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker
{
    [self dismissModalViewControllerAnimated: YES];

    NSLog(@"cancel");

}


- (void) uploadMusicFile:(MPMediaItem *)song
{
    NSURL *url = [song valueForProperty: MPMediaItemPropertyAssetURL];

    // Given some file path URL: NSURL *pathURL
    // Note: [pathURL isFileURL] must return YES
    NSString *path = [NSString stringWithFormat:@"%@",url];
    NSLog(@"path %@",path);
    NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
    NSLog(@"data %@" ,data);
    NSString *audioName = @"myAudio.caf";
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://192.168.1.176:1001/api?type=iphoneupload"]];
    [request addData:data withFileName:audioName andContentType:@"audio/caf" forKey:@"audioFile"];
    [request setDelegate:self];
    [request setTimeOutSeconds:500];
    [request setDidFinishSelector:@selector(uploadRequestFinished:)];
    [request setDidFailSelector:@selector(uploadRequestFailed:)];
    [request startAsynchronous];



}

- (void)uploadRequestFinished:(ASIHTTPRequest *)request
{
    UIApplication* app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = NO;
    NSData *webData = [[NSData alloc] initWithData:[request responseData]];
    NSString *strEr =  [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
    NSLog(@"strEr %@",strEr);
}

- (void) uploadRequestFailed:(ASIHTTPRequest *)request
{
    NSLog(@"responseStatusCode %i",[request responseStatusCode]);
    NSLog(@"responseString %@",[request responseString]);
    NSError *error = [request error];
    NSLog(@"error %@",error);
    UIApplication* app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = NO;
}
4

0 回答 0