Swift-在Cocoa中播放系统默认声音的最简单方法(macOS,OS X)

Kyl*_*KIM 2 macos cocoa swift

我正在学习可可编程.

我只需要在异步任务完成或在我自己的Cocoa项目中失败时播放声音.

所以我想知道最简单的方法是什么.

虽然它应该很容易,但我还没有在Swift中找到它.

提前谢谢了

Sup*_*tar 13

最简单?将其放入项目中的 Swift 文件中:

Swift 4.2 到 5.3

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")
}
Run Code Online (Sandbox Code Playgroud)

Swift 3.2 到 4.1

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")
}
Run Code Online (Sandbox Code Playgroud)

然后您可以非常简单地播放任何系统声音,如下所示:

NSSound.glass?.play()
Run Code Online (Sandbox Code Playgroud)

请注意,您还可以让系统播放这样的默认错误声音:

Swift 4.0 及更高版本(通过 5.3 测试)

NSSound.beep()
Run Code Online (Sandbox Code Playgroud)

Swift 1-3 和 Objective-C

NSBeep()
Run Code Online (Sandbox Code Playgroud)


Bor*_*kyi 9

可能,最简单的方法是使用NSSound.例如:

NSSound(named: "Purr")?.play()
Run Code Online (Sandbox Code Playgroud)

来自Apple文档:

如果没有使用soundName的已知NSSound对象,则此方法尝试通过在应用程序的主包中搜索声音文件来创建一个(有关如何搜索包的内容的说明,请参阅NSBundle).如果应用程序主包中没有声音文件,则按顺序搜索以下目录:

  • 〜/库/声音

  • /库/声音

  • /网络/库/声音

  • /系统/库/声音

如果要播放系统蜂鸣声,请使用此NSBeep功能.

  • 我应该导入什么才能使用 NSSound?Swift 显示未解析标识符“NSSound”的使用 (3认同)

小智 5

我发现这在版本 8.3.3 Playground 中有效:

import AudioToolbox
AudioServicesPlaySystemSound(1209) //plays a beep tone
Run Code Online (Sandbox Code Playgroud)

  • 我如何知道为该函数调用提供什么幻数? (3认同)