欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS编程Text to Speech 及支持语言

程序员文章站 2022-08-12 22:10:33
iOS编程中Text to Speech的基本框架,需要包含头文件: #import 基本编程框架: 头文件定义 @interface Text2Speech : NSO...
iOS编程中Text to Speech的基本框架,需要包含头文件:

#import

基本编程框架:

头文件定义

@interface Text2Speech : NSObject

{

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=[super init]){

NSLog(@"\n----------Text2Speech init OK----------\n");

self.rate= 1.0;

self.pitch =1.0;

self.timeInterval=300;

self.text=[NSString alloc];

self.text=@"";

self.speakFinished=false;

self.speakingString=@"";

self.voiceSynthesizer=[[AVSpeechSynthesizer alloc]init];

self.voiceSynthesizer.delegate=self;

logFlag=NO;

}

return self;

}

-(void)speak:(NSString *)uterString{

@autoreleasepool

{

AVSpeechUtterance* utterance=[[AVSpeechUtterance alloc]initWithString:uterString];

utterance.rate=self.rate;

utterance.pitchMultiplier=self.pitch;

utterance.postUtteranceDelay=0;

speakFinished=false;

switch(self.voice){

case EN_UK:

utterance.voice=[AVSpeechSynthesisVoice voiceWithLanguage:@"en-gb"];

break;

case EN_USA:

utterance.voice=[AVSpeechSynthesisVoice voiceWithLanguage:@"en-us"];

break;

case EN_CA:

utterance.voice=[AVSpeechSynthesisVoice voiceWithLanguage:@"en-ca"];

break;

case EN_AU:

utterance.voice=[AVSpeechSynthesisVoice voiceWithLanguage:@"en-au"];

break;

}

speakingString=@"";

[self.voiceSynthesizer speakUtterance: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=[speechSting substringWithRange:characterRange];

self.speakingString=subString;

printTestMsg(@"speechSynthesizer", @"-------the current speaking string---", NO);

}

-(BOOL)speakFinish:(NSString *)uterString{

//need add code

return speakFinished;

}

-(void)stop{

[self.voiceSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];

}

-(void)pause{

[self.voiceSynthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];

}

-(void)continueSpeak{

[self.voiceSynthesizer continueSpeaking];

}

@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 (* SAR China) - zh-HK
Chinese (*) - 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
iOS编程Text to Speech 及支持语言