Skip to content

Instantly share code, notes, and snippets.

@y0unghe
Created August 29, 2013 06:28
Show Gist options
  • Save y0unghe/6374799 to your computer and use it in GitHub Desktop.
Save y0unghe/6374799 to your computer and use it in GitHub Desktop.
用google tts api发音
/*
q 要发音的中文或英文词语、句子
tl 发音语言
http://translate.google.com/translate_tts?ie=UTF-8&q=你好&tl=zh-CN&total=1&idx=0&textlen=11&prev=input
*/
NSString* userAgent = @"Mozilla/5.0";
NSURL *url = [NSURL URLWithString:[@"http://translate.google.com/translate_tts?ie=UTF-8&q=你好&tl=zh-CN&total=1&idx=0&textlen=11&prev=input"
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSError *playerError = nil;
AVAudioPlayer * avPlayerObject = [[AVAudioPlayer alloc] initWithData:data error:&playerError];
if(avPlayerObject)
{
NSLog(@"%f", avPlayerObject.duration);
[avPlayerObject play];
} else
{
NSLog(@"cant't play");
}
}];
@XiaoYulong
Copy link

谢谢!有google tts 的文档吗?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment