【发布时间】:2016-07-18 16:27:04
【问题描述】:
我正在学习 Cocoa 编程。
我只需要在我自己的 Cocoa 项目中完成或失败异步任务时播放声音。
所以我想知道什么是最简单的方法。
虽然它应该很容易,但我在 Swift 中没有找到它。
在此先感谢
【问题讨论】:
我正在学习 Cocoa 编程。
我只需要在我自己的 Cocoa 项目中完成或失败异步任务时播放声音。
所以我想知道什么是最简单的方法。
虽然它应该很容易,但我在 Swift 中没有找到它。
在此先感谢
【问题讨论】:
可能,最简单的方法是使用NSSound。例如:
NSSound(named: "Purr")?.play()
来自 Apple 文档:
如果没有已知的带有 soundName 的 NSSound 对象,这个方法会尝试 通过在应用程序的主目录中搜索声音文件来创建一个 捆绑包(有关捆绑包内容的描述,请参见 NSBundle 被搜索)。如果应用程序主目录中没有声音文件 bundle,依次搜索以下目录:
~/库/声音
/图书馆/声音
/Network/Library/Sounds
/System/Library/Sounds
如果要播放系统提示音,请使用
NSBeep函数。
【讨论】:
NSSound(named: NSSound.Name(rawValue: "Purr"))?.play()。
NSSound(named: NSSound.Name("Purr"))?.play() EDIT kitsune 是正确的,但不需要 rawValue 描述符。
NSSound,第一个结果是the Apple documentation for it。在那里,你会看到它在 AppKit 中。
最简单?将其放入项目中的 Swift 文件中:
import AppKit
public extension NSSound {
static let basso = NSSound(named: .basso)
static let blow = NSSound(named: .blow)
static let bottle = NSSound(named: .bottle)
static let frog = NSSound(named: .frog)
static let funk = NSSound(named: .funk)
static let glass = NSSound(named: .glass)
static let hero = NSSound(named: .hero)
static let morse = NSSound(named: .morse)
static let ping = NSSound(named: .ping)
static let pop = NSSound(named: .pop)
static let purr = NSSound(named: .purr)
static let sosumi = NSSound(named: .sosumi)
static let submarine = NSSound(named: .submarine)
static let tink = NSSound(named: .tink)
}
public extension NSSound.Name {
static let basso = NSSound.Name("Basso")
static let blow = NSSound.Name("Blow")
static let bottle = NSSound.Name("Bottle")
static let frog = NSSound.Name("Frog")
static let funk = NSSound.Name("Funk")
static let glass = NSSound.Name("Glass")
static let hero = NSSound.Name("Hero")
static let morse = NSSound.Name("Morse")
static let ping = NSSound.Name("Ping")
static let pop = NSSound.Name("Pop")
static let purr = NSSound.Name("Purr")
static let sosumi = NSSound.Name("Sosumi")
static let submarine = NSSound.Name("Submarine")
static let tink = NSSound.Name("Tink")
}
import AppKit
public extension NSSound {
#if !swift(>=4)
private convenience init?(named name: Name) {
self.init(named: name as String)
}
#endif
public static let basso = NSSound(named: .basso)
public static let blow = NSSound(named: .blow)
public static let bottle = NSSound(named: .bottle)
public static let frog = NSSound(named: .frog)
public static let funk = NSSound(named: .funk)
public static let glass = NSSound(named: .glass)
public static let hero = NSSound(named: .hero)
public static let morse = NSSound(named: .morse)
public static let ping = NSSound(named: .ping)
public static let pop = NSSound(named: .pop)
public static let purr = NSSound(named: .purr)
public static let sosumi = NSSound(named: .sosumi)
public static let submarine = NSSound(named: .submarine)
public static let tink = NSSound(named: .tink)
}
public extension NSSound.Name {
#if !swift(>=4)
private convenience init(_ rawValue: String) {
self.init(string: rawValue)
}
#endif
public static let basso = NSSound.Name("Basso")
public static let blow = NSSound.Name("Blow")
public static let bottle = NSSound.Name("Bottle")
public static let frog = NSSound.Name("Frog")
public static let funk = NSSound.Name("Funk")
public static let glass = NSSound.Name("Glass")
public static let hero = NSSound.Name("Hero")
public static let morse = NSSound.Name("Morse")
public static let ping = NSSound.Name("Ping")
public static let pop = NSSound.Name("Pop")
public static let purr = NSSound.Name("Purr")
public static let sosumi = NSSound.Name("Sosumi")
public static let submarine = NSSound.Name("Submarine")
public static let tink = NSSound.Name("Tink")
}
然后您可以非常简单地播放任何系统声音,如下所示:
NSSound.glass?.play()
注意,你也可以让系统播放默认的错误声音如下:
NSSound.beep()
NSBeep()
【讨论】:
我发现这适用于版本 8.3.3 Playground:
import AudioToolbox
AudioServicesPlaySystemSound(1209) //plays a beep tone
【讨论】:
1209 似乎在播放“dtmf-9.caf”。
在 Swift 4 中,您可以使用 NSSound.beep() 来播放系统偏好设置中设置的系统提示音。
【讨论】:
在这里,导入 macOS 10.9+ (Xcode 9.0+):https://developer.apple.com/documentation/appkit/nssound/2903487-beep
import AppKit
NSSound.beep()
【讨论】: