使用苹果提供的语音合成器,可以通过简单的代码做到朗读文本。
#import <AVFoundation/AVFoundation.h>
// 创建嗓音,指定嗓音不存在则返回nil
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
// 创建语音合成器
AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init];
// 实例化发声的对象
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"跑步完成"];
utterance.voice = voice;
utterance.rate = 0.5;<span style="white-space:pre"> </span>// 语速
// 朗读的内容
[synthesizer speakUtterance:utterance];
所有的嗓音如下:
"[AVSpeechSynthesisVoice 0x978a0b0] Language: th-TH",
&#