我有一个录制语音命令的AVAudioRecorder,然后使用以下代码将其转换为nsdata -
NSData * data = [NSData dataWithContentsOfURL:recorder.url];
现在我使用以下代码执行语音识别请求 -
NSDictionary * headers = @ {@"授权":[NSString stringWithFormat:@" Bearer%@",authCode], @"转移编码":@" chunked", @" Content-Type":@" multipart / form-data;边界= someboundary" };
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"\r\n--someboundary\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Disposition: form-data; name=\"request\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Type: application/json; charset=UTF-8\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"{\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"messageHeader\": {\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"deviceContext\": [\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"{\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"name\": \"playbackState\",\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"namespace\": \"AudioPlayer\",\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"payload\": {\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"streamId\": \"\",\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; // what is the streamid supposed to be?
[postData appendData:[@"\"offsetInMilliseconds\": \"\",\r\n" dataUsingEncoding:NSUTF8StringEncoding]];//Is this correct?
[postData appendData:[@"\"playerActivity\": \"IDLE\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"}\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"}\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"]\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"},\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"messageBody\": {\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"profile\": \"alexa-close-talk\",\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"locale\": \"en-us\",\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"format\": \"audio/L16; rate=16000; channels=1\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"}\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"}\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"--someboundary\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Disposition: form-data; name=\"audio\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Type: audio/L16; rate=16000; channels=1\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[NSData dataWithData:audioData]];
[postData appendData:[[NSString stringWithFormat:@"\r\n--someboundry--"] dataUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://access-alexa-na.amazon.com/v1/avs/speechrecognizer/recognize"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:20.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSLog(@"SpeechRecognition %@", httpResponse);
}
}];
[dataTask resume];
但是上面的代码给了我一个500错误。非常感谢任何帮助
答案 0 :(得分:0)
我弄清楚出了什么问题。音频数据之前的内容类型应为
[postData appendData:[@"Content-Type: audio/L16; rate=16000; channels=1\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
不是
[postData appendData:[@"Content-Type: audio/L16; rate=16000; channels=1\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
它缺少一个额外的“\ r \ n”