首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在iOS上使用google Text-To-Speech (TTS) API

在iOS上使用google Text-To-Speech (TTS) API
EN

Stack Overflow用户
提问于 2013-04-17 18:44:35
回答 2查看 9.6K关注 0票数 4

我将API与以下代码一起使用:

代码语言:javascript
复制
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"file.mp3"];

NSString *text = textToTranslate; //@"You are one chromosome away from being a potato.";
NSString *urlString = [NSString stringWithFormat:@"http://www.translate.google.com/translate_tts?tl=en&q=%@",text];
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url] ;
[request setValue:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1" forHTTPHeaderField:@"User-Agent"];
NSURLResponse* response = nil;
NSError* error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request
                                     returningResponse:&response
                                                 error:&error];
[data writeToFile:path atomically:YES];

SystemSoundID soundID;
NSURL *url2 = [NSURL fileURLWithPath:path];

AudioServicesCreateSystemSoundID((__bridge CFURLRef)url2, &soundID);
AudioServicesPlaySystemSound (soundID);

它只适用于短句(大约少于10个单词)我做错了什么?我如何才能在不降低语音质量的情况下解决这个问题或将其分成几个文本?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-19 09:25:30

谷歌TTS限于100个字符的

所以你应该把你的长句子分成100个字符的小块,然后把它传递给Google TTS方法。

您可以通过执行以下步骤来实现此目的。

  1. 将您长句子拆分为100个字符的小块。
  2. 使用第一个拆分100个字符串的Google调用Google。
  3. 使用Google & AVAudioPlayer
  4. Implement AVAudioPlayer audioPlayerDidFinishPlaying委托播放它。在委托中,使用第二个拆分100个字符串调用Google。

H117递归调用该过程,直到到达最后一个字符。H218G219

下面是我为您创建的库:)

票数 3
EN

Stack Overflow用户

发布于 2013-07-28 12:44:24

Google TTS限制为100个字符。

我最近创建了一个库,它对解决这个问题有很大帮助。有一些改进要做,但请免费使用它。GoogleTTSAPI

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16057961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档