#import <AVFoundation/AVFoundation.h>
基本编程框架:
头文件定义
@interface Text2Speech :NSObject<AVSpeechSynthesizerDelegate>
{
NSString * text;//需要发音的文本
float rate;//速率
float pitch;//音高
int timeInterval;//每句话的间隔
enum EnglishType voice;//英语的种类
AVSpeechSynthesizer* voiceSynthesizer;//合成器
BOOL speakFinished;//当前是否完成speech
NSString *speakingString;//正在合成器中的文本
}
-(void)speak:(NSString *)uterString;
-(BOOL)speakFinish:(NSString *)uterString;
-(void)stop;
-(void)pause;
-(void)continueSpeak;
//源文件代码
-(id)init{
if(self=[superinit]){
NSLog(@"\n----------Text2Speech init OK----------\n");
self.rate=1.0;
self.pitch =1.0;
self.timeInterval=300;
self.text=[NSStringalloc];
self.text=@"";
self.speakFinished=false;
self.speakingString=@"";
self.voiceSynthesizer=[[AVSpeechSynthesizeralloc]init];
self.voiceSynthesizer.delegate=self;
logFlag=NO;
}
returnself;
}
-(void)speak:(NSString *)uterString{
@autoreleasepool
{
AVSpeechUtterance* utterance=[[AVSpeechUtterancealloc]initWithString:uterString];
utterance.rate=self.rate;
utterance.pitchMultiplier=self.pitch;
utterance.postUtteranceDelay=0;
speakFinished=false;
switch(self.voice){
caseEN_UK:
utterance.voice=[AVSpeechSynthesisVoicevoiceWithLanguage:@"en-gb"];
break;
caseEN_USA:
utterance.voice=[AVSpeechSynthesisVoicevoiceWithLanguage:@"en-us"];
break;
caseEN_CA:
utterance.voice=[AVSpeechSynthesisVoicevoiceWithLanguage:@"en-ca"];
break;
caseEN_AU:
utterance.voice=[AVSpeechSynthesisVoicevoiceWithLanguage:@"en-au"];
break;
}
speakingString=@"";
[self.voiceSynthesizerspeakUtterance:utterance];
}
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer
didFinishSpeechUtterance:(AVSpeechUtterance *)utterance{
printTestMsg(@"speechSynthesizer",@"--finish speaking--",logFlag);
speakFinished=true;
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer
willSpeakRangeOfSpeechString:(NSRange)characterRange
utterance:(AVSpeechUtterance *)utterance{
NSString *speechSting=utterance.speechString;
NSString *subString=[speechStingsubstringWithRange:characterRange];
self.speakingString=subString;
printTestMsg(@"speechSynthesizer",@"-------the current speaking string---",NO);
}
-(BOOL)speakFinish:(NSString *)uterString{
//need add code
returnspeakFinished;
}
-(void)stop{
[self.voiceSynthesizerstopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
-(void)pause{
[self.voiceSynthesizerpauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
-(void)continueSpeak{
[self.voiceSynthesizercontinueSpeaking];
}
@end
/*
BOOL speechPaused = 0;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.synthesizer = [[AVSpeechSynthesizer alloc] init];
speechPaused = NO;
self.synthesizer.delegate = self;
}
- (IBAction)playPauseButtonPressed:(UIButton *)sender {
[self.textToSpeak resignFirstResponder];
if (speechPaused == NO) {
[self.playPauseButton setTitle:@"Pause" forState:UIControlStateNormal];
[self.synthesizer continueSpeaking];
speechPaused = YES;
} else {
[self.playPauseButton setTitle:@"Play" forState:UIControlStateNormal];
speechPaused = NO;
[self.synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
if (self.synthesizer.speaking == NO) {
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:self.textToSpeak.text];
//utterance.rate = AVSpeechUtteranceMinimumSpeechRate;
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-au"];
[self.synthesizer speakUtterance:utterance];
}
}
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
[self.playPauseButton setTitle:@"Play" forState:UIControlStateNormal];
speechPaused = NO;
NSLog(@"Playback finished");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
*/
Text To Speech所支持的语言类型,请参考
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