text to speech conversion

2019-02-27 17:31发布

问题:

We are making iPhone app where client want reminder as voice message.

Requirement is user will set time and text they want for for reminder.

Using text, I will convert to speech and play the audio file when reminder is triggered.

For this, I planned to use google service

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

Play those text and download the audio file for the same.

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

I can use those code, but client don't want to go online.

Is there any library which can do text-to-speech conversion without internet (like siri is doing)?

Any info on this would be great.

回答1:

Use 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



回答2:

This works iOS 7 onwards ONLY

Below is the code I used.

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

Imports used are

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

Languages supported are as below.

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



回答3:

Starting from iOS7, Apple already provide that in its SDK as per below:

Apple documentation