4

I'm using the API with this code:

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

Which works but only on short sentences (less than 10 words approx.) What am I doing wrong? How can I solve that or separate into few texts without reducing the speech quality?

Idan
  • 9,880
  • 10
  • 47
  • 76

2 Answers2

3

Google TTS limited to 100 characters.

So you should split-up your long sentence in to small 100 character chunks and pass it to the Google TTS method.

You can achieve this through implementing below steps.

  1. Split-up your long sentence in to small 100 character chunks.
  2. Call Google TTS with first split 100 character string.
  3. Play it using Google TTS & AVAudioPlayer
  4. Implement AVAudioPlayer audioPlayerDidFinishPlaying delegate .
  5. In that delegate, call Google TTS with second split 100 character string.
  6. Call the process recursively until reach the last character.

Here is my Google-TTS-Library-For-iOS library that I created for you :)

Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
  • This really helps but still sentences are cut by characters and not by words (easy to fix), Main problem is there is a big quiet between the two 100 chars sentences (maybe because the second request is made only after finish playing and not before...). – Idan Apr 20 '13 at 09:12
  • nope.. what you need to achieve? do you need source code? find here https://github.com/marceloqueiroz/GoogleTTSAPI – Shamsudheen TK Jan 03 '14 at 07:13
  • 1
    @Idan did you find a fix for the "pause/quiet" between 100-char blocks? – Crashalot Mar 06 '16 at 06:23
  • @Crashalot It was a long time ago so I think all this should be re-evaluate. At that time I just used another service provider for TTS. – Idan Mar 06 '16 at 10:14
2

Google TTS is limited to 100 characters.

I recently created a lib that helped a lot with that problem. There are some improvements to be made, but please free to use it. GoogleTTSAPI

Marcelo
  • 21
  • 3
  • I'll try this one because you provide the source code. Thanks – Bagusflyer Jan 03 '14 at 07:13
  • thanks for sharing! quick question: does this solve the pause that occurs between 100-character utterances, i.e., if you break a 500-char post into 5 chunks, will the 5 chunks be smoothly linked together or will there be noticeable gaps between chunks? – Crashalot Mar 06 '16 at 06:25