使用speakUtterance:的方法AVSpeechSynthesizer将文本转换为语音。您需要将一个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)