json 从alexa语音服务获得500错误

tyg4sfes  于 2023-03-13  发布在  其他
关注(0)|答案(1)|浏览(92)

我有一个AVAudioRecorder正在录制语音命令,然后我使用以下代码将其转换为nsdata-
NSData * 数据= [具有URL内容的NSData数据:记录器.url];
现在我用下面的代码发出一个语音识别请求-
NSDictionary * 标题= @{ @“授权”:[NSString字符串格式:@“承载方%@",身份验证代码],@“传输编码”:@“分块”,@“内容类型”:@“多部分/表格-数据;边界=某个边界”};

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);
            }
}];

[数据任务恢复];
但是上面的代码给了我一个500的错误。任何帮助都非常感谢

2hh7jdfx

2hh7jdfx1#

我知道哪里出了问题。音频数据之前的内容类型应该是

[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”

相关问题