Class

NSSound

A simple interface for loading and playing audio files.

Declaration

@interface NSSound : NSObject

Overview

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.

Topics

Detecting When a Sound Finishes Playing

delegate

The sound’s delegate.

NSSoundDelegate

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

name

The name assigned to the sound.

volume

The volume of the sound.

currentTime

The sound’s playback progress, in seconds.

loops

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

playbackDeviceIdentifier

Identifies the sound’s output device

Getting Sound Information

soundUnfilteredTypes

Provides the file types the NSSound class understands.

+ soundNamed:

Returns the NSSound instance associated with a given name.

duration

The duration of the sound, in seconds.

Playing Sounds

playing

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.

Deprecated

- channelMapping

Provides the receiver’s channel map.

Deprecated
- setChannelMapping:

Specifies the receiver’s channel map.

Deprecated
+ soundUnfilteredFileTypes

Provides the list of file types the NSSound class understands.

Deprecated
+ soundUnfilteredPasteboardTypes

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

Deprecated

Constants

NSPasteboard Type for Sound Data

The NSSound class defines this common pasteboard data type.

See Also

Sounds

NSBeep

Plays the system beep.