A simple interface for loading and playing audio files.


class NSSound : NSObject


NSSound 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 beep() function.


Detecting When a Sound Finishes Playing

var delegate: NSSoundDelegate?

The sound’s delegate.

protocol NSSoundDelegate

A set of optional methods implemented by delegates of NSSound objects.

Creating Sounds

class func canInit(with: NSPasteboard) -> Bool

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 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: NSSound.Name?

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.

Getting Sound Information

class var soundUnfilteredTypes: [String]

Provides the file types the NSSound class understands.

init?(named: NSSound.Name)

Returns the NSSound instance associated with a given name.

var duration: TimeInterval

The duration of the sound, in seconds.

Playing Sounds

static func beep()

Plays the system beep.

var isPlaying: Bool

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

func pause() -> Bool

Pauses audio playback.

func play() -> Bool

Initiates audio playback.

func resume() -> Bool

Resumes audio playback.

func stop() -> Bool

Concludes audio playback.

Writing Sounds

func write(to: NSPasteboard)

Writes the receiver’s data to a pasteboard.