无法通过Google TTS Api获取音频数据

时间:2016-03-07 05:53:11

标签: ios

无法通过GoogleTTS API获得回复。

NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);     NSString * documentsDirectory = [paths objectAtIndex:0];     NSString * path = [documentsDirectory stringByAppendingPathComponent:@“file.mp3”];

NSString *text = @"Hello there.";
NSString *urlString = [NSString stringWithFormat:@"http://www.translate.google.com/translate_tts?tl=en&key=%@&q=%@",GOOGLE_SPEECH_TO_TEXT_KEY,text];
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url] ;
[request setValue:@"Mozilla/5.0" forHTTPHeaderField:@"User-Agent"];
NSURLResponse* response = nil;
NSError* error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request
                                     returningResponse:&response
                                                 error:&error];
[data writeToFile:path atomically:YES];

NSData *fileData = [NSData dataWithContentsOfFile:path];
_audioPlayer = [[AVAudioPlayer alloc] initWithData:fileData error:&error];
[_audioPlayer prepareToPlay];

0 个答案:

没有答案