我正在开发一个iOS应用程序。我必须以编程方式将音频文件发送到我们的服务器。我使用以下代码将示例wav音频文件发送到服务器。我们的服务器只接受带符号字节数组格式的音频文件以便接收。
NSURL *urlPath = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"Crash" ofType:@"wav"]];
NSString *wavbundlepath = [urlPath absoluteString];
NSLog(@"wavbundlepath: %@",wavbundlepath);
NSData *bytes = [wavbundlepath dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"bytes: %@",bytes);
NSString *recordPostLength = [NSString stringWithFormat:@"%d", [bytes length]];
NSMutableString *urlstr = [NSMutableString stringWithFormat:@"%@", @"http://www.mywebserver/api/UploadFile?Name="];
[urlstr appendString:@"crash"];
[urlstr appendFormat:@"&MemberID=%d", 0];
[urlstr appendFormat:@"&Type=%@",@"Recording"];
[urlstr appendFormat:@"&client=%@",@"ios"];
NSLog(@"urlstr.......%@",urlstr);
NSMutableURLRequest *recordRequest = [[NSMutableURLRequest alloc] init] ;
[recordRequest setURL:[NSURL URLWithString:urlstr]];
[recordRequest setHTTPMethod:@"POST"];
[recordRequest setValue:recordPostLength forHTTPHeaderField:@"Content-Length"];
[recordRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[recordRequest setHTTPBody:bytes];
NSURLResponse *recordResponse;
NSError *recordError;
NSData *recordResponseData = [NSURLConnection sendSynchronousRequest:recordRequest returningResponse:&recordResponse error:&recordError];
NSString *recordResp = [[NSString alloc]initWithData:recordResponseData encoding:NSUTF8StringEncoding];
NSLog(@"recordResp:%@", recordResp);
但问题是,总是收到“输入字符串不正确”。回应错误。
我不太清楚将音频字节上传到服务器。有人可以检查一下这段代码并告诉我这是否是有效的代码,用于将wav音频字节数组上传到服务器?
谢谢。
更新代码
我尝试下面的代码,因为我的服务器端工程师说没有任何其他东西贴身,它工作正常并得到积极的回应。但是,服务器无法使用我正在发送的格式NSData字节(32位元素),因为服务器端实现的只接收字节数组或带符号字节数据格式。
NSURL *urlPath = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"Temp" ofType:@"wav"]];
NSString *wavbundlepath = [urlPath absoluteString];
NSLog(@"wavbundlepath: %@",wavbundlepath);
NSData *bytes=[NSData dataWithContentsOfURL:urlPath];
NSLog(@"bytes: %@",bytes);
NSString *recordPostLength = [NSString stringWithFormat:@"%d", [bytes length]];
NSMutableString *urlstr = [NSMutableString stringWithFormat:@"%@", @"http://www.myserver.com/api/UploadFile?Name="];
[urlstr appendString:@"Temp"];
[urlstr appendFormat:@"&MemberID=%d", 0];
[urlstr appendFormat:@"&Type=%@",@"Recording"];
[urlstr appendFormat:@"&client=%@",@"ios"];
NSLog(@"urlstr.......%@",urlstr);
NSMutableURLRequest *recordRequest = [[NSMutableURLRequest alloc] init] ;
[recordRequest setURL:[NSURL URLWithString:urlstr]];
NSInputStream *dataStream = [NSInputStream inputStreamWithData:bytes];
[recordRequest setHTTPBodyStream:dataStream];
[recordRequest setHTTPMethod:@"POST"];
NSURLResponse *recordResponse;
NSError *recordError;
NSData *recordResponseData = [NSURLConnection sendSynchronousRequest:recordRequest returningResponse:&recordResponse error:&recordError];
NSString *recordResp = [[NSString alloc]initWithData:recordResponseData encoding:NSUTF8StringEncoding];
NSLog(@"recordResp:%@", recordResp);
recordResponceJson = [recordResp JSONValue];
NSLog(@"recordResponceJson = %@",recordResponceJson);
recId = [recordResponceJson valueForKey:@"ID"];
NSLog(@"recId....%@", recId);
有人可以指导我,我如何在这个http帖子中发送字节数组?
答案 0 :(得分:0)
有几个问题: 首先,您如何读取文件将不会返回有效的WAV文件:
您应该使用[NSData dataWithContentsOfURL:]来读取字节。你正在阅读它们的方式,它会尝试将字节转换为字符。这会破坏你的wav文件。
其次,POST正文的内容类型可能不正确。见行:
[recordRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
音频字节不会以x-www-form-urlencoded格式编码。服务器更可能接受音频/ wav或应用程序/八位字节流内容类型。请咨询您的服务器开发人员,以确切了解服务器的期望。
此外,您可能想要查看名为setHTTPBodyStream的NSMutableURLRequest方法:它允许您发送文件而无需将整个内容加载到内存中。