The NSSound class provides a simple interface for loading and playing audio files. This class supports the same audio encodings and file formats that are supported by Core Audio and QuickTime.


To use this class, initialize a new instance with the desired file or audio data. You can configure assorted aspects of the audio playback, including the volume and whether the sound loops before you play it. Depending on the type of the audio data, this class may use either Core Audio or QuickTime to handle the actual playback. (Typically, it uses Core Audio to play files in the AIFF, WAVE, NeXT, SD2, AU, and MP3 formats and may use it for other formats in the future as well.) Playback occurs asynchronously so that your application can continue doing work.

You should retain NSSound objects before initiating playback or make sure you have a strong reference to them in a garbage-collected environment. Upon deallocation, a sound object stops playback of the sound (as needed) so that it can free up the corresponding audio resources. If you want to deallocate a sound object immediately after playback, assign a delegate and use the sound(_:didFinishPlaying:) method to deallocate it.

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


Creating Sounds

class func canInit(with: NSPasteboard)

Indicates whether the receiver can create an instance of itself from the data in a pasteboard.

init?(contentsOfFile: String, byReference: Bool)

Initializes the receiver with the the audio data located at a given filepath.

init?(contentsOf: URL, byReference: Bool)

Initializes the receiver with the audio data located at a given URL.

init?(data: Data)

Initializes the receiver with a given audio data.

init?(pasteboard: NSPasteboard)

Initializes the receiver with data from a pasteboard. The pasteboard should contain a type returned by NSSound. NSSound expects the data to have a proper magic number, sound header, and data for the formats it supports.

Configuring Sounds

var name: String?

The name assigned to the sound.

var volume: Float

The volume of the sound.

var currentTime: TimeInterval

The sound’s playback progress, in seconds.

var loops: Bool

A Boolean that indicates whether the sound restarts playback when it reaches the end of its content.

var playbackDeviceIdentifier: String?

Identifies the sound’s output device

var delegate: NSSoundDelegate?

The sound’s delegate.

Getting Sound Information

class func soundUnfilteredTypes()

Provides the file types the NSSound class understands.

init?(named: String)

Returns the NSSound instance associated with a given name.

var duration: TimeInterval

The duration of the sound, in seconds.

Playing Sounds

var isPlaying: Bool

A Boolean that indicates whether the sound is playing its audio data.

func pause()

Pauses audio playback.

func play()

Initiates audio playback.

func resume()

Resumes audio playback.

func stop()

Concludes audio playback.

Writing Sounds

func write(to: NSPasteboard)

Writes the receiver’s data to a pasteboard.


NSPasteboard Type for Sound Data

The NSSound class defines this common pasteboard data type.

Instance Methods