swift - 为什么我的 AVFoundation Speech 不起作用?
问题描述
嘿伙计们,这是我的代码:
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"
我将按钮放在滚动视图的内容视图上,有人能告诉我问题出在哪里吗?提前致谢
解决方案
你AVSpeechSynthesizer
的超出范围。将其分配给成员变量以延长其生命周期。
推荐阅读
- python-3.x - Python ZMQ Publisher 连接到 GNUradio SUB 消息源块问题
- python - 从 .pcd 读取点云到 ROS PointCloud2
- django - 如何在 Django ListView 中列出模型字段的子字段?
- python - Kivy 问题 - 无法获取窗口,中止
- django - Google Analytics Measurement Protocol 无法跟踪购物车添加和产品视图
- xcode - 静态方法“buildBlock”需要“ToolbarItem”
'符合'视图' - python - 用于在 Windows 上打开的 Linux 文本文件编码
- javascript - Fullcalendar:可拖动的样式搞砸了
- javascript - ajax 帖子列表仅更新第一个帖子
- c++ - vcpkg:如何提交包更新请求,或查看包是否应更新?