iOS 语音合成TTS AVSpeechSynthesizer

632 阅读1分钟

“我报名参加金石计划1期挑战——瓜分10万奖池,这是我的第n篇文章,点击查看活动详情

AVSpeechSynthesis 是一个 可以在你的 iOS 设备上 生成计算机合成语音的 API 它有很多用途 例如 你可能希望 在你的 App 中发布公告 你可能正在创建 一个不可见的界面 或者你可能正在开发 一个教育 App 合成语音可以为学习材料 起到很好的巩固作用

import AVFoundation

let synth = AVSpeechSynthesizer()

synth.delegate = self

let utterance = AVSpeechUtterance.init(string:"Hello world")

//utterance.voice = AVSpeechSynthesisVoice.init(language: "en-US")

let allVoices = AVSpeechSynthesisVoice.speechVoices() //获取多种发音

utterance.voice = AVSpeechSynthesisVoice(identifier: allVoices[17].identifier)

// Choose a rate between 0 and 1, 0.5 is the default rate
utterance.rate = 0.5 

// Choose a pitch multiplier between 0.5 and 2, 1 is the default multiplier

utterance.pitchMultiplier = 1

// Choose a volume between 0 and 1, 1 is the default value

utterance.volume = 0.8

//self.synth.usesApplicationAudioSession = false

//self.synth.mixToTelephonyUplink = false

synth.speak(utterance)

  






相关资料: WWDC18