2011-11-24 129 views
6

我想(僅用於測試)使用谷歌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 
] 
} 

哪些錯誤/我應該怎麼辦?

+0

+1,有趣的一個。 – 2012-06-22 02:39:33

+0

順便說一下,你濫用'addValue:forHTTPHeaderField:'和'setHTTPBody:'方法 - 在文檔中查找它們的用法。 – 2012-06-22 02:42:26

回答

0

而是用相當混亂的可可實施的煩心事,看看我非常容易使用的(一個函數)C庫谷歌語音:http://github.com/H2CO3/libsprec

+0

是否有任何示例代碼 –

+0

@VipinVijay只是訪問GitHub回購鏈接。有兩個示例程序。 – 2012-11-03 14:40:23

+0

可以請你提供一個關於我的鏈接 –

2

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

}

+0

我試過你的代碼,但得到空結果字符串,我所做的是記錄一個wav音頻文件,然後將其轉換爲flac文件,現在使用你的代碼但沒有成功,請指導我更多。 – Mak13

+0

嘿我解決了它:)謝謝 – Mak13

0

我用同樣的問題掙扎,但後來我通過解決它參考此鏈接https://github.com/gillesdemey/google-speech-v2.and只需 用此替換您的NSMutableURLRequest *請求 NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"https://github.com/gillesdemey/google-speech-v2"]];