创建基本的文本到语音

使用 AVSpeechSynthesizerspeakUtterance:方法将文本转换为语音。你需要将 AVSpeechUtterance 对象传递给此方法,该方法包含你要说出的文本。

目标 C.

AVSpeechSynthesizer *speaker = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *speech    = [AVSpeechUtterance speechUtteranceWithString:@"Hello World"];
[speaker speakUtterance:speech];

迅速

let speaker = AVSpeechSynthesizer()
let speech = AVSpeechUtterance(string: "Hello World")
speaker.speakUtterance(speech)