0

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?

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
Tim
  • 1,428
  • 19
  • 26

1 Answers1

2

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];
    }
Ganapathy
  • 4,594
  • 5
  • 23
  • 41
  • Thanks for posting Ganapathy. How fast do you find this given it has to query the google translate website? – Tim Apr 08 '13 at 21:44
  • Thats based on your text message. i have implemented in my application its good only. – Ganapathy Apr 09 '13 at 01:08