iOS---文字转语音

 1.导入导入 AVFoundation框架

 2.导入头文件#import


 AVSpeechSynthesizerDelegate 实现文字转语音需要代理方法 用来控制声音



 IOS7包含了一组可以用来合成的声音的嗓音,你可以自定义多种嗓音来合成。

 "[AVSpeechSynthesisVoice 0x978a0b0] Language: th-TH",


 "[AVSpeechSynthesisVoice 0x977a450] Language: pt-BR",


 "[AVSpeechSynthesisVoice 0x977a480] Language: sk-SK",


 "[AVSpeechSynthesisVoice 0x978ad50] Language: fr-CA",


 "[AVSpeechSynthesisVoice 0x978ada0] Language: ro-RO",


 "[AVSpeechSynthesisVoice 0x97823f0] Language: no-NO",


 "[AVSpeechSynthesisVoice 0x978e7b0] Language: fi-FI",


 "[AVSpeechSynthesisVoice 0x978af50] Language: pl-PL",


 "[AVSpeechSynthesisVoice 0x978afa0] Language: de-DE",


 "[AVSpeechSynthesisVoice 0x978e390] Language: nl-NL",


 "[AVSpeechSynthesisVoice 0x978b030] Language: id-ID",


 "[AVSpeechSynthesisVoice 0x978b080] Language: tr-TR",


 "[AVSpeechSynthesisVoice 0x978b0d0] Language: it-IT",


 "[AVSpeechSynthesisVoice 0x978b120] Language: pt-PT",


 "[AVSpeechSynthesisVoice 0x978b170] Language: fr-FR",


 "[AVSpeechSynthesisVoice 0x978b1c0] Language: ru-RU",


 "[AVSpeechSynthesisVoice 0x978b210]Language: es-MX",


 "[AVSpeechSynthesisVoice 0x978b2d0] Language: zh-HK",中文(香港) 粤语


 "[AVSpeechSynthesisVoice 0x978b320] Language: sv-SE",


 "[AVSpeechSynthesisVoice 0x978b010] Language: hu-HU",


 "[AVSpeechSynthesisVoice 0x978b440] Language: zh-TW", 中文(台湾)


 "[AVSpeechSynthesisVoice 0x978b490] Language: es-ES",


 "[AVSpeechSynthesisVoice 0x978b4e0] Language: zh-CN",汉语


 "[AVSpeechSynthesisVoice 0x978b530] Language: nl-BE",


 "[AVSpeechSynthesisVoice 0x978b580] Language: en-GB", 英语(英国)


 "[AVSpeechSynthesisVoice 0x978b5d0] Language: ar-SA",


 "[AVSpeechSynthesisVoice 0x978b620] Language: ko-KR",


 "[AVSpeechSynthesisVoice 0x978b670] Language: cs-CZ",


 "[AVSpeechSynthesisVoice 0x978b6c0] Language: en-ZA",


 "[AVSpeechSynthesisVoice 0x978aed0] Language: en-AU",


 "[AVSpeechSynthesisVoice 0x978af20] Language: da-DK",


 "[AVSpeechSynthesisVoice 0x978b810] Language: en-US",


 "[AVSpeechSynthesisVoice 0x978b860] Language: en-IE",


 "[AVSpeechSynthesisVoice 0x978b8b0] Language: hi-IN",


 "[AVSpeechSynthesisVoice 0x978b900] Language: el-GR",


 "[AVSpeechSynthesisVoice 0x978b950] Language: ja-JP"


 进入iPhone的 设置 > 通用 > 辅助功能 > 语音,开启“朗读所选项”,并在“嗓音”中选择“中文(台湾)”

代码实现如下


#import "ViewController.h"

#import

@interface ViewController ()

@property (nonatomic,strong)UIButton *swicthBut;//按钮

@property (nonatomic,strong)UITextField *textField;//文字输入框

@property(nonatomic,strong)AVSpeechSynthesizer*speechSynthesizer;//实现文本到语音

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    self.title=@"文字转语音";

    self.view.backgroundColor = [UIColor whiteColor];

    [self.viewaddSubview:self.swicthBut];

    [self.viewaddSubview:self.textField];

}

#pragma mark------开始文本转语音

- (void)textIntoSpeechSynthesizer{

    if(self.textField.text.length>0) {

        AVSpeechUtterance *speechUtterance = [[AVSpeechUtterance alloc]initWithString:self.textField.text];

        speechUtterance.voice= [AVSpeechSynthesisVoicevoiceWithLanguage:@"zh-CN"];//国家语言

        speechUtterance.rate=0.4f;//声速

        speechUtterance.pitchMultiplier=0.8;// 音调调节[0.5 - 2] Default = 1

        speechUtterance.volume=0.3;//音量调节  调节范围 :[0-1]  默认是 :1

        [self.speechSynthesizerspeakUtterance:speechUtterance];//开始播放

    }else{

        NSLog(@"请输入文字");


    }

}

#pragma mark---实现文本到语音

-(AVSpeechSynthesizer*)speechSynthesizer{

    if (!_speechSynthesizer) {

        _speechSynthesizer = [[AVSpeechSynthesizer alloc]init];

        _speechSynthesizer.delegate = self;//设置代理控制声音的暂停/继续

    }

    return _speechSynthesizer;

}

#pragma mark---文字输入

- (UITextField*)textField{


    if (!_textField) {

        _textField= [[UITextFieldalloc]init];

        _textField.frame = CGRectMake(0, 140, [UIScreen mainScreen].bounds.size.width, 100);

        _textField.placeholder=@"请输入文字";

        _textField.textColor = [UIColor blackColor];


    }

    return _textField;

}

#pragma mark----开关

- (UIButton*)swicthBut{


    if (!_swicthBut) {

        _swicthBut= [[UIButtonalloc]init];

        _swicthBut.frame=CGRectMake(50,100,80,30);

        _swicthBut.titleLabel.textAlignment = NSTextAlignmentCenter;

        [_swicthBut setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];

        [_swicthBut addTarget:self action:@selector(switchOn:) forControlEvents:UIControlEventTouchUpInside];

        [_swicthBut setTitle:@"开始" forState:UIControlStateNormal];

    }

    return _swicthBut;

}

- (void)switchOn:(UIButton*)sender{


    sender.selected= !sender.selected;

    if(!(self.textField.text.length>0)) {

        NSLog(@"请输入文字");

    }

   else  if(sender.isSelected) {

        [self textIntoSpeechSynthesizer];

        [_swicthBut setTitle:@"暂停" forState:UIControlStateNormal];

    }else{

        [self.speechSynthesizer continueSpeaking];//停止播放

        //[self.speechSynthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryWord];//暂停

        [_swicthBut setTitle:@"开始" forState:UIControlStateNormal];


    }

}

#pragma mark------AVSpeechSynthesizerDelegate

//开始

- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didStartSpeechUtterance:(AVSpeechUtterance*)utterance{


}

//完成

- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance*)utterance{


}

//暂停

- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance*)utterance{


}

//恢复

- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance*)utterance{


}

//取消

- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance*)utterance{


}

//将要说的某段话

-(void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance*)utterance{


}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

你可能感兴趣的:(iOS---文字转语音)