谷歌的语音识别API
我想(仅用于测试)使用谷歌API:谷歌的语音识别API
https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US
我的问题是:我应该如何发送POST请求,这个网址是什么?我使用的是:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *recDir = [paths objectAtIndex:0];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/recordTest.flac", recDir]];
NSData *myData = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/recordTest.flac", recDir]];
//NSString *audio = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@/recordTest.flac", recDir]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:[NSURL
URLWithString:@"https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US"]];
[request setHTTPMethod:@"POST"];
//set headers
[request addValue:@"Content-Type" forHTTPHeaderField:@"audio/x-flac; rate=16000"];
[request addValue:@"audio/x-flac; rate=16000" forHTTPHeaderField:@"Content-Type"];
NSString *requestBody = [[NSString alloc] initWithFormat:@"Content=%@", myData];
[request setHTTPBody:[requestBody dataUsingEncoding:NSASCIIStringEncoding]];
[request setValue:[NSString stringWithFormat:@"%d",[myData length]] forHTTPHeaderField:@"Content-length"];
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"The answer is: %@",result);
但我只得到
{
"status":5,
"id":"fe6ba68a593f9919f5fd33e819d493a0-1",
"hypotheses":[
HERE SHOULD BE THE TEXT
]
}
哪些错误/我应该怎么办?
而是用相当混乱的可可实施的烦心事,看看我非常容易使用的(一个函数)C库谷歌语音:http://github.com/H2CO3/libsprec
是否有任何示例代码 –
@VipinVijay只是访问GitHub回购链接。有两个示例程序。 – 2012-11-03 14:40:23
可以请你提供一个关于我的链接 –
FYI 状态:0 - 正确的, 状态:4 - 缺少音频文件, 状态:5 - 音频文件不正确。
In place of ;
NSString *requestBody = [[NSString alloc] initWithFormat:@"Content=%@", myData];
[request setHTTPBody:[requestBody dataUsingEncoding:NSASCIIStringEncoding]];
Just Use;
[request setHTTPBody:myData];
这里是工作的代码以供参考:
- (无效)googleSTT {
NSString *homeDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", homeDirectory, @"test.flac"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:[NSURL
URLWithString:@"https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US"]];
[request setHTTPMethod:@"POST"];
//set headers
[request addValue:@"Content-Type" forHTTPHeaderField:@"audio/x-flac; rate=16000"];
[request addValue:@"audio/x-flac; rate=16000" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:myData];
[request setValue:[NSString stringWithFormat:@"%d",[myData length]] forHTTPHeaderField:@"Content-length"];
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"The answer is: %@",result);
}
我用同样的问题挣扎,但后来我通过解决它参考此链接https://github.com/gillesdemey/google-speech-v2.and只需 用此替换您的NSMutableURLRequest *请求 NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"https://github.com/gillesdemey/google-speech-v2"]];
+1,有趣的一个。 – 2012-06-22 02:39:33
顺便说一下,你滥用'addValue:forHTTPHeaderField:'和'setHTTPBody:'方法 - 在文档中查找它们的用法。 – 2012-06-22 02:42:26