a38*_*380 5 iphone text-to-speech
我正在开发一个支持多语言的文本到语音的iPhone应用程序.
这是我的请求网址
requestUrlStr = @"http://www.translate.google.com/translate_tts?tl=en&q=hello";
Run Code Online (Sandbox Code Playgroud)
对于英语,上面的网址没有问题
但对中国人来说
requestUrlStr = @"http://www.translate.google.com/translate_tts?tl=zh-TW&q=??";
Run Code Online (Sandbox Code Playgroud)
我知道上面的url会给出'Bad URL',所以我用follow方法将字符串编码成 UTF-8
requestUrlStr = [requestUrlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Run Code Online (Sandbox Code Playgroud)
它会变成
http://www.translate.google.com/translate_tts?tl=zh-CN&q=%E4%BD%A0%E5%A5%BD
Run Code Online (Sandbox Code Playgroud)
然后Google TTS无法识别此中文文本.
Vas*_*lis 10
你必须伪装成NSURLRequest中默认(appName等)以外的用户代理.试试这个(我用希腊语)...
NSString* userAgent = @"Mozilla/5.0";
NSURL *url = [NSURL URLWithString:[@"http://www.translate.google.com/translate_tts?tl=el&q=????????"
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
NSURLResponse* response = nil;
NSError* error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
[data writeToFile:@"/var/tmp/tts.mp3" atomically:YES];
Run Code Online (Sandbox Code Playgroud)
2017年更新
由于我们最喜欢的公司喜欢更新和弃用的东西,这里是上面的例子,因为它应该是现在...
NSString* text = @"????????";
NSString* lang = @"el";
NSString* sUrl = [NSString stringWithFormat:@"https://translate.google.com/translate_tts?q=%@&tl=%@&client=tw-ob", text, lang];
sUrl = [sUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
NSURL* url = [NSURL URLWithString:sUrl];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:@"Mozilla/5.0" forHTTPHeaderField:@"User-Agent"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
delegate:nil
delegateQueue:[NSOperationQueue mainQueue]];
[[session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
[data writeToFile:@"/var/tmp/tts.mp3" atomically:YES];
}
] resume];
Run Code Online (Sandbox Code Playgroud)
的...delegate:nil delegateQueue:[NSOperationQueue mainQueue]可被省略.
| 归档时间: |
|
| 查看次数: |
8919 次 |
| 最近记录: |