Skip to content

Instantly share code, notes, and snippets.

@ziggear
Created April 30, 2013 10:34
Show Gist options
  • Save ziggear/5487902 to your computer and use it in GitHub Desktop.
Save ziggear/5487902 to your computer and use it in GitHub Desktop.
#define GOOGLE_AUDIO_URL @"http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN"
[self uploadAudioToGoogle:GOOGLE_AUDIO_URL];
- (void) uploadAudioToGoogle:(NSString *)sURL
{
NSLog(@"%@", sURL);
NSURL *url = [NSURL URLWithString:sURL];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addRequestHeader:@"Content-Type" value:@"audio/x-flac; rate=16000"];
request.requestMethod = @"POST";
NSString *filePath_dest = [NSTemporaryDirectory() stringByAppendingFormat:@"dest.flac"];
NSLog(@"dest.flac path:%@", filePath_dest);
[request appendPostDataFromFile:filePath_dest];
NSLog(@"%@", request.postData);
self.httpRequest = request;
[request startSynchronous];
NSError *error = [request error];
if (!error)
{
NSString *responseString = [request responseString];
NSLog(@"sURL:%@\n谷歌返回内容:%@", sURL, responseString);
// 解析Google语音识别返回的内容
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *dic = [parser objectWithString:responseString];
if(dic == nil || [dic count] <= 0)
{
[parser release];
if(![self isCancelled])
[delegate requestError:nil];
return;
}
NSArray *array = [dic objectForKey:@"hypotheses"];
NSLog(@"%@", array);
NSDictionary *dic_hypotheses = [array objectAtIndex:0];
NSLog(@"%@", dic_hypotheses);
NSString *sContent = [NSString stringWithFormat:@"%@", [dic_hypotheses objectForKey:@"utterance"]];
if(sContent != nil && [sContent length] > 0)
{
if(![self isCancelled])
[delegate didFinishRequest:sContent];
}
[parser release];
}
if((nil != error) && [error localizedDescription])
{
NSError *error = [request error];
NSLog(@"error = %@",error);
if(![self isCancelled])
[delegate requestError:error];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment