63

Is it possible to use the existing Apple system sounds in my own app? I would like to write a sample app in Swift that does the following steps:

  1. Read/Get a list of all available systemSounds on the device (I think they are located in /System/Library/Audio/UISounds/)
  2. show the list on the screen
  3. if I touch a list item, play these sound

So its basically the same, like when you choose a new ringtone on your iPhone.

I think some apps are using this sounds, or have they copied/bought it?

maxmitz
  • 258
  • 1
  • 4
  • 17
TheOnlyXan
  • 631
  • 1
  • 5
  • 3
  • 1
    It's worth noting that `/System/Library/Audio/UISounds/` exists on a physical devices but does not exist in the simulator. – Derek Soike Jan 24 '19 at 01:44

4 Answers4

177

You can use this Swift 5 code to play system sounds:

// import this
import AVFoundation

// create a sound ID, in this case its the tweet sound.
let systemSoundID: SystemSoundID = 1016

// to play sound
AudioServicesPlaySystemSound(systemSoundID)

The most up to date list of sounds I could find are here.

Documentation Reference

And this is what they all sound like: https://www.youtube.com/watch?v=TjmkmIsUEbA

Beau Nouvelle
  • 6,962
  • 3
  • 39
  • 54
  • 3
    Where is that documented by Apple? – Meriw Jun 10 '16 at 18:47
  • Quick google search for AudioServicesPlaySystemSound will lead you there. However, I have added it to my answer. – Beau Nouvelle Jun 11 '16 at 09:51
  • 7
    Are there Apple guidelines on when developers can use iOS system sounds? – Dave G Mar 30 '17 at 12:31
  • 2
    If you import `AVFoundation` you can do the rest in one line: `AudioServicesPlaySystemSound(SystemSoundID(1016))` – peacetype Dec 14 '17 at 11:44
  • 1
    I noticed that this is not super reliable... I call this code and also print to the console at the same time. Often it will print to the console but the sound will not play. Seems to fail at random. – peacetype Dec 14 '17 at 11:46
  • 3
    According to the documentation for AudioServicesPlaySystemSound: "Before using this function, call the AudioServicesCreateSystemSoundID(_:_:) function to obtain a system sound." Also mentioned on AudioServicesPlayAlertSound: "System-supplied alert sounds and system-supplied user-interface sound effects are not available to your iOS application." – biomiker May 06 '20 at 13:44
  • While this is a simple way to play system sounds, it is not as reliable as using other methods and comes with a wide array of limitations based on [Apples Docs](https://developer.apple.com/documentation/audiotoolbox/1405248-audioservicesplaysystemsound) such as no volume control and sounds won't play if the systems audio services are in use already. Search around and you will find that AVAudioPlayer is a more flexible playback of system sounds. – kenjikato Mar 03 '22 at 10:41
  • @kenjikato This is quite a popular question. If you know of a better way to play these sounds, please take the time to submit an answer. – Beau Nouvelle Mar 04 '22 at 08:36
24

Swift 4

import AVFoundation

AudioServicesPlayAlertSound(SystemSoundID(1322))
Neha
  • 666
  • 1
  • 10
  • 17
10

Here's a quick way to test the sounds.

import AVFoundation

func displaySoundsAlert() {
    let alert = UIAlertController(title: "Play Sound", message: nil, preferredStyle: UIAlertController.Style.alert)
    for i in 1000...1010 {
        alert.addAction(UIAlertAction(title: "\(i)", style: .default, handler: {_ in
            AudioServicesPlayAlertSound(UInt32(i))
            self.displaySoundsAlert()
        }))
    }
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    self.present(alert, animated: true, completion: nil)
}
Derek Soike
  • 11,238
  • 3
  • 79
  • 74
0

for a list of ID (as usual, don't use hardcoded values):

https://developer.apple.com/documentation/audiotoolbox/audio_services/1618202-alert_sound_identifiers

and use version with completion, as apple says:

This function will be deprecated in a future release. Use AudioServicesPlayAlertSoundWithCompletion or AudioServicesPlaySystemSoundWithCompletion instead.

so:

AudioServicesPlaySystemSoundWithCompletion(kSystemSoundID_Vibrate, nil)
ingconti
  • 10,876
  • 3
  • 61
  • 48