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

我正在使用这个代码的API:

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

哪些工作,但只有短句(less于10个字左右)我做错了什么? 如何在不降低语音质量的情况下解决这个问题或将其分解成几个文本?

Google TTS限制为100个字元

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

您可以通过执行以下步骤来实现这一点。

  1. 把你的长句分成小块100个字符。
  2. 使用第一个分隔100个字符的string呼叫Google TTS。
  3. 使用Google TTS和AVAudioPlayer播放
  4. 实现AVAudioPlayer audioPlayerDidFinishPlaying委托。
  5. 在该代表中,使用第二个分隔100个字符的string调用Google TTS。
  6. 调用过程recursion直到到达最后一个字符。

这是我为你创build的Google-TTS-Library-For-iOS库:)

Google TTS限制为100个字元。

我最近创build了一个对这个问题有很大帮助的lib。 有一些改进,但请自由使用它。 GoogleTTSAPI