6

我想使用这个 Google API(仅用于测试):

https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US

我的问题是:我应该如何向这个 URL 发送 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
 ]
}

怎么了/我该怎么办?

4

3 回答 3

2

仅供参考,状态:0 – 正确,状态:4 – 缺少音频文件,状态:5 – 音频文件不正确。

  In place of ;

    NSString *requestBody = [[NSString alloc] initWithFormat:@"Content=%@", myData];
    [request setHTTPBody:[requestBody dataUsingEncoding:NSASCIIStringEncoding]];

只需使用;

    [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);

}

于 2013-01-03T12:37:24.543 回答
0

与其为相当混乱的 Cocoa 实现而烦恼,不如查看我用于 Google Speech 的极其易于使用(一个函数)的 C 库:http: //github.com/H2CO3/libsprec

于 2012-06-22T02:38:51.497 回答
0

我在同样的问题上苦苦挣扎,但后来我通过参考这个链接解决了这个问题https://github.com/gillesdemey/google-speech-v2.and只是用这个替换了你的 NSMutableURLRequest *request NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"https://github.com/gillesdemey/google-speech-v2"]];

于 2014-09-18T13:49:59.040 回答