ios - GoogleLiveTTS 不适用于 iOS

标签 ios google-text-to-speech

Google Text to Speech 可以正常工作,但并非总是如此。它很少工作(15 次中有 1 次)。如果我获取 URL 并从浏览器浏览它,我可以听到音频播放但它没有在我的应用程序中播放。以下是在 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];

}

调用speak方法的方法:

-(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];

}

关于解决这个问题有什么想法吗? 提前致谢。

最佳答案

Google Text-to-Speech 现在不是开源的。他们添加了一个带有可下载的 tts mp3 url 的 key 。您可以随机生成 key ,这可能会有所帮助。 但在 iOS 中有谷歌文本转语音的替代品。我用了AVSpeechSynthesizer对于某些语言。文章写于2014年,之后苹果在iOS SDK中添加了一些其他语言。使用此代码段检查现有语言代码:

for(AVSpeechSynthesisVoice *[AVSpeechSynthesisVoice speechVoices] 中的语音) { NSLog(@"%@", voice.language); }

Sabdkosh 中有一些印度语言可用于 tts .

关于ios - GoogleLiveTTS 不适用于 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34015191/

相关文章:

java - Android TTS如何在textview中显示说话的单词

java - 使用 google texttospeech : Could not find TLS ALPN provider; no working netty-tcnative, Conscrypt 时出错,或 Jetty NPN/ALPN 可用

ios - Firebase 身份验证尝试使用 Apple 的身份 token 会在 Xamarin.iOS 应用程序中返回 17999 INVALID_CREDENTIAL_OR_PROVIDER_ID

ios - 从 Parse 对象设置 UIPickerView 中的选定行

ios - 快速将 UIPageControl 添加到 titleView

text-to-speech - 如何在谷歌文本到语音中使用Phonetic或Phoneme发音?

ios - 如何在 ios 中将 .mp3 文件从 NSBundle 添加到 NSArray

ios - 远程 IO 音频非常嘈杂

java - 如何使用 Google TTS java 客户端修复 "Could not find policy ' pick_first'"?

android - 文本转语音初始化延迟