iOS-AVSpeechSynthesizer暂停并继续

时间:2019-02-04 11:05:20

标签: ios swift swift4 text-to-speech speech-synthesis

  

macOS:Mojave 10.14.4 Beta

     

iOS:12.2试用版

     

Xcode:10.2测试版

我使用的是AVSpeechSynthesizer,但是下面的代码无法从暂停位置恢复。

// The pause functionality works fine
if (synth.isSpeaking) {
   synth.pauseSpeaking(at: AVSpeechBoundary.word)
}


// But continueSpeaking always starting from the beginning.
if (synth.isPaused) {
   synth.continueSpeaking();
}

如何从离开的地方继续?我想念什么吗?

1 个答案:

答案 0 :(得分:1)

我实现了以下代码来检查您的问题Mojave 10.14.4iOS 12.2Xcode 10.2.1swift 5.0下的测试)

>
class SpeechSynthesis: UIViewController {

    var synthesizer = AVSpeechSynthesizer()
    var playQueue = [AVSpeechUtterance]()

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

        for i in 1...10 {
            let stringNb = "number " + String(i) + " of the speech synthesizer."
            let utterance = AVSpeechUtterance(string: stringNb)
            playQueue.append(utterance)
        }

        for utterance in playQueue {
            synthesizer.speak(utterance)
        }
    }

    @IBAction func pauseButton(_ sender: UIButton) {

        if (synthesizer.isSpeaking == true) {
            if (synthesizer.pauseSpeaking(at: .immediate) == true) {
                print("PAUSE")
            } else {
                print("P.R.O.B.L.E.M. when pausing.")
            }
        }
    }

    @IBAction func resumeButton(_ sender: UIButton) {

        if (synthesizer.isPaused == true) {
            if (synthesizer.continueSpeaking() == true) {
                print("CONTINUE")
            } else {
                print("P.R.O.B.L.E.M. when resuming.")
            }
        }
    }
}

我注意到边界.word的另一个问题在触发时并不总是暂停,但是当此约束更改为.immediate 时,一切都从暂停位置恢复

但是,当它很少在边界.word处暂停时,它也always resumes从其暂停处开始。

我不知道您的问题来自哪里,但是有了上述配置和此代码段,语音合成器从暂停的位置恢复