A simple interface for loading and playing audio files.


@interface 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 NSBeep function.


Detecting When a Sound Finishes Playing


The sound’s delegate.


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

Creating Sounds

+ canInitWithPasteboard:

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

- initWithContentsOfFile:byReference:

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

- initWithContentsOfURL:byReference:

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

- initWithData:

Initializes the receiver with a given audio data.

- initWithPasteboard:

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


The name assigned to the sound.


The volume of the sound.


The sound’s playback progress, in seconds.


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


Identifies the sound’s output device

Getting Sound Information


Provides the file types the NSSound class understands.

+ soundNamed:

Returns the NSSound instance associated with a given name.


The duration of the sound, in seconds.

Playing Sounds


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

- pause

Pauses audio playback.

- play

Initiates audio playback.

- resume

Resumes audio playback.

- stop

Concludes audio playback.

Writing Sounds

- writeToPasteboard:

Writes the receiver’s data to a pasteboard.


- channelMapping

Provides the receiver’s channel map.

- setChannelMapping:

Specifies the receiver’s channel map.

+ soundUnfilteredFileTypes

Provides the list of file types the NSSound class understands.

+ soundUnfilteredPasteboardTypes

Provides a list of the pasteboard types that the NSSound class can accept.



NSPasteboard Type for Sound Data

The NSSound class defines this common pasteboard data type.

See Also



Plays the system beep.