【问题标题】:Why is my AVFoundation Speech not working?为什么我的 AVFoundation Speech 不起作用?
【发布时间】:2021-01-16 01:52:05
【问题描述】:

大家好,这是我的代码:

func configureSpeechButton() {
        contentView.addSubview(speechButton)
        speechButton.setImage(speechImage, for: .normal)
        speechButton.addTarget(self, action: #selector(speechButtontapped), for: .touchUpInside)
        
        //Constraints
        speechButton.translatesAutoresizingMaskIntoConstraints = false
        speechButton.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 40).isActive = true
        speechButton.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20).isActive = true
        speechButton.widthAnchor.constraint(equalToConstant: 50).isActive = true
        speechButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
        
    }
    
    @objc func speechButtontapped() {
        
        let speech = AVSpeechUtterance(string: "\(testSpeech)")
        speech.voice = AVSpeechSynthesisVoice(language: "de")
        
        let synth = AVSpeechSynthesizer()
        synth.speak(speech)
        
    }
    
    let testSpeech = "This is a test"

我将按钮放在滚动视图的内容视图上,谁能告诉我问题出在哪里?提前致谢

【问题讨论】:

  • 您确定“de”语言代码是有效值吗?

标签: swift avfoundation siri


【解决方案1】:

您的AVSpeechSynthesizer 超出范围。将其分配给成员变量以延长其生命周期。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 2018-03-05
    • 2013-04-19
    • 2015-10-17
    • 2016-02-20
    • 2023-04-03
    • 2012-01-07
    相关资源
    最近更新 更多