This question already has an answer here:
-
Text to speech on iPhone [closed]
9 answers
I'm in the process of building a app that helps students learn foreign languages. I notice the iPhone can read foreign language text in Safari when I have "Speak Selection" turned on in the Accessibility menu in settings. Is it possible to programmatically have the in-built text-to-speech software on the phone read a foreign word (an NSString) which is being displayed in a UIView?
Me too tried to implement TTS in my application, i tried to implement it with some SDK but i cant. Now i am using GOOGLE TRANSLATE API ["http://www.translate.google.com/translate_tts?tl=en&q=%@",text"]. that will covert your text and give you a audio file you need to run the audio file after you stored it in Document directory [or customize as you like]. Just use this code. Hope it will help.
//Conversion using Google TTS API
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"file.mp3"];
NSString *text = textToConvert.text;
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];
NSError *err;
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
{
// player = [[AVAudioPlayer alloc] initWithContentsOfURL:
// [NSURL fileURLWithPath:path] error:&err];
//// player.volume = 0.4f;
// [player prepareToPlay];
//// [player setNumberOfLoops:0];
// [player play];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&err];
// player.delegate = self;
[player prepareToPlay];
[player play];
}