#import "ViewController.h"
#import <AVFoundation/AVFoundation.h> //引入AVFoundation框架
@interface ViewController ()
@property(nonatomic,strong)AVSpeechSynthesizer *speechSynthsizer;//定义语音合成器
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
}
#pragma mark - 语音按钮事件
- (IBAction)speakBtnDidClicked:(UIButton *)sender {
//指定要合成语音的文字
AVSpeechUtterance *speechUtterance = [[AVSpeechUtterancealloc]initWithString:@"Hello 你好 my friend"];
//根据BCP-47标准,指定一个语言,各种语言请参考http://www.rfc-editor.org/rfc/rfc4646.txt
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoicevoiceWithLanguage:@"zh-Hans-CN"];
speechUtterance.voice = voice;
//发音
[self.speechSynthsizerspeakUtterance:speechUtterance];
}
#pragma mark - 懒加载
- (AVSpeechSynthesizer *) speechSynthsizer{
if (!_speechSynthsizer) {
_speechSynthsizer = [[AVSpeechSynthesizeralloc]init];
}
return_speechSynthsizer;
}
@end