从macOS 10.13.2开始,以下简单的Cocoa app应用程序会在主线程上导致死锁(我们永远不会看到NSAlert):
AppDelegate.swift:
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
let text = "Lorem ipsum dolor sit amet consectetur adipiscing elit, phasellus elementum lobortis quisque aptent leo a, rutrum dictum velit nostra viverra mollis. Nam pharetra feugiat sollicitudin quisque consequat suspendisse nisi blandit purus nascetur proin mi, fermentum magna felis et aenean condimentum ultrices justo vulputate etiam fusce, turpis id netus tristique porta libero hendrerit scelerisque semper fringilla placerat. Accumsan nisl felis nascetur lorem vulputate metus mauris aliquet purus, rutrum non viverra dictumst mi commodo mollis risus, sociis ullamcorper integer tincidunt donec himenaeos porta vestibulum. Senectus torquent per odio viverra mauris dui lacus nisi ligula porta pretium ornare purus amet, ad cursus suspendisse dis porttitor nec enim class nunc leo ante cras tempor. Mollis posuere cum gravida a primis et donec mauris, sagittis sit duis nulla hac penatibus imperdiet amet montes, libero mattis blandit urna orci fringilla vestibulum. Vehicula porta tempor augue fringilla fames pellentesque feugiat sagittis, quis lobortis lectus phasellus class sapien vulputate senectus, volutpat duis erat tellus fusce ultricies morbi. Id cubilia malesuada consectetur quisque tempus mattis a, vulputate aliquet elementum non laoreet penatibus auctor, hac ad neque pharetra magnis amet."
var synth: NSSpeechSynthesizer?
func applicationDidFinishLaunching(_ aNotification: Notification) {
let url = URL(fileURLWithPath: "\(NSTemporaryDirectory())/\(UUID().uuidString).aiff")
synth = NSSpeechSynthesizer()
synth?.startSpeaking(text, to: url)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
self.synth?.stopSpeaking()
let alert = NSAlert()
alert.messageText = "After 'stopSpeaking'"
alert.runModal()
}
}
}
据我所知,这个问题在macOS 10.13.2之前不存在。有很多方法可以让语音合成器陷入僵局;上面的代码是最简单的再现案例。
我提交了雷达(36138159)(https://openradar.appspot.com/radar?id=4990843182120960),但我很想知道是否有其他人有幸解决这个问题。
补充说明:一旦我遇到这种僵局,我必须杀死延迟的com.apple.speech.speechsynthesisd
进程,否则任何我试图用NSSpeechSynthesizer导致死锁。