iOS9 AVSpeechUtterance rate for AVSpeechSynthesizer issue

后端 未结 2 1781
日久生厌
日久生厌 2021-02-06 05:03

The AVSpeechUtterance rate does not work the same for iOS 9 and prior versions of OS. Which is the change I have to make so that the sentence is spoken at the same speed. Are th

2条回答
  •  萌比男神i
    2021-02-06 05:59

    I've bumped into this issue a few times, too. Given the frequency of changes to iOS lately, I set a default speech rate based on the iOS version the user is running in AppDelegate.swift like so:

    // iOS speech synthesis is flakey between 8 and 9, so set default utterance rate based on iOS version
    if NSProcessInfo().isOperatingSystemAtLeastVersion(NSOperatingSystemVersion(majorVersion: 8, minorVersion: 0, patchVersion: 0)) {
        NSUserDefaults.standardUserDefaults().setFloat(0.15, forKey: "defaultSpeechRate")
        if NSProcessInfo().isOperatingSystemAtLeastVersion(NSOperatingSystemVersion(majorVersion: 9, minorVersion: 0, patchVersion: 0)) {
            NSUserDefaults.standardUserDefaults().setFloat(0.53, forKey: "defaultSpeechRate")
        }
    } else {
        NSUserDefaults.standardUserDefaults().setFloat(0.5, forKey: "defaultSpeechRate")
    }
    

    On my preferences scene, I added a slider to adjust the rate:

    @IBOutlet var speechRateSlider: UISlider!
    

    In viewDidLoad, I added the following:

        // Set speech rate
        speechRateSlider.value = NSUserDefaults.standardUserDefaults().floatForKey("defaultSpeechRate")
        speechRateSlider.maximumValue = 1.0
        speechRateSlider.minimumValue = 0.0
        speechRateSlider.continuous = true
        speechRateSlider.addTarget(self, action: "adjustSpeechRate:", forControlEvents: UIControlEvents.ValueChanged)
    

    I also tied an action to the UISlider:

    @IBAction func adjustSpeechRate(sender: AnyObject) {
    
        NSUserDefaults.standardUserDefaults().setFloat(speechRateSlider.value, forKey: "defaultSpeechRate")
        speechRateSlider.setValue(NSUserDefaults.standardUserDefaults().floatForKey("defaultSpeechRate"), animated: true)
        print(NSUserDefaults.standardUserDefaults().floatForKey("defaultSpeechRate"))
    }
    

    Given the unexpected behavior across devices and so many versions of iOS floating around, I opted to go this route rather than hard-code it into my app.

提交回复
热议问题