gpt4 book ai didi

swift - 为什么 NSSpeechSynthesizer 不调用它的委托(delegate)方法?

转载 作者:行者123 更新时间:2023-11-28 15:11:15 25 4
gpt4 key购买 nike

当我在 NSSpeechSynthesizer 实例上调用 startSpeaking(String) 方法时,设置了一个委托(delegate),会说话,但以下委托(delegate)方法永远不会触发:

speechSynthesizer(_ sender: NSSpeechSynthesizer, 
willSpeakWord characterRange: NSRange,
of string: String)

func speechSynthesizer(_ sender: NSSpeechSynthesizer,
didFinishSpeaking finishedSpeaking: Bool)

为什么?我正在使用 Swift 4; X代码9.1; MacOS 10.13.1

重现步骤:

编译并运行以下命令:

import AppKit

class SynthDelegate: NSObject, NSSpeechSynthesizerDelegate {

var str = ""

func speechSynthesizer(_ sender: NSSpeechSynthesizer, willSpeakWord characterRange: NSRange, of string: String) {
str = "spoke a word"
}

func speechSynthesizer(_ sender: NSSpeechSynthesizer, didFinishSpeaking finishedSpeaking: Bool) {
str = "finished speaking"
}

}

let mySpeaker = NSSpeechSynthesizer()

let myDelegate = SynthDelegate()

mySpeaker.delegate = myDelegate

mySpeaker.startSpeaking("test string to read aloud")

sleep(5) // keep alive while speaking

print(myDelegate.str)

预期结果:

“说完”

实际结果:

“”

更新

基于 OOPers 的优秀答案,以下在新测试项目中工作:

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {



func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application

let mySpeaker = NSSpeechSynthesizer()
let myDelegate = SynthDelegate()
mySpeaker.delegate = myDelegate
mySpeaker.startSpeaking("test string to read aloud")


Timer.scheduledTimer(withTimeInterval: 5.0, repeats: false) {_ in
print(myDelegate.str)

}
}

func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}


}


class SynthDelegate: NSObject, NSSpeechSynthesizerDelegate {
var str = ""

func speechSynthesizer(_ sender: NSSpeechSynthesizer, willSpeakWord characterRange: NSRange, of string: String) {
str = "spoke a word"
print(str)
}

func speechSynthesizer(_ sender: NSSpeechSynthesizer, didFinishSpeaking finishedSpeaking: Bool) {
str = "finished speaking"
print(str)
}
}

最佳答案

您是否在 Playground 中测试您的代码?无论如何,你永远不应该在你的应用程序中调用 sleep(),包括 Playground 代码。

它阻止了使框架正常工作所需的许多事件,例如调用委托(delegate)方法。当您需要在延迟后执行某些代码时,可以使用 Timer

可在 Playground 中测试:

import AppKit

class SynthDelegate: NSObject, NSSpeechSynthesizerDelegate {
var str = ""

func speechSynthesizer(_ sender: NSSpeechSynthesizer, willSpeakWord characterRange: NSRange, of string: String) {
str = "spoke a word"
print(str)
}

func speechSynthesizer(_ sender: NSSpeechSynthesizer, didFinishSpeaking finishedSpeaking: Bool) {
str = "finished speaking"
print(str)
}
}

let mySpeaker = NSSpeechSynthesizer()
let myDelegate = SynthDelegate()
mySpeaker.delegate = myDelegate
mySpeaker.startSpeaking("test string to read aloud")

import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true

Timer.scheduledTimer(withTimeInterval: 5.0, repeats: false) {_ in
print(myDelegate.str)
PlaygroundPage.current.finishExecution()
}

关于swift - 为什么 NSSpeechSynthesizer 不调用它的委托(delegate)方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47605138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com