文本到语音转换

时间:2014-03-05 11:04:47

标签: ios objective-c text-to-speech

我们正在制作iPhone应用程序,客户希望将提醒作为语音消息。

要求是用户将设置他们想要提醒的时间和文本。

使用文字,我会转换为语音,并在触发提醒时播放音频文件。

为此,我计划使用谷歌服务

http://www.translate.google.com/translate_tts?tl=ar&q=%D9%85%D8%B1%D8%AD%D8%A8%D8%A7%20%D8%B5%D8%AF%D9%8A%D9%82%D8%8C%20%D9%83%D9%8A%D9%81%20%D8%AD%D8%A7%D9%84%D9%83%D8%9F

http://www.translate.google.com/translate_tts?tl=en&q=helloE%20friend

播放这些文字并下载相同的音频文件。

NSString* userAgent = @"Mozilla/5.0";

NSURL *url = [NSURL URLWithString:[@"http://www.translate.google.com/translate_tts?tl=en&q=helloE%20friend" 
                                   stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];


NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];

[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];


NSURLResponse* response = nil;
NSError* error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request
                                     returningResponse:&response
                                                 error:&error];



[data writeToFile:@"/var/tmp/tts.mp3" atomically:YES];

我可以使用这些代码,但客户不想上网。

是否有任何图书馆可以在没有互联网的情况下进行文字转语音转换(如siri正在做的那样)?

关于此的任何信息都会很棒。

3 个答案:

答案 0 :(得分:6)

使用AVSpeechSynthesizer

AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc]init];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"Some random text that you want to be spoken"];
[utterance setRate:0.7];
[synthesizer speakUtterance:utterance];

Reference

答案 1 :(得分:5)

这仅适用于iOS 7以上

以下是我使用的代码。

NSString *myStr;

//    myStr = @"Hello friend, how are you?";
myStr = @"مرحبا صديق، كيف حالك؟";

AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc]init];

AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:myStr];
[utterance setRate:0.2f];
//    utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-us"];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"ar-SA"];
[synthesizer speakUtterance:utterance];

使用的进口货物是

#import <AVFoundation/AVFoundation.h>
#import <QuartzCore/QuartzCore.h>

支持的语言如下。

Arabic (Saudi Arabia) - ar-SA
Chinese (China) - zh-CN
Chinese (Hong Kong SAR China) - zh-HK
Chinese (Taiwan) - zh-TW
Czech (Czech Republic) - cs-CZ
Danish (Denmark) - da-DK
Dutch (Belgium) - nl-BE
Dutch (Netherlands) - nl-NL
English (Australia) - en-AU
English (Ireland) - en-IE
English (South Africa) - en-ZA
English (United Kingdom) - en-GB
English (United States) - en-US
Finnish (Finland) - fi-FI
French (Canada) - fr-CA
French (France) - fr-FR
German (Germany) - de-DE
Greek (Greece) - el-GR
Hindi (India) - hi-IN
Hungarian (Hungary) - hu-HU
Indonesian (Indonesia) - id-ID
Italian (Italy) - it-IT
Japanese (Japan) - ja-JP
Korean (South Korea) - ko-KR
Norwegian (Norway) - no-NO
Polish (Poland) - pl-PL
Portuguese (Brazil) - pt-BR
Portuguese (Portugal) - pt-PT
Romanian (Romania) - ro-RO
Russian (Russia) - ru-RU
Slovak (Slovakia) - sk-SK
Spanish (Mexico) - es-MX
Spanish (Spain) - es-ES
Swedish (Sweden) - sv-SE
Thai (Thailand) - th-TH
Turkish (Turkey) - tr-TR

Reference

答案 2 :(得分:1)

从iOS7开始,Apple已经在其SDK中提供了以下内容:

Apple documentation