文字转语音

目标 C.

AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc]init];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"Some text"];
[utterance setRate:0.2f];
[synthesizer speakUtterance:utterance];

迅速

let synthesizer = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance(string: "Some text")
utterance.rate = 0.2

你也可以改变这样的声音:

utterance.voice = AVSpeechSynthesisVoice(language: "fr-FR")

然后说

  • 在 Swift 2:synthesizer.speakUtterance(utterance)
  • 在 Swift 3:synthesizer.speak(utterance)

不要忘记导入 AVFoundation

有用的方法

你可以使用以下两种方法停止或暂停所有语音:

- (BOOL)pauseSpeakingAtBoundary:(AVSpeechBoundary)boundary;
- (BOOL)stopSpeakingAtBoundary:(AVSpeechBoundary)boundary;

AVSpeechBoundary 指示语音是应该暂停还是立即停止(AVSpeechBoundaryImmediate),或者它应该在当前被说出的单词之后暂停或停止(AVSpeechBoundaryWord)。