Play System Sound Notification with AudtioToolbox in Swift 4

Category: Swift
Play System Sound Notification with AudtioToolbox in Swift 4

In this article we will cover how to play a system sound in iOS application using AudioToolbox. We will first play Apple’s built in system sound and then we’ll add our custom sound. We will use a beep sound.

There have been several ways to approach this case - some use AVFoundation and some use AudioToolbox. Well we could use AVFoundation but we only play a very short sound, we don’t need to control it. You might also want to make the device to vibrate. All of this can be simply done by AudioToolbox.

First you need to import AudioToolbox.
We need AudioServicesPlaySystemSound function that 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 AudioServicesAddSystemSoundCompletion(_:_:_:_:_:) function to register a callback function. Please take a look at the docs for more detailed information.

Apple has assigned numbers to specific notification sounds in the system. Here you can find the list of all system sounds available in iOS: iPhoneDevWiki or iOSSystemSoundsLibrary.

The code below displays how to play Apple’s built in system sound:

let systemSoundId: SystemSoundID = 1016
AudioServicesPlaySystemSound(customSoundId)

With the first line we assign systemSoundId a number from the list of available sounds and then we play the sound.

NOTE: You need to note that there are two functions that can play system sound. Depending on the particular iOS device, AudioServicesPlayAlertSound function plays a short sound and may invoke vibration. AudioServicesPlaySystemSound will only play system sound without vibration.

Now it’s time to load our custom sound. Simply drag and drop the file into Project Navigator window and mark copy if needed. In my case I created a folder assets and dropped the file in it.

In order to play our sound we first need to create it by using AudioServicesCreateSystemSoundID(_:_:) function. It creates a system sound object. We need to pass two parameters inFileURL that will take the location of our file and outSystemSoundID is the number we have to assign to our sound.

Apple docs say that we need to dispose a system sound object and associated resources after we finished playing it and create again next time. We need to use AudioServicesDisposeSystemSoundID function and pass the ID of system sound object to dispose.

In order to find out when playing the sound has finished we are going to use AudioServicesAddSystemSoundCompletion function. It registers a callback function that is invoked when a specified system sound finishes playing. Take a look at documentation for this function to get a better understanding of the parameters that are being passed.

The completion callback function is a closure. It takes two parameters. We need only one parameter SystemSoundID to pass the ID to AudioServicesDisposeSystemSoundID. We don’t need the other parameter. Hence we place underscore _.

The other use case for this function might be: 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.

The whole code for playing system sound looks like this:

// Play system sound with custom mp3 file
if let customSoundUrl = Bundle.main.url(forResource: "beep-07", withExtension: "mp3") {
    var customSoundId: SystemSoundID = 0
    AudioServicesCreateSystemSoundID(customSoundUrl as CFURL, &customSoundId)
    //let systemSoundId: SystemSoundID = 1016  // to play apple's built in sound, no need for upper 3 lines
            
    AudioServicesAddSystemSoundCompletion(customSoundId, nil, nil, { (customSoundId, _) -> Void in
        AudioServicesDisposeSystemSoundID(customSoundId)
    }, nil)
            
    AudioServicesPlaySystemSound(customSoundId)
}

Conclusion

So that’s it! Now you can play sound alerts in your application. I hope you found it helpful. In case of any questions, wishes, criticism please don’t hesitate to send me an email and follow me on Twitter. Cheers!