user1380136
user1380136

Reputation:

How to change Language in Google TTS API in iOS?

I am using Google Text To Speech Services in iOS.

I am ok in English TTS with following codes.

NSString *queryTTS = [text stringByReplacingOccurrencesOfString:@" " withString:@"+"];

NSString *linkTTS = [NSString stringWithFormat:@"http://translate.google.com/translate_tts?tl=en&q=%@",queryTTS];

NSData *dataTTS = [NSData dataWithContentsOfURL:[NSURL URLWithString:linkTTS]];

_googlePlayer = [[AVAudioPlayer alloc] initWithData:dataTTS error:nil]; 
[_googlePlayer play];

So i changed url address to speech thailand TTS like following link.

NSString *queryTTS = [text stringByReplacingOccurrencesOfString:@" " withString:@"+"];

NSString *linkTTS = [NSString stringWithFormat:@"http://translate.google.com/translate_tts?tl=th&q=%@",queryTTS];

NSData *dataTTS = [NSData dataWithContentsOfURL:[NSURL URLWithString:linkTTS]];

_googlePlayer = [[AVAudioPlayer alloc] initWithData:dataTTS error:nil]; 
[_googlePlayer play];

After I changed that link into "th" , that google TTS services not speech my thai text.

Am I wrong in my codes?

Or If no , how can I change languages in Google Text To Speech API?

Thanks you for reading.

Upvotes: 2

Views: 1626

Answers (1)

arsenius
arsenius

Reputation: 13266

Just wanted to say thanks. I was looking for how to use the API on iPhone and this helped. Also, I tried changing the language to Thai and had no problem.

* ** UPDATE ** *

Sorry, I was not able to get it work. I didn't have any Thai text, so I just used English and it spelled the word out. I assumed that meant it was working. However, when I changed to Korean and Chinese using proper words I was not able to hear anything. English, French, German and Italian all worked, however.

I was able to fix this by adding the following:

NSURL *url = [NSURL URLWithString:[linkTTS stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *dataTTS = [NSData dataWithContentsOfURL:url];

The problem in my case (and probably yours) was that there were international characters in the URL. That caused [NSURL URLWithString] to return nil.

That will work in the simulator, and to get the rest of the way, check out this thread:

iPhone SDK - Google TTS and encoding

Basically, you have to change your user agent to something else, like "Mozilla/5.0".

Upvotes: 0

Related Questions