【问题标题】:Force custom pronunciation of words in AVSpeech and AVSpeechUtterance in Swift在 Swift 中强制自定义 AVSpeech 和 AVSpeechUtterance 中的单词发音
【发布时间】:2019-05-24 18:02:27
【问题描述】:

对于使用 AVSpeechUtterance 说出数字,我希望 Siri 以尊重数字类型约定的方式说出数字。

对于日期,我希望将 1492 发音为 1492,而不是 1000、400、92。

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

有没有使用 AVSpeech 和 AVUtterance 来指定发音? the docs 中似乎没有任何明显的内容。

【问题讨论】:

    标签: ios swift avspeechsynthesizer avspeechutterance


    【解决方案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))
      }
    }
    

    【讨论】:

      【解决方案2】:

      有没有使用 AVSpeech 和 AVUtterance 来指定发音?文档中似乎没有任何明显的内容。

      达到目的的最佳方式是提供appropriate format 以便读出文本。

      下面的代码 sn-p 用几个例子突出了这个断言:

          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) 如果这个例子还不够的话。

      【讨论】:

      • 您链接的网站看起来非常好,超出了 Apple 的标准。明确一点,不需要额外的库,也不需要调用辅助方法?
      • @user6631314: 所呈现的都是 iOS 原生的,无需添加任何内容,而且您绝对不需要调用可访问性方法,如我的代码示例所示。
      • 非常酷的方法,我投了赞成票。我接受了其他稍微简单的方法,但您的方法非常优雅。希望我能同时接受。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-18
      • 1970-01-01
      相关资源
      最近更新 更多