文字轉語音
目標 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
)。