2012-08-08 39 views
0

我想从我的iPhone应用程序上传一个音频文件到网络服务器,但是当我将它转换成NSData的时候,它将花费30到40分钟的时间进行转换,所以你能为它提供更好的解决方案吗?如何在iPhone上的网络服务器上传一个mp3文件达到5 MB大小

+0

那是什么,你要上传的音频文件的大小?我约谈话。 22MB的音频文件到NSData,并没有花费这么大的时间进行转换。请发布您的转换代码。 – 2012-08-08 10:15:28

+0

NSString * file1 = [[NSBundle mainBundle] pathForResource:@“background”ofType:@“mp3”]; NSData * file1Data = [[NSData alloc] initWithContentsOfFile:file1]; background.mp3文件的大小仅为4.2 mb,转换时间大约需要30到40分钟。 – SKMAISM 2012-08-08 10:26:03

回答

2

1)。为了音频文件转换为NSData的:

NSString *audioFilePath = @""; // local path where audio stored 
NSData *audioData = [[NSData alloc] initWithContentsOfFile:audioFilePath]; 

2)。为了上传音频文件:

NSString *audioName = @"myAudio.caf"; 
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://someurl.aspx"]];  
[request addData:audioData 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]; 
} 

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

感谢您的支持。但我正在使用xcode 4.2和IOS 5,并且我正面临着大量文件丢失或框架问题。你可以附上源代码zip文件吗? – SKMAISM 2012-08-08 11:10:41

+0

上面的代码与Xcode 4.2和IOS 5完美配合。你可能没有ASIHTTP帮助文件,你可以从https://github.com/pokeb/asi-http-request/tree下载它。 – 2012-08-08 11:18:56

+0

感谢您的回复和支持。但是,当我通过您的代码将音频文件转换为NSData时,它再次需要很长时间才能转换。除了nsdata之外,还有其他的备用服务器可以快速发送iur音频文件到服务器上。 – SKMAISM 2012-08-08 12:30:10

相关问题