iOS - AVSpeechSynthesizer 暂停和继续说话问题

问题描述 投票:0回答:2

macOS:Mojave 10.14.4 测试版

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();
}

我如何从离开的地方继续?我还缺少什么吗?

ios swift swift4 text-to-speech speech-synthesis
2个回答
1
投票

我实现了以下代码来检查您的问题(在

Mojave 10.14.4
iOS 12.2
Xcode 10.2.1
swift 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
处暂停时,它也总是从暂停的地方恢复

我不知道你的问题来自哪里,但是通过上面提到的配置和这个代码片段,语音合成器从暂停的地方恢复


0
投票

我认为逻辑是这样的,因为“isSpeaking”包括说话和暂停状态,如下文所述。因此,需要额外检查。

if synthesizer.isSpeaking {
    if synthesizer.isPaused {
        synthesizer.continueSpeaking()
    } else {
        synthesizer.pauseSpeaking(at: .word)
    }
} else {
    speak(text)
}
© www.soinside.com 2019 - 2024. All rights reserved.