Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/AppKit.framework |
Availability | Available in Mac OS X v10.0 and later. |
Companion guide | |
Declared in | NSSound.h |
Related sample code |
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.
+ canInitWithPasteboard:
– initWithContentsOfFile:byReference:
– initWithContentsOfURL:byReference:
– initWithData:
– initWithPasteboard:
– name
– setName:
– volume
– setVolume:
– currentTime
– setCurrentTime:
– loops
– setLoops:
– playbackDeviceIdentifier
– setPlaybackDeviceIdentifier:
– channelMapping
– setChannelMapping:
– delegate
– setDelegate:
– isPlaying
– pause
– play
– resume
– stop
– sound:didFinishPlaying:
delegate method
+ soundUnfilteredFileTypes
Deprecated in Mac OS X v10.5
+ soundUnfilteredPasteboardTypes
Deprecated in Mac OS X v10.5
Indicates whether the receiver can create an instance of itself from the data in a pasteboard.
+ (BOOL)canInitWithPasteboard:(NSPasteboard *)pasteboard
Pasteboard containing sound data.
YES
when the receiver can handle the data represented by pasteboard; NO
otherwise.
The soundUnfilteredPasteboardTypes
method is used to find out whether the class can handle the data in pasteboard.
NSSound.h
Returns the NSSound
instance associated with a given name.
+ (id)soundNamed:(NSString *)soundName
Name that identifies sound data.
NSSound
instance initialized with the sound data identified by soundName.
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.
NSSound.h
Provides the file types the NSSound
class understands.
+ (NSArray*)soundUnfilteredTypes
Array of UTIs identifying the file types the NSSound
class understands.
NSSound.h
Provides the receiver’s channel map.
- (NSArray *)channelMapping
The receiver’s audio-channel–to–device–channel mappings.
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 > “An Overview of Common Tasks.”
NSSound.h
Provides the receiver’s playback progress in seconds.
- (NSTimeInterval)currentTime
Receiver’s playback progress in seconds.
Sounds start with currentTime == 0
and end with currentTime == ([<sound> duration] - 1)
.
NSSound.h
Returns the receiver’s delegate.
- (id)delegate
The receiver’s delegate.
NSSound.h
Provides the duration of the receiver in seconds.
- (NSTimeInterval)duration
Duration of the receiver in seconds.
NSSound.h
Initializes the receiver with the the audio data located at a given filepath.
- (id)initWithContentsOfFile:(NSString *)filepath byReference:(BOOL)byRef
Path to the sound file with which the receiver is to be initialized.
When YES
only the name of the sound is stored with the NSSound
instance when archived using encodeWithCoder:
; otherwise the audio data is archived along with the instance.
Initialized NSSound
instance.
NSSound.h
Initializes the receiver with the audio data located at a given URL.
- (id)initWithContentsOfURL:(NSURL *)fileUrl byReference:(BOOL)byRef
URL to the sound file with which the receiver is to be initialized.
When YES
only the name of the sound is stored with the NSSound
instance when archived using encodeWithCoder:
; otherwise the audio data is archived along with the instance.
Initialized NSSound
instance.
NSSound.h
Initializes the receiver with a given audio data.
- (id)initWithData:(NSData *)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 NSSound
class supports.
Initialized NSSound
instance.
NSSound.h
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.
- (id)initWithPasteboard:(NSPasteboard *)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 the NSSound
class supports.
Initialized NSSound
instance.
NSSound.h
Indicates whether the receiver is playing its audio data.
- (BOOL)isPlaying
YES
when the receiver is playing its audio data, NO
otherwise.
NSSound.h
Indicates whether the receiver restarts playback when it reaches the end of its content. Default: NO
.
- (BOOL)loops
YES
when the receiver restarts playback when it finishes, NO
otherwise.
NSSound.h
Returns the name assigned to the receiver.
- (NSString *)name
Name assigned to the receiver; nil
when no name has been assigned.
NSSound.h
Pauses audio playback.
- (BOOL)pause
YES
when playback is paused successfully, NO
when playback is already paused or when an error occurred.
NSSound.h
Initiates audio playback.
- (BOOL)play
YES
when playback is initiated, NO
when playback is already in progress or when an error occurred.
This method initiates playback asynchronously and returns control to your application. Therefore, your application can continue doing work while the audio is playing.
NSSound.h
Identifies the receiver’s output device.
- (NSString *)playbackDeviceIdentifier
Unique identifier of a sound output device.
NSSound.h
Resumes audio playback.
- (BOOL)resume
YES
when playback is resumed, NO
when playback is in progress or when an error occurred.
Assumes the receiver has been previously paused by sending it pause
.
NSSound.h
Specifies the receiver’s channel map.
- (void)setChannelMapping:(NSArray *)channelMapping
Audio-channel—to—device–channel mappings for the receiver.
NSSound.h
Specifies the receivers playback progress in seconds.
- (void)setCurrentTime:(NSTimeInterval)currentTime
Playback progress for the receiver.
This property is not archived, copied, or stored on the pasteboard.
NSSound.h
Set the receiver’s delegate.
- (void)setDelegate:(id)delegate
Object to serve as the receiver’s delegate.
NSSound.h
Specifies whether the receiver restarts playback when it reaches the end of its content.
- (void)setLoops:(BOOL)loops
YES
to have the receiver restart playback when it reaches the end of its content.
NO
to have the receiver conclude playback, instead.
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.
NSSound.h
Registers the receiver under a given name.
- (BOOL)setName:(NSString *)soundName
Name to assign the receiver. The name must be unused by other NSSound
instances.
YES
when successful; NO
otherwise.
If the receiver is already registered under another name, this method first unregisters the prior name.
NSSound.h
Specifies the receiver’s output device.
- (void)setPlaybackDeviceIdentifier:(NSString *)playbackDeviceIdentifier
Unique identifier of a sound output device.
NSSound.h
Specifies the volume of the receiver.
- (void)setVolume:(float)volume
Volume at which the receiver is to play.
This method does not affect the systemwide volume.
NSSound.h
Concludes audio playback.
- (BOOL)stop
YES
when playback is concluded successfully or if it’s paused, NO
otherwise.
NSSound.h
Provides the volume of the receiver.
- (float)volume
Volume of the receiver.
NSSound.h
Writes the receiver’s data to a pasteboard.
- (void)writeToPasteboard:(NSPasteboard *)pasteboard
Pasteboard to which the receiver is to write its data.
NSSound.h
This delegate method is called when an NSSound
instance has completed playback of its sound data.
- (void)sound:(NSSound *)sound didFinishPlaying:(BOOL)finishedPlaying
The NSSound
that has completed playback of its sound data.
YES
when playback was successful; NO
otherwise.
NSSound.h
The NSSound
class defines this common pasteboard data type.
NSString *NSSoundPboardType;
NSSound.h
© 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-10-15)