iPhone SDK - Google TTS 和编码

发布于 2024-11-05 16:56:42 字数 668 浏览 1 评论 0原文

我正在开发一个支持多语言的文本转语音 iPhone 应用程序。

这是我的英语请求 URL

requestUrlStr = @"http://www.translate.google.com/translate_tts?tl=en&q=hello";

,上面的 url 没有问题

,但是对于中文,

requestUrlStr = @"http://www.translate.google.com/translate_tts?tl=zh-TW&q=你好";

我知道上面的 url 会给出“Bad URL”,所以我使用 follow 方法将字符串编码为 UTF-8

requestUrlStr = [requestUrlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

它将变成

http://www.translate.google.com/translate_tts?tl=zh-CN&q=%E4%BD%A0%E5%A5%BD

那么 Google TTS 无法识别这个中文文本。

I am developing a text-to-speech iphone app that support multi-languages.

Here is my request URL

requestUrlStr = @"http://www.translate.google.com/translate_tts?tl=en&q=hello";

for english the above url has no problem

but for Chinese

requestUrlStr = @"http://www.translate.google.com/translate_tts?tl=zh-TW&q=你好";

I know the above url will give 'Bad URL', so I used follow method to encode the string into UTF-8

requestUrlStr = [requestUrlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

It will become

http://www.translate.google.com/translate_tts?tl=zh-CN&q=%E4%BD%A0%E5%A5%BD

Then the Google TTS cannot recognize this Chinese text.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风无影 2024-11-12 16:56:42

您必须假装是 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];

更新 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];

.. .delegate:nil delegateQueue:[NSOperationQueue mainQueue] 可以省略。

You have to pretend to be a User-Agent other than the default (appName, etc) in your NSURLRequest. Try this (I use Greek language) ...

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];

UPDATE 2017

Since our favorite companies enjoy to update and deprecate things, here is the above example as it should be now...

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];

The ...delegate:nil delegateQueue:[NSOperationQueue mainQueue] can be omitted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文