from sys import getfilesystemencoding elif operating_system == 'Darwin': from AppKit import NSSound '/' + sound sound = 'file://' + sound url = NSURL.URLWithString_(sound) nssound = NSSound.alloc().initWithContentsOfURL_byReference_(url, True) if not nssound: raise IOError('Unable to load sound named: ' + sound) nssound.play() if block: sleep(nssound.duration()) def stop(self): raise NotImplemented class playsoundNix(playsoundBase
设置是否当鼠标移动到按钮上时显示边框 @property BOOL showsBorderOnlyWhileMouseInside; //设置按钮声音 @property (nullable, strong) NSSound
最后一段与 SwiftUI 无关的代码是创建 NSSound 实例。为了避免使示例过于复杂,笔者创建了几个全局变量: let bellSound: NSSound? let url = Bundle.main.url(forResource: "bell", withExtension: "mp3") else { return nil } return NSSound (contentsOf: url, byReference: true) }() let beatSound: NSSound? let url = Bundle.main.url(forResource: "beat", withExtension: "mp3") else { return nil } return NSSound