在 Swift 中强制对 AVSpeech 和 AVSpeechUtterance 中的单词进行自定义发音

Force custom pronunciation of words in AVSpeech and AVSpeechUtterance in Swift

为了使用 AVSpeechUtterance 说出数字,我希望 Siri 以符合数字类型约定的方式说出数字。

对于日期,我希望它把 1492 读成十四九十二,而不是一千、四百、九十二。

对于 phone 号码 650-412-3456,我想说六五哦,四一二三四五六而不是六百五十四百十二三千四百五十六.

有没有用AVSpeech和AVUtterance指定发音的? the docs.

似乎没有什么明显的

虽然不是 AV 设置,但为说话者解析短语会得到预期的结果。

例如,使用下面的扩展名:

let number = "1492"
let phrase = number.separate(every: 2, with: " ")
print(phrase) // 14 92

而对于 phone:

let phone   = "650-412-3456"
let parts = phone.components(separatedBy: CharacterSet.decimalDigits.inverted)
var phrase2 = String()

for part in parts {
  if Int(part) != nil {
    phrase2.append(String(describing: part).separate(every: 1, with: " ") + ",")
  }
} 

print(phrase2) // 6 5 0,4 1 2,3 4 5 6,

添加逗号是为了让语音合成器更自然地阅读,但可以省略。

extension String {
  func separate(every: Int, with separator: String) -> String {
    return String(stride(from: 0, to: Array(self).count, by: every).map {
       Array(Array(self)[[=12=]..<min([=12=] + every, Array(self).count)])
       }.joined(separator: separator))
  }
}

Is there anyway to specify pronunciation using AVSpeech and AVUtterance? There does not seem to be anything obvious in the docs.

实现目的的最佳方法是提供 appropriate format 供要读出的文本。

下面的代码片段用几个例子突出了这个断言:

    class ViewController: UIViewController {

        let synthesizer = AVSpeechSynthesizer()

        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)

            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "dd/MM/yyyy HH:mm"

            let date = dateFormatter.date(from: "01/04/2015 05:30")

            let dateStr = DateFormatter.localizedString(from: date!,
                                                        dateStyle: .medium,
                                                        timeStyle: .short)
            let dateUtterance = AVSpeechUtterance(string: dateStr)
            dateUtterance.voice = AVSpeechSynthesisVoice(language: "en-GB")

            let hourComponents = Calendar.current.dateComponents([.hour, .minute],
                                                                 from: date!)
            let hourStr = DateComponentsFormatter.localizedString(from: hourComponents,
                                                                  unitsStyle: .spellOut)
            let hourUtterance = AVSpeechUtterance(string: hourStr!)
            hourUtterance.voice = AVSpeechSynthesisVoice(language: "en-GB")

            let numberValue = NSNumber(value: 54038921.7)
            let nbStr = NumberFormatter.localizedString(from: numberValue,
                                                        number: .decimal)
            let nbUtterance = AVSpeechUtterance(string: nbStr)
            nbUtterance.voice = AVSpeechSynthesisVoice(language: "en-GB")

            synthesizer.speak(hourUtterance) //Only time
            synthesizer.speak(dateUtterance) //The complete date and hour
            synthesizer.speak(nbUtterance) //Just for numbers
        }
    }

更多 info with code snippets (Objc 和 Swift) 如果此示例还不够,可用。