1
import AVFoundation
123456789
let player = AVSpeechSynthesizer();player.delegate = self;let u = AVSpeechUtterance(string: "今天天气不错");u.voice = AVSpeechSynthesisVoice(language: "zh-CN");u.volume = 1.0;//音量 [0-1] Default = 1u.rate = 0.48;//播放速度u.pitchMultiplier = 1.0;//播放基准音调 [0.5 - 2] Default = 1player.speakUtterance(u);
在iPhone6以下rate设置为0.1否则会读的很快 iOS的坑啊
iPhone6
rate
0.1
123456789101112大专栏 iOS语音合成>131415
//开始播放func (synthesizer: AVSpeechSynthesizer,didStartSpeechUtterance utterance: AVSpeechUtterance) { print("开始播放")}//播放中func (synthesizer: AVSpeechSynthesizer,willSpeakRangeOfSpeechString characterRange: NSRange,utterance: AVSpeechUtterance) { let process = Double(characterRange.location) / Double(utterance.speechString.characters.count); print("播放中,播放进度为:(process)")}//播放完毕func (synthesizer: AVSpeechSynthesizer,didFinishSpeechUtterance utterance: AVSpeechUtterance) { print("播放完毕")}
1234
//停止播放player.stopSpeakingAtBoundary(AVSpeechBoundary.Immediate);//暂停播放player.pauseSpeakingAtBoundary(AVSpeechBoundary.Immediate);