Ashik
Ashik

Reputation: 1259

GoogleLiveTTS not working for iOS

Google Text to Speech is working but not always. It's working very rarely (1 out of 15 times). If I take the URL and browse it from browser, I can hear the audio playing but it's not playing in my app. Here is the method that's called in GoogleLiveTTS.m:

    -(void)speak:(NSString *)text InLanguage:(NSString *)languageCode{
    NSString *urlString = [NSString stringWithFormat:@"http://translate.google.com/translate_tts?tl=%@&q=%@&client=t",languageCode,[text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSURL *url = [NSURL URLWithString:urlString];

    NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]
                                       initWithURL:url];

    NSLog(@"URL:%@",urlRequest);

    NSString *userAgent = @"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:25.0) Gecko/20100101 Firefox/25.0";
    [urlRequest setValue:userAgent forHTTPHeaderField:@"User-Agent"];

    //NSData *responseData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:NULL error:NULL];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if([self.audioPlayer isPlaying])
            [self.audioPlayer stop];
        self.audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:&connectionError];
        [self.audioPlayer prepareToPlay];
        [self.audioPlayer play];

    }];
    [queue release];

}

The method that speak method is called from:

-(IBAction)btnSpeak_Click:(id)sender{
if (self.ttsCandidate && [[AppDelegate sharedDelegate]connectedToInternet]) {
    [[AppDelegate sharedDelegate].tts speak:self.ttsCandidate];
    if(self.isEnglishText)
        [[AppDelegate sharedDelegate].tts speak:self.ttsCandidate InLanguage:@"en"];
    else
        [[AppDelegate sharedDelegate].tts speak:self.ttsCandidate InLanguage:LanguageCode];

}
else if (![[AppDelegate sharedDelegate]connectedToInternet]){
    toast = [self.view makeToast:@"Connection error." duration:3.0 position:@"bottom"];

    [self performSelector:@selector(hideToast) withObject:nil afterDelay:2.0];

}

}

Any idea about solving this problem? Thanks in Advance.

Upvotes: 1

Views: 212

Answers (2)

Alejandro Cotilla
Alejandro Cotilla

Reputation: 2621

As Ashik pointed out, you could use AVSpeechSynthesizer. But if you're after better audio quality, you can use Google Cloud Text-to-Speech API.

I made a tutorial on how to integrate it into your iOS app, you can find it here.

There's also a demo app here.

Upvotes: 0

Ashik
Ashik

Reputation: 1259

Google Text-to-Speech is not open source now. They added a key with the downloadable tts mp3 url. You can generate the key randomly, it might help a couple of time. But there is replacement for Google Text-to-speech in iOS. I used AVSpeechSynthesizer for some languages. The article is written in 2014, apple added some other languages in iOS SDK since then. Check the existing language codes with this snippet:

for(AVSpeechSynthesisVoice *voice in [AVSpeechSynthesisVoice speechVoices]) { NSLog(@"%@", voice.language); }

There are some Indian languages available for tts in Sabdkosh.

Upvotes: 1

Related Questions