添加引用
播放语音
1 2 3 4 5 6 7 8 9
| let player = AVSpeechSynthesizer(); player.delegate = self; let u = AVSpeechUtterance(string: "今天天气不错"); u.voice = AVSpeechSynthesisVoice(language: "zh-CN"); u.volume = 1.0; u.rate = 0.48; u.pitchMultiplier = 1.0; player.speakUtterance(u);
|
在iPhone6以下rate设置为0.1否则会读的很快 iOS的坑啊
代理方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| func speechSynthesizer(synthesizer: AVSpeechSynthesizer, didStartSpeechUtterance utterance: AVSpeechUtterance) { print("开始播放") }
func speechSynthesizer(synthesizer: AVSpeechSynthesizer, willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance) { let process = Double(characterRange.location) / Double(utterance.speechString.characters.count); print("播放中,播放进度为:\(process)") }
func speechSynthesizer(synthesizer: AVSpeechSynthesizer, didFinishSpeechUtterance utterance: AVSpeechUtterance) { print("播放完毕") }
|
常用方法
1 2 3 4
| player.stopSpeakingAtBoundary(AVSpeechBoundary.Immediate);
player.pauseSpeakingAtBoundary(AVSpeechBoundary.Immediate);
|