Saturday, October 22, 2011

use Bing Language TTS & Google TTS with Objective-C

Often times you need to translate text to speech and play it for the end user. The google TTS is very effective as well as the Bing's TTS. I'll include both in this example.



- (void)dictateText
{
    
    NSString *outputText = [[NSString alloc] initWithFormat:@"%@", [output text]];
    NSString *toLang = [[NSString alloc] initWithFormat:@"%@", [to text]];
    NSString *fromLang = [[NSString alloc] initWithFormat:@"%@", [from text]];
    
    NSString *urlString = [NSString stringWithFormat:@"http://api.microsofttranslator.com/v2/Http.svc/Speak?appId=5DD2F722273E26C2045108AD8D7E9F194E8AA30F&text=%@&language=%@",outputText,toLang];
    
    
    NSLog(@"to language is:%@", toLang);
    
    
    if ([toLang isEqualToString:@"fa"] || [toLang isEqualToString:@"hi"] || [toLang isEqualToString:@"tr"] || [toLang isEqualToString:@"hu"] || [toLang isEqualToString:@"id"] || [toLang isEqualToString:@"sk"] || [toLang isEqualToString:@"hu"] || [toLang isEqualToString:@"id"] || [toLang isEqualToString:@"sk"] || [toLang isEqualToString:@"hu"] || [toLang isEqualToString:@"id"] || [toLang isEqualToString:@"sk"] ||[toLang isEqualToString:@"ht"] || [toLang isEqualToString:@"lv"] || [toLang isEqualToString:@"hy"] || [toLang isEqualToString:@"vi"] || [toLang isEqualToString:@"el"]  || [toLang isEqualToString:@"cs"] || [toLang isEqualToString:@"ro"] ||  [toLang isEqualToString:@"la"] || [fromLang isEqualToString:@"la"] || [toLang isEqualToString:@"ar"]  )
    {
      //  exit(0);
        
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"file.mp3"];
        
        
        urlString = [NSString stringWithFormat:@"http://www.translate.google.com/translate_tts?tl=%@&q=%@",toLang,outputText];
        
        
        NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        
        NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
        [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];
        
        AVAudioPlayer  *player;
        NSError        *err;
        if ([[NSFileManager defaultManager] fileExistsAtPath:path]) 
        {    
            player = [[AVAudioPlayer alloc] initWithContentsOfURL:
                      [NSURL fileURLWithPath:path] error:&err];
            player.volume = 1.0f;
            [player prepareToPlay];
            [player setNumberOfLoops:0];
            [player play];    
        }

    }
    else
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"file.wav"];
        
        NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
        [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];
        
        AVAudioPlayer  *player;
        NSError        *err;
        if ([[NSFileManager defaultManager] fileExistsAtPath:path]) 
        {    
            player = [[AVAudioPlayer alloc] initWithContentsOfURL:
                      [NSURL fileURLWithPath:path] error:&err];
            player.volume = 1.0f;
            [player prepareToPlay];
            [player setNumberOfLoops:0];
            [player play];    
        }
        
        AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:urlString] error:NULL];
       // theAudio.delegate = self;
        theAudio.volume = 1.0f;
        [theAudio play];
        
    }
    
}

No comments:

Post a Comment