System Sound Services Reference
System Sound Services provides a C interface for playing short sounds and for invoking vibration on iOS devices that support vibration.
You can use System Sound Services to play short (30 seconds or shorter) sounds. The interface does not provide level, positioning, looping, or timing control, and does not support simultaneous playback: You can play only one sound at a time. You can use System Sound Services to provide audible alerts. On some iOS devices, alerts can include vibration.
For usage information for iOS, refer to Using Audio in Multimedia Programming Guide.
-
Creates a system sound object.
Declaration
Swift
func AudioServicesCreateSystemSoundID(_inFileURL: CFURL, _outSystemSoundID: UnsafeMutablePointer<SystemSoundID>) -> OSStatusObjective-C
OSStatus AudioServicesCreateSystemSoundID ( CFURLRef inFileURL, SystemSoundID *outSystemSoundID );Parameters
inFileURLThe URL of the audio file to play.
outSystemSoundIDOn output, a system sound object associated with the specified audio file.
Return Value
A result code.
Availability
Available in iOS 2.0 and later.
See Also
-
Disposes of a system sound object and associated resources.
Declaration
Swift
func AudioServicesDisposeSystemSoundID(_inSystemSoundID: SystemSoundID) -> OSStatusObjective-C
OSStatus AudioServicesDisposeSystemSoundID ( SystemSoundID inSystemSoundID );Parameters
inSystemSoundIDThe system sound object to dispose of.
Return Value
A result code.
Availability
Available in iOS 2.0 and later.
See Also
-
Plays a system sound as an alert.
Declaration
Swift
func AudioServicesPlayAlertSound(_inSystemSoundID: SystemSoundID)Objective-C
void AudioServicesPlayAlertSound ( SystemSoundID inSystemSoundID );Parameters
inSystemSoundIDThe system sound object to play as an alert.
Before using this function, call the
AudioServicesCreateSystemSoundIDfunction to obtain a system sound.Discussion
Depending on the particular iOS device, this function plays a short sound and may invoke vibration. Calling this function does the following on various iOS devices:
iPhone—plays the specified sound. If the user has configured the Settings application for vibration on ring, also invokes vibration. However, the device does not vibrate if your app’s audio session is configured with the
AVAudioSessionCategoryPlayAndRecordorAVAudioSessionCategoryRecordaudio session category. This ensures that vibration doesn’t interfere with audio recording. For an explanation of audio session categories, see Categories Express Audio Roles.iPod touch, original—plays a short alert melody.
iPod touch, 2nd generation and newer—plays the specified sound.
In iOS, the duration of the sound to be played must not be more than 30 seconds.
In OS X, when a user has configured System Preferences to flash the screen for alerts, or if sound cannot be rendered, calling this function will result in the screen flashing. In OS X, pass the constant
kSystemSoundID_UserPreferredAlertto play the alert sound selected by the user in System Preferences. In iOS there is no preferred user alert sound.To play a short sound not used as an alert, use
AudioServicesPlaySystemSound.Availability
Available in iOS 2.0 and later.
-
Plays a system sound object.
Declaration
Swift
func AudioServicesPlaySystemSound(_inSystemSoundID: SystemSoundID)Objective-C
void AudioServicesPlaySystemSound ( SystemSoundID inSystemSoundID );Parameters
inSystemSoundIDThe system sound to play. Before using this function, call the
AudioServicesCreateSystemSoundIDfunction to obtain a system sound.Discussion
This function plays a short sound (30 seconds or less in duration). Because sound might play for several seconds, this function is executed asynchronously. To know when a sound has finished playing, call the
AudioServicesAddSystemSoundCompletionfunction to register a callback function.On some iOS devices, you can pass the
kSystemSoundID_Vibrateconstant to invoke vibration. On other iOS devices, calling this function with that constant does nothing.Sound files that you play using this function must be:
No longer than 30 seconds in duration
In linear PCM or IMA4 (IMA/ADPCM) format
Packaged in a
.caf,.aif, or.wavfile
In addition, when you use the
AudioServicesPlaySystemSoundfunction:Sounds play at the current system audio volume, with no programmatic volume control available
Sounds play immediately
Looping and stereo positioning are unavailable
Simultaneous playback is unavailable: You can play only one sound at a time
The sound is played locally on the device speakers; it does not use audio routing.
Availability
Available in iOS 2.0 and later.
-
Registers a callback function that is invoked when a specified system sound finishes playing.
Declaration
Swift
func AudioServicesAddSystemSoundCompletion(_inSystemSoundID: SystemSoundID, _inRunLoop: CFRunLoop?, _inRunLoopMode: CFString?, _inCompletionRoutine: AudioServicesSystemSoundCompletionProc, _inClientData: UnsafeMutablePointer<Void>) -> OSStatusObjective-C
OSStatus AudioServicesAddSystemSoundCompletion ( SystemSoundID inSystemSoundID, CFRunLoopRef inRunLoop, CFStringRef inRunLoopMode, AudioServicesSystemSoundCompletionProc inCompletionRoutine, void *inClientData );Parameters
inSystemSoundIDThe system sound that your callback function is to respond to.
inRunLoopThe run loop in which the callback function should run. Pass
NULLto use the main run loop.inRunLoopModeThe mode for the run loop in which the callback functions should run. Pass
NULLto use the default run loop mode.inCompletionRoutineThe callback function to be invoked when the specified system sound has finished playing.
inClientDataApplication data to be passed to your callback function when it is invoked. Can be
NULL.Return Value
A result code.
Discussion
Because a system sound may play for several seconds, you might want to know when it has finished playing. For example, you may want to wait until a system sound has finished playing before you play another sound.
Availability
Available in iOS 2.0 and later.
-
Unregisters any completion callback functions that were registered for a specified system sound.
Declaration
Swift
func AudioServicesRemoveSystemSoundCompletion(_inSystemSoundID: SystemSoundID)Objective-C
void AudioServicesRemoveSystemSoundCompletion ( SystemSoundID inSystemSoundID );Parameters
inSystemSoundIDThe system sound for which callback functions should be removed.
Availability
Available in iOS 2.0 and later.
-
Gets information about a System Sound Services property.
Declaration
Swift
func AudioServicesGetPropertyInfo(_inPropertyID: AudioServicesPropertyID, _inSpecifierSize: UInt32, _inSpecifier: UnsafePointer<Void>, _outPropertyDataSize: UnsafeMutablePointer<UInt32>, _outWritable: UnsafeMutablePointer<DarwinBoolean>) -> OSStatusObjective-C
OSStatus AudioServicesGetPropertyInfo ( AudioServicesPropertyID inPropertyID, UInt32 inSpecifierSize, const void *inSpecifier, UInt32 *outPropertyDataSize, Boolean *outWritable );Parameters
inPropertyIDThe property you want information about.
inSpecifierSizeThe size of the buffer pointed to by the
inSpecifierparameter. Pass0if no specifier buffer is required.inSpecifierA pointer to a specifier buffer, if such a buffer is required by the property about which you want information. Pass
NULLif no specifier is required.outPropertyDataSizeOn output, the size, in bytes, of the property value. To get the property value, you need a buffer of at least this size.
outWritableOn output,
trueif the property is writable, orfalseif the property is read only.Return Value
A result code.
Discussion
System Sound Services properties are listed and described in System Sound Services Property Identifiers.
Availability
Available in iOS 2.0 and later.
See Also
-
Gets a specified System Sound Services property value.
Declaration
Swift
func AudioServicesGetProperty(_inPropertyID: AudioServicesPropertyID, _inSpecifierSize: UInt32, _inSpecifier: UnsafePointer<Void>, _ioPropertyDataSize: UnsafeMutablePointer<UInt32>, _outPropertyData: UnsafeMutablePointer<Void>) -> OSStatusObjective-C
OSStatus AudioServicesGetProperty ( AudioServicesPropertyID inPropertyID, UInt32 inSpecifierSize, const void *inSpecifier, UInt32 *ioPropertyDataSize, void *outPropertyData );Parameters
inPropertyIDThe property whose value you want.
inSpecifierSizeThe size of the buffer pointed to by the
inSpecifierparameter. Pass0if no specifier buffer is required.inSpecifierA pointer to a specifier buffer, if such a buffer is required by the property about which you want information. Pass
NULLif no specifier is required.ioPropertyDataSizeOn input, the size, in bytes, of the buffer pointed to by the
outPropertyDataparameter. Call theAudioServicesGetPropertyInfofunction to find out the size required for this buffer. On output, the number of bytes written to the buffer.outPropertyDataOn output, the property value.
Return Value
A result code.
Discussion
System Sound Services properties are listed and described in System Sound Services Property Identifiers.
Special Considerations
Some Core Audio property values are C types and others are Core Foundation objects.
If you call this function to retrieve a value that is a Core Foundation object, then this function—despite the use of “Get” in its name—duplicates the object. You are responsible for releasing the object, as described in The Create Rule in Memory Management Programming Guide for Core Foundation.
Availability
Available in iOS 2.0 and later.
-
Sets the value for a specified System Sound Services property.
Declaration
Swift
func AudioServicesSetProperty(_inPropertyID: AudioServicesPropertyID, _inSpecifierSize: UInt32, _inSpecifier: UnsafePointer<Void>, _inPropertyDataSize: UInt32, _inPropertyData: UnsafePointer<Void>) -> OSStatusObjective-C
OSStatus AudioServicesSetProperty ( AudioServicesPropertyID inPropertyID, UInt32 inSpecifierSize, const void *inSpecifier, UInt32 inPropertyDataSize, const void *inPropertyData );Parameters
inPropertyIDThe property whose value you want to set.
inSpecifierSizeThe size of the buffer pointed to by the
inSpecifierparameter. Pass0if no specifier buffer is required.inSpecifierA pointer to a specifier buffer, if such a buffer is required by the property about which you want information. Pass
NULLif no specifier is required.inPropertyDataSizeThe size, in bytes, of the buffer pointed to by the
inPropertyDataparameter.inPropertyDataThe property value you want to set.
Return Value
A result code.
Discussion
System Sound Services properties are listed and described in System Sound Services Property Identifiers.
Availability
Available in iOS 2.0 and later.
See Also
-
Invoked when a system sound finishes playing.
Declaration
Swift
typealias AudioServicesSystemSoundCompletionProc = (SystemSoundID, UnsafeMutablePointer<Void>) -> VoidObjective-C
typedef void (*AudioServicesSystemSoundCompletionProc) ( SystemSoundID ssID, void *clientData );Parameters
ssIDThe system sound that has finished playing.
clientDataApplication data that you specified when registering the callback function.
Discussion
Because a system sound may play for up to 30 seconds, the
AudioServicesPlaySystemSoundfunction executes asynchronously (that is, it returns immediately). This callback gets invoked when a specified system sound has finished playing. You can use this callback, for example, to help you avoid playing a second sound while a first sound is still playing.Availability
Available in iOS 2.0 and later.
-
The data type for a system sound property identifier.
Declaration
Swift
typealias AudioServicesPropertyID = UInt32Objective-C
typedef UInt32 AudioServicesPropertyID;Discussion
System Audio Services properties are listed in System Sound Services Property Identifiers.
Import Statement
Objective-C
@import AudioToolbox;Swift
import AudioToolboxAvailability
Available in iOS 2.0 and later.
-
A system sound object, identified with a sound file you want to play.
Discussion
Call the
AudioServicesCreateSystemSoundIDfunction to obtain a system sound object.Import Statement
Objective-C
@import AudioToolbox;Swift
import AudioToolboxAvailability
Available in iOS 2.0 and later.
-
Identifiers for alert sounds and alternatives to sounds, for use with the
AudioServicesPlayAlertSoundfunction.Declaration
Swift
var kSystemSoundID_Vibrate: SystemSoundID { get }Objective-C
enum { kSystemSoundID_Vibrate = 0x00000FFF };Constants
-
kSystemSoundID_VibratekSystemSoundID_VibrateOn the iPhone, use this constant with the
AudioServicesPlayAlertSoundfunction to invoke a brief vibration. On the iPod touch, does nothing.Available in iOS 2.0 and later.
-
-
Property identifiers used when playing alerts with System Sound Services.
Declaration
Swift
var kAudioServicesPropertyIsUISound: AudioServicesPropertyID { get } var kAudioServicesPropertyCompletePlaybackIfAppDies: AudioServicesPropertyID { get }Objective-C
enum { kAudioServicesPropertyIsUISound = 'isui', kAudioServicesPropertyCompletePlaybackIfAppDies = 'ifdi' };Constants
-
kAudioServicesPropertyIsUISoundkAudioServicesPropertyIsUISoundA
UInt32value, where1means that, for the audio file specified by a system sound passed in theinSpecifierparameter, the System Sound server respects the user setting in the Sound Effects preference and is silent when the user turns off sound effects.This property is set to
1by default. Set it to0for the system sound to always play when passed toAudioServicesPlaySystemSound, regardless of the user's setting in sound preferences.Available in iOS 2.0 and later.
-
kAudioServicesPropertyCompletePlaybackIfAppDieskAudioServicesPropertyCompletePlaybackIfAppDiesA
UInt32value, where1means that the audio file specified by a system sound passed in theinSpecifierparameter should finish playing even if the client application terminates. This could happen, for example, if the user quits or the application terminates unexpectedly while the sound is playing. The default is0. That is, you must explicitly set this property’s value to1if you want the sound to complete playing even if the application terminates.Available in iOS 2.0 and later.
-
This table lists the result codes defined for System Sound Services.
-
No error has occurred.
Value
0
Description
No error has occurred.
Available in iOS 2.0 and later.
-
The property is not supported.
Value
'pty?'
Description
The property is not supported.
Available in iOS 2.0 and later.
-
The size of the property data was not correct.
Value
'!siz'
Description
The size of the property data was not correct.
Available in iOS 2.0 and later.
-
The size of the specifier data was not correct.
Value
'!spc'
Description
The size of the specifier data was not correct.
Available in iOS 2.0 and later.
-
An unspecified error has occurred.
Value
-1500
Description
An unspecified error has occurred.
Available in iOS 2.0 and later.
-
System sound client message timed out.
Value
-1501
Description
System sound client message timed out.
Available in iOS 2.0 and later.
Copyright © 2015 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2013-08-08
