import AVFoundation
//播放器
let player = AVSpeechSynthesizer();
player.delegate = self;
let u = AVSpeechUtterance(string: "今天天气不错");
u.voice = AVSpeechSynthesisVoice(language: "zh-CN");
u.volume = 1.0;//音量 [0-1] Default = 1
u.rate = 0.48;//播放速度
u.pitchMultiplier = 1.0;//播放基准音调 [0.5 - 2] Default = 1
player.speakUtterance(u);
在
iPhone6
以下rate
设置为0.1
否则会读的很快 iOS的坑啊
//开始播放
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("播放完毕")
}
//停止播放
player.stopSpeakingAtBoundary(AVSpeechBoundary.Immediate);
//暂停播放
player.pauseSpeakingAtBoundary(AVSpeechBoundary.Immediate);
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有