要使用AVSpeechUtterance来讲数字,我希望Siri以尊重数字类型惯例的方式讲数字。
对于日期,我想将1492发音为十四点九十二点,而不是一千零四百点九十二点。
对于电话号码650-412-3456,我想说的是六点五分,四一二三四五六,而不是六百五十四百一百二十三三分,一千四百六十六。 / p>
是否有使用AVSpeech和AVUtterance指定发音的方法? the docs中似乎没有什么明显的东西。
答案 0 :(得分:1)
虽然不是AV设置,但解析说话者的短语将获得所需的结果。
例如,使用以下扩展名:
let number = "1492"
let phrase = number.separate(every: 2, with: " ")
print(phrase) // 14 92
电话:
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,
添加了逗号,语音合成器可以使阅读更加自然,但是可以省略它们。
String extension from Joe Maher:
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)[$0..<min($0 + every, Array(self).count)])
}.joined(separator: separator))
}
}
答案 1 :(得分:1)
是否有使用AVSpeech和AVUtterance指定发音的方法?在文档中似乎没有什么明显的东西。
达到目的的最好方法是提供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)(如果此示例不够用)。