iOS编程Text to Speech 及支持语言

iOS编程中Text to Speech的基本框架,需要包含头文件:



#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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值