Kyle KIM
Kyle KIM

Reputation: 1441

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

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

Upvotes: 12

Views: 11523

Answers (7)

soundflix
soundflix

Reputation: 2828

Another very short way to write is:

NSSound.init(named: "Pop")?.play()

Upvotes: 0

This came up in my search today so I've cleaned up Ky.'s excellent answer a little bit for the next person:

import AppKit

public extension NSSound {

    enum Sound: String {
        case basso = "Basso"
        case blow = "Blow"
        case bottle = "Bottle"
        case frog = "Frog"
        case funk = "Funk"
        case glass = "Glass"
        case hero = "Hero"
        case morse = "Morse"
        case ping = "Ping"
        case pop = "Pop"
        case purr = "Purr"
        case sosumi = "Sosumi"
        case submarine = "Submarine"
        case tink = "Tink"
    }

    static func play(_ sound: Sound) {
        NSSound(named: NSSound.Name(sound.rawValue))?.play()
    }
}

Usage:

NSSound.play(.submarine)

Upvotes: 4

Ky -
Ky -

Reputation: 32173

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()

Upvotes: 22

Hlung
Hlung

Reputation: 14338

Here you go, with import for macOS 10.9+ (Xcode 9.0+): https://developer.apple.com/documentation/appkit/nssound/2903487-beep

import AppKit

NSSound.beep()

Upvotes: 3

MariaS
MariaS

Reputation: 79

I found this to work in Version 8.3.3 Playground:

import AudioToolbox
AudioServicesPlaySystemSound(1209) //plays a beep tone

Upvotes: 7

Béatrice Cassistat
Béatrice Cassistat

Reputation: 1078

In Swift 4, you can use NSSound.beep() which should play the system beep set in the System Preferences.

Upvotes: 4

Borys Verebskyi
Borys Verebskyi

Reputation: 4278

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.

Upvotes: 20

Related Questions