AVSpeechSynthesizer isSpeaking 在 Swift 中不起作用

AVSpeechSynthesizer isSpeaking not working in Swift

所以在更新到 Xcode 12.0.1 之后,AVSpeechSynthesizer 现在可以在模拟器上工作了(它已经有一段时间没有为我工作了)。现在,无论合成器是否在说话,isSpeaking 变量始终为假。我想根据合成器是否在说话来触发我的观点的变化。下面是简单的版本,有什么想法吗?

import SwiftUI
import AVFoundation

struct ContentView: View {

var synthesizer = AVSpeechSynthesizer()
var utterance = AVSpeechUtterance(string: "Hello World")

var body: some View {
    VStack {
        Text(synthesizer.isSpeaking ? "Speaking" : "Not Speaking")
        Button(action: {synthesizer.speak(utterance)}) {
            Text("Speak To Me")
        }
    }
}
}

它只是没有更新你的情况,因为 synthesiser.isSpeaking 对于 SwiftUI 是不可观察的。您必须使用视图模型 class 和委托回调来处理此状态更改

这里是初始演示(你可以自己添加stop/pause/continue动作)

class SpeachViewModel: NSObject, ObservableObject, AVSpeechSynthesizerDelegate {
    @Published var isSpeaking = false

    private var synthesizer = AVSpeechSynthesizer()
    override init() {
        super.init()
        synthesizer.delegate = self
    }

    deinit {
        synthesizer.delegate = nil
    }

    func speak(_ utterance: AVSpeechUtterance) {
        self.synthesizer.speak(utterance)
    }

    // MARK: AVSpeechSynthesizerDelegate
    internal func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didStart utterance: AVSpeechUtterance) {
        self.isSpeaking = true
    }

    internal func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
        self.isSpeaking = false
    }

    internal func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didPause utterance: AVSpeechUtterance) {
        self.isSpeaking = false
    }

    internal func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didCancel utterance: AVSpeechUtterance) {
        self.isSpeaking = false
    }

    internal func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didContinue utterance: AVSpeechUtterance) {
        self.isSpeaking = true
    }
}

struct ContentView: View {

    @ObservedObject var vm = SpeachViewModel()
    var utterance = AVSpeechUtterance(string: "Hello World")

    var body: some View {
        VStack {
            Text(vm.isSpeaking ? "Speaking" : "Not Speaking")
            Button(action: {vm.speak(utterance)}) {
                Text("Speak To Me")
            }
        }
    }
}