JSQSystemSoundPlayer
@interface JSQSystemSoundPlayer : NSObject
The JSQSystemSoundPlayer class enables you to play sound effects, alert sounds, or other short sounds.
It lazily loads and caches all SystemSoundID objects and purges them upon
receiving the UIApplicationDidReceiveMemoryWarningNotification notification.
-
Returns whether or not the sound player is on. That is, whether the sound player is enabled or disabled. If disabled, it will not play sounds.
See
toggleSoundPlayerOn:Declaration
Objective-C
@property (readonly, assign, nonatomic) BOOL on;Swift
var on: Bool { get } -
The bundle in which the sound player uses to search for sound file resources. You may change this property as needed. The default value is the main bundle. This value must not be
nil.Declaration
Objective-C
@property (readwrite, strong, nonatomic) NSBundle *_Nonnull bundle;Swift
var bundle: Bundle { get set } -
Returns the shared
JSQSystemSoundPlayerobject. This method always returns the same sound system player object.Declaration
Objective-C
+ (nonnull JSQSystemSoundPlayer *)sharedPlayer;Swift
class func shared() -> JSQSystemSoundPlayerReturn Value
A shared instance of
JSQSystemSoundPlayer. -
Returns a new
JSQSystemSoundPlayerinstance with the specified bundle.Warning
Completion blocks are only called for sounds played with the shared player.
Declaration
Objective-C
- (nonnull instancetype)initWithBundle:(nonnull NSBundle *)bundle;Swift
init(bundle: Bundle)Parameters
bundleThe bundle in which the sound player uses to search for sound file resources.
Return Value
An initialized
JSQSystemSoundPlayerobject. -
Returns a new
JSQSystemSoundPlayerinstance using the main bundle.Warning
Completion blocks are only called for sounds played with the shared player.
Declaration
Objective-C
- (nonnull instancetype)init;Swift
init()Return Value
An initialized
JSQSystemSoundPlayerobject. -
Toggles the sound player on or off by setting the
kJSQSystemSoundPlayerUserDefaultsKeykey inNSUserDefaultsto the given value. This will enable or disable the playing of sounds viaJSQSystemSoundPlayerglobally. This setting is persisted across application launches.Warning
Disabling the sound player (passing a value of
NO) will invoke thestopAllSoundsmethod.Declaration
Objective-C
- (void)toggleSoundPlayerOn:(BOOL)on;Swift
func toggle(on: Bool)Parameters
onA boolean indicating whether or not to enable or disable the sound player settings. Pass
YESto turn sounds on, andNOto turn sounds off. -
Plays a system sound object corresponding to an audio file with the given filename and extension, and excutes
completionBlockwhen the sound has stopped playing. The system sound player will lazily initialize and load the file before playing it, and then cache its correspondingSystemSoundID. If this file has previously been played, it will be loaded from cache and played immediately.Note
The fileExtension parameter must be one of
kJSQSystemSoundTypeCAF,kJSQSystemSoundTypeAIF,kJSQSystemSoundTypeAIFF, orkJSQSystemSoundTypeWAV.Warning
If the system sound object cannot be created, this method does nothing.
Declaration
Objective-C
- (void)playSoundWithFilename:(nonnull NSString *)filename fileExtension:(nonnull NSString *)fileExtension completion:(nullable JSQSystemSoundPlayerCompletionBlock) completionBlock;Swift
func playSound(withFilename filename: String, fileExtension: String, completion completionBlock: JSQSystemSoundPlayerCompletionBlock? = nil)Parameters
filenameA string containing the base name of the audio file to play.
fileExtensionA string containing the extension of the audio file to play.
completionBlockA block called after the sound has stopped playing. This block is retained by
JSQSystemSoundPlayer, temporarily cached, and released after its execution. -
Plays a system sound object as an alert corresponding to an audio file with the given filename and extension, and and excutes
completionBlockwhen the sound has stopped playing. The system sound player will lazily initialize and load the file before playing it, and then cache its correspondingSystemSoundID. If this file has previously been played, it will be loaded from cache and played immediately.Note
The fileExtension parameter must be one of
kJSQSystemSoundTypeCAF,kJSQSystemSoundTypeAIF,kJSQSystemSoundTypeAIFF, orkJSQSystemSoundTypeWAV.Warning
If the system sound object cannot be created, this method does nothing.
Warning
This method performs the same functions as
playSoundWithName: extension: completion:, with the excepion that, depending on the particular iOS device, this method may invoke vibration.Declaration
Objective-C
- (void)playAlertSoundWithFilename:(nonnull NSString *)filename fileExtension:(nonnull NSString *)fileExtension completion: (nullable JSQSystemSoundPlayerCompletionBlock) completionBlock;Swift
func playAlertSound(withFilename filename: String, fileExtension: String, completion completionBlock: JSQSystemSoundPlayerCompletionBlock? = nil)Parameters
filenameA string containing the base name of the audio file to play.
fileExtensionA string containing the extension of the audio file to play.
completionBlockA block called after the sound has stopped playing. This block is retained by
JSQSystemSoundPlayer, temporarily cached, and released after its execution. -
Plays the sound that corresponds to the specified
SystemSoundID, optionally as an alert.Declaration
Objective-C
- (void)playSoundWithSoundID:(SystemSoundID)soundID asAlert:(BOOL)asAlert completion:(nullable JSQSystemSoundPlayerCompletionBlock) completionBlock;Swift
func playSound(withSoundID soundID: SystemSoundID, asAlert: Bool, completion completionBlock: JSQSystemSoundPlayerCompletionBlock? = nil)Parameters
soundIDThe SystemSoundID to play.
asAlertSpecifies whether or not the sound should be played as an alert.
completionBlockA block called after the sound has stopped playing. This block is retained by
JSQSystemSoundPlayer, temporarily cached, and released after its execution. -
Stops playing all sounds immediately.
Warning
Any completion blocks attached to any currently playing sound will not be executed. Calling this method will purge allSystemSoundIDobjects from cache, regardless of whether or not they were currently playing.Declaration
Objective-C
- (void)stopAllSounds;Swift
func stopAllSounds() -
Stops playing the sound with the given filename immediately.
Warning
If a completion block is attached to the given sound, it will not be executed. Calling this method will purge the
SystemSoundIDobject for this file from cache, regardless of whether or not it was currently playing.Declaration
Objective-C
- (void)stopSoundWithFilename:(nonnull NSString *)filename;Swift
func stopSound(withFilename filename: String)Parameters
filenameThe filename of the sound to stop playing.
-
Preloads a system sound object corresponding to an audio file with the given filename and extension. The system sound player will initialize, load, and cache the corresponding
SystemSoundID.Note
The fileExtension parameter must be one of
kJSQSystemSoundTypeCAF,kJSQSystemSoundTypeAIF,kJSQSystemSoundTypeAIFF, orkJSQSystemSoundTypeWAV.Declaration
Objective-C
- (void)preloadSoundWithFilename:(nonnull NSString *)filename fileExtension:(nonnull NSString *)fileExtension;Swift
func preloadSound(withFilename filename: String, fileExtension: String)Parameters
filenameA string containing the base name of the audio file to play.
fileExtensionA string containing the extension of the audio file to play.
View on GitHub
JSQSystemSoundPlayer Class Reference