我调用以下函数以使用 google api 将语音转换为文本。我在最新版本的 xcode 中执行此操作,并为 mac 制作此应用程序。问题是下面的功能似乎中断了,NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:nil];
所以我无法检查语音。我尝试过异步调度,但似乎没有帮助。我也尝试了 @autoreleasepool {} 但它似乎也没有帮助。任何帮助表示赞赏!
-(void)processSpeech {
NSURL *url = [NSURL fileURLWithPath:@"/Users/marcus/Documents/testvoices/okSpeak.query.flac"];
NSData *flacFile = [NSData dataWithContentsOfURL:url];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.google.com/speech-api/v1/recognize?lang=en-US"]];
[request setHTTPMethod:@"POST"];
[request addValue:@"Content-Type" forHTTPHeaderField:@"audio/x-flac; rate=16000"];
[request addValue:@"audio/x-flac; rate=16000" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:flacFile];
[request setValue:[NSString stringWithFormat:@"%ld",[flacFile length]] forHTTPHeaderField:@"Content-length"];
NSHTTPURLResponse *urlResponse = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:nil];
NSString *rawData = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Raw: %@",rawData);
return rawData;
}