NSSound Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/AppKit.framework |
| Availability | Available in OS X v10.0 and later. |
| Companion guide | |
| Declared in | NSSound.h |
Overview
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.
Tasks
Creating Sounds
-
+ canInitWithPasteboard: -
– initWithContentsOfFile:byReference: -
– initWithContentsOfURL:byReference: -
– initWithData: -
– initWithPasteboard:
Configuring Sounds
-
– name -
– setName: -
– volume -
– setVolume: -
– currentTime -
– setCurrentTime: -
– loops -
– setLoops: -
– playbackDeviceIdentifier -
– setPlaybackDeviceIdentifier: -
– channelMapping -
– setChannelMapping: -
– delegate -
– setDelegate:
Getting Sound Information
Playing Sounds
Writing Sounds
Deprecated
-
+ soundUnfilteredFileTypesDeprecated in OS X v10.5 -
+ soundUnfilteredPasteboardTypesDeprecated in OS X v10.5
Class Methods
canInitWithPasteboard:
Indicates whether the receiver can create an instance of itself from the data in a pasteboard.
Parameters
- pasteboard
Pasteboard containing sound data.
Return Value
YES when the receiver can handle the data represented by pasteboard; NO otherwise.
Discussion
The soundUnfilteredPasteboardTypes method is used to find out whether the class can handle the data in pasteboard.
Availability
- Available in OS X v10.0 and later.
Declared In
NSSound.hsoundNamed:
Returns the NSSound instance associated with a given name.
Parameters
- soundName
Name that identifies sound data.
Return Value
NSSound instance initialized with the sound data identified by soundName.
Discussion
The returned object can be one of the following:
One that’s been assigned a name with
setName:One of the named system sounds provided by the Application Kit framework
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 no data can be found for soundName, no object is created, and nil is returned.
The preferred way to locate a sound is to pass a name without the file extension. See the class description for a list of the supported sound file extensions.
Availability
- Available in OS X v10.0 and later.
Declared In
NSSound.hsoundUnfilteredTypes
Provides the file types the NSSound class understands.
Return Value
Array of UTIs identifying the file types the NSSound class understands.
Availability
- Available in OS X v10.5 and later.
Declared In
NSSound.hInstance Methods
channelMapping
Provides the receiver’s channel map.
Return Value
The receiver’s audio-channel–to–device–channel mappings.
Discussion
A channel map correlates a sound’s channels to the the output-device’s channels. For example, a two-channel sound being played on a five-channel device should have a channel map to optimize the sound-playing experience. The default map, correlates the first sound channel to the first output channel, the second sound channel to the second output channel, and so on.
For details about channel maps, see Core Audio Overview > “Common Tasks in OS X.”
Availability
- Available in OS X v10.5 and later.
See Also
Declared In
NSSound.hcurrentTime
Provides the receiver’s playback progress in seconds.
Return Value
Receiver’s playback progress in seconds.
Discussion
Sounds start with currentTime == 0 and end with currentTime == ([<sound> duration] - 1).
Availability
- Available in OS X v10.5 and later.
See Also
Declared In
NSSound.hdelegate
Returns the receiver’s delegate.
Return Value
The receiver’s delegate.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSSound.hduration
Provides the duration of the receiver in seconds.
Return Value
Duration of the receiver in seconds.
Availability
- Available in OS X v10.5 and later.
Declared In
NSSound.hinitWithContentsOfFile:byReference:
Initializes the receiver with the the audio data located at a given filepath.
Parameters
- filepath
Path to the sound file with which the receiver is to be initialized.
- byRef
When
YESonly the name of the sound is stored with theNSSoundinstance when archived usingencodeWithCoder:; otherwise the audio data is archived along with the instance.
Return Value
Initialized NSSound instance.
Availability
- Available in OS X v10.0 and later.
Declared In
NSSound.hinitWithContentsOfURL:byReference:
Initializes the receiver with the audio data located at a given URL.
Parameters
- fileUrl
URL to the sound file with which the receiver is to be initialized.
- byRef
When
YESonly the name of the sound is stored with theNSSoundinstance when archived usingencodeWithCoder:; otherwise the audio data is archived along with the instance.
Return Value
Initialized NSSound instance.
Availability
- Available in OS X v10.0 and later.
Declared In
NSSound.hinitWithData:
Initializes the receiver with a given audio data.
Parameters
- audioData
Audio data with which the receiver is to be initialized. The data must have a proper magic number, sound header, and data for the formats the
NSSoundclass supports.
Return Value
Initialized NSSound instance.
Availability
- Available in OS X v10.0 and later.
Declared In
NSSound.hinitWithPasteboard:
Initializes the receiver with data from a pasteboard. The pasteboard should contain a type returned by soundUnfilteredPasteboardTypes. NSSound expects the data to have a proper magic number, sound header, and data for the formats it supports.
Parameters
- pasteboard
The pasteboard containing the audio data with which the receiver is to be initialized. The pasteboard must contain a type returned by
soundUnfilteredPasteboardTypes. The contained data must have a proper magic number, sound header, and data for the formats theNSSoundclass supports.
Return Value
Initialized NSSound instance.
Availability
- Available in OS X v10.0 and later.
Declared In
NSSound.hisPlaying
Indicates whether the receiver is playing its audio data.
Return Value
YES when the receiver is playing its audio data, NO otherwise.
Availability
- Available in OS X v10.0 and later.
Declared In
NSSound.hloops
Indicates whether the receiver restarts playback when it reaches the end of its content. Default: NO.
Availability
- Available in OS X v10.5 and later.
See Also
Declared In
NSSound.hname
Returns the name assigned to the receiver.
Return Value
Name assigned to the receiver; nil when no name has been assigned.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSSound.hpause
Pauses audio playback.
Return Value
YES when playback is paused successfully, NO when playback is already paused or when an error occurred.
Availability
- Available in OS X v10.0 and later.
Declared In
NSSound.hplay
Initiates audio playback.
Return Value
YES when playback is initiated, NO when playback is already in progress or when an error occurred.
Discussion
This method initiates playback asynchronously and returns control to your application. Therefore, your application can continue doing work while the audio is playing.
Availability
- Available in OS X v10.0 and later.
Declared In
NSSound.hplaybackDeviceIdentifier
Identifies the receiver’s output device.
Return Value
Unique identifier of a sound output device.
Availability
- Available in OS X v10.5 and later.
See Also
Declared In
NSSound.hresume
Resumes audio playback.
Return Value
YES when playback is resumed, NO when playback is in progress or when an error occurred.
Discussion
Assumes the receiver has been previously paused by sending it pause.
Availability
- Available in OS X v10.0 and later.
Declared In
NSSound.hsetChannelMapping:
Specifies the receiver’s channel map.
Parameters
- channelMapping
Audio-channel—to—device–channel mappings for the receiver.
Availability
- Available in OS X v10.5 and later.
See Also
Declared In
NSSound.hsetCurrentTime:
Specifies the receivers playback progress in seconds.
Parameters
- currentTime
Playback progress for the receiver.
Discussion
This property is not archived, copied, or stored on the pasteboard.
Availability
- Available in OS X v10.5 and later.
See Also
Declared In
NSSound.hsetDelegate:
Set the receiver’s delegate.
Parameters
- delegate
Object to serve as the receiver’s delegate.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSSound.hsetLoops:
Specifies whether the receiver restarts playback when it reaches the end of its content.
Parameters
Discussion
When loops is YES, the receiver does not send sound:didFinishPlaying: to its delegate when it reaches the end of its content and restarts playback.
Availability
- Available in OS X v10.5 and later.
Declared In
NSSound.hsetName:
Registers the receiver under a given name.
Parameters
- soundName
Name to assign the receiver. The name must be unused by other
NSSoundinstances.
Return Value
YES when successful; NO otherwise.
Discussion
If the receiver is already registered under another name, this method first unregisters the prior name.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSSound.hsetPlaybackDeviceIdentifier:
Specifies the receiver’s output device.
Parameters
- playbackDeviceIdentifier
Unique identifier of a sound output device.
Availability
- Available in OS X v10.5 and later.
See Also
Declared In
NSSound.hsetVolume:
Specifies the volume of the receiver.
Parameters
- volume
Volume at which the receiver is to play.
Discussion
The valid range is between 0.0 and 1.0.
This method does not affect the systemwide volume.
Availability
- Available in OS X v10.5 and later.
See Also
Declared In
NSSound.hstop
Concludes audio playback.
Return Value
YES when playback is concluded successfully or if it’s paused, NO otherwise.
Availability
- Available in OS X v10.0 and later.
See Also
-
– sound:didFinishPlaying:(NSSoundDelegate)
Declared In
NSSound.hvolume
Provides the volume of the receiver.
Return Value
Volume of the receiver.
Discussion
The valid range is between 0.0 and 1.0.
This method does not affect the systemwide volume.
Availability
- Available in OS X v10.5 and later.
See Also
Declared In
NSSound.hwriteToPasteboard:
Writes the receiver’s data to a pasteboard.
Parameters
- pasteboard
Pasteboard to which the receiver is to write its data.
Availability
- Available in OS X v10.0 and later.
Declared In
NSSound.hConstants
NSPasteboard Type for Sound Data
The NSSound class defines this common pasteboard data type.
NSString *NSSoundPboardType;
Constants
© 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-06-04)