很简单 直接上代码
需要导入的框架
storyboard上加个button,添加到viewController.m中
viewController.mz中代码如下:
#import "ViewController.h"
#import <AVFoundation/AVSpeechSynthesis.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)handleButtonAction:(id)sender {
//创建一个声音合成器
AVSpeechSynthesizer *av = [[AVSpeechSynthesizer alloc]init];
//需要转换的文本
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:@"malegbi"];
//英式发音
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-GB"];
utterance.voice = voice;
[av speakUtterance:utterance];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
完成