我正在調用下面的函數,使用google api將語音轉換爲文本。我在最新版本的xcode中做這件事,我爲Mac製作這個應用程序。問題是下面的函數似乎在NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:nil];
中斷,所以我無法檢查語音。我試過一個異步調度,但它似乎沒有幫助。我也嘗試了@autoreleasepool {},但它似乎也沒有幫助。任何幫助表示讚賞!NSURLConnection在目標c中未被正確調用,適用於Mac應用
-(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;
}
您是否收到錯誤?沒有數據返回?還有別的嗎? – Zarathuztra