在iOS上使用谷歌文字转语音(TTS)API [英] Using google Text-To-Speech (TTS) API on iOS

查看:194
本文介绍了在iOS上使用谷歌文字转语音(TTS)API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有此代码的API:

I'm using the API with this code:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"file.mp3"];

NSString *text = textToTranslate; //@"You are one chromosome away from being a potato.";
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];

SystemSoundID soundID;
NSURL *url2 = [NSURL fileURLWithPath:path];

AudioServicesCreateSystemSoundID((__bridge CFURLRef)url2, &soundID);
AudioServicesPlaySystemSound (soundID);

哪个有效,但只适用于短句(约不超过10个字)
什么是我做错了?如何在不降低语音质量的情况下解决这个问题或将其分成几个文本?

Which works but only on short sentences (less than 10 words approx.) What am I doing wrong? How can I solve that or separate into few texts without reducing the speech quality?

推荐答案

Google TTS仅限 100个字符

Google TTS limited to 100 characters.

所以你应该将你的长句分成小的100个字符块并将其传递给Google TTS方法。

So you should split-up your long sentence in to small 100 character chunks and pass it to the Google TTS method.

您可以通过实施以下步骤来实现这一目标。


  1. 将你的长句分成小的100个字符块。

  2. 使用第一个拆分的100个字符串来调用Google TTS。

  3. 播放它使用Google TTS& AVAudioPlayer

  4. 实施AVAudioPlayer audioPlayerDidFinishPlaying委托。

  5. 在该委托中,使用第二个拆分的100个字符串调用Google TTS。

  6. 递归调用该进程,直到到达最后一个字符。

  1. Split-up your long sentence in to small 100 character chunks.
  2. Call Google TTS with first split 100 character string.
  3. Play it using Google TTS & AVAudioPlayer
  4. Implement AVAudioPlayer audioPlayerDidFinishPlaying delegate .
  5. In that delegate, call Google TTS with second split 100 character string.
  6. Call the process recursively until reach the last character.

这是我的 Google-TTS-Library-For-iOS 我为您创建的库:)

Here is my Google-TTS-Library-For-iOS library that I created for you :)

这篇关于在iOS上使用谷歌文字转语音(TTS)API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆