Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift- Simplest way to play system default sounds in Cocoa(macOS,OS X)

Tags:

macos

swift

cocoa

I am learning Cocoa Programming.

I only need to play sounds when an async task is done or failed in my very own Cocoa project.

So I would like to know what is the simplest way.

Although It should be quite easy, I have not found it out in Swift.

Many thanks in advance

like image 354
Kyle KIM Avatar asked Jul 18 '16 16:07

Kyle KIM


2 Answers

Probably, the easiest way is to use NSSound. For example:

NSSound(named: "Purr")?.play()

From Apple documentation:

If there’s no known NSSound object with soundName, this method tries to create one by searching for sound files in the application’s main bundle (see NSBundle for a description of how the bundle’s contents are searched). If no sound file can be located in the application main bundle, the following directories are searched in order:

  • ~/Library/Sounds

  • /Library/Sounds

  • /Network/Library/Sounds

  • /System/Library/Sounds

If you want to play the system beep sound, use the NSBeep function.

like image 80
Borys Verebskyi Avatar answered Oct 16 '22 16:10

Borys Verebskyi


Simplest? Put this in a Swift file in your project:

Swift 4.2 through 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")
}

Swift 3.2 through 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")
}

Then you can very simply play any system sound like this:

NSSound.glass?.play()

Note that you can also make the system play the default error sound like this:

Swift 4.0 and up (tested through 5.3)

NSSound.beep()

Swift 1-3 and Objective-C

NSBeep()
like image 29
Ky. Avatar answered Oct 16 '22 16:10

Ky.