UI Sounds

4 minutes, 37 seconds

Sounds are used minimally in apps, but when they’re played, they can transform a mundane action into a truly rewarding experience. People can recognize your app without even seeing it. When the sound gives the right impression, you win a lot with a small amount of effort.

You’ll learn how to find great sounds and when to use them for a maximum effectiveness.

I love the expression “humanizing design”. We design things that makes us more human, experiences that makes us feel and use our senses. So far we’ve achieved 3 out of 5 senses: touch, see and hear. The third one is as important as the rest. That’s why we should make use of it as best as we can.

Where to Use Sounds

Unless you’re making a game, you shouldn’t use sounds everywhere. Not every user has their sound turned on. There are a few key places that you should place your sounds to enhance your app rather than annoy.

Notifications

Notifications in iOS come with a default sound. That sound can be customized to fit the branding of your app. Since you’re simply replacing one sound by another, you are absolutely encouraged to do this.

Positive Actions

Anything that requires a bit of effort, but that is rewarding or beneficial to the user. Some examples are Liking something, following someone, creating a task, etc. A fun animation and whimsical sound should be played for those cases.

Destructive Actions

In a similar fashion, the destructive actions give confirmation that something was removed. In some circumstances, people take pleasure in deleting things as it makes them feel in control.

AudioToolbox

Play as System Sound

This is the snippet to play system sound, which mean it respect to silent mode if that user choose.

Play custom sound with your own file. Make sure the lenght is less than 30 seconds.


let filePath = Bundle.main.path(forResource: "filename", ofType: "aif")
if let url = URL(fileURLWithPath: filePath!) as URL?{
    var soundID : SystemSoundID = 0
    AudioServicesCreateSystemSoundID(url as CFURL, &soundID)
    AudioServicesPlayAlertSound(soundID)
}

Or, use default system sound that you can use.


let systemSoundID: SystemSoundID = 1016
AudioServicesPlayAlertSound(systemSoundID)

Please refert to this link for list of the system sound.

Vibration


AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)

AVFoundation

Use thie method to play longer sound file. It’s also respect silent mode as default.


import AVFoundation

// declare first
var mySound: AVAudioPlayer?

// initialize the sound at viewDidLoad
mySound = setupAudioPlayer(filename: "filename", type: "aif")

// code to get initialize AVAudioPlayer
func setupAudioPlayer(filename: String, type: String) -> AVAudioPlayer? {
    
    let path = Bundle.main.path(forResource:filename, ofType:type)
    let url = URL.init(fileURLWithPath: path!)
    var audioPlayer: AVAudioPlayer?
    do {
        try audioPlayer = AVAudioPlayer(contentsOf: url)
    } catch {
        print("Player not available")
    }
    return audioPlayer

}

// When you are ready to play the sound, wheather at IBAction
@IBAction func playSound(_ sender: Any) {
    mySound?.play()
}

Play On Silent Mode

Add this line of codes at viewDidLoad to make the audio session declared as audio playback which is going to play even user set is silent. This should be used for item that explicitly known by user to play audio.


do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
    //print("AVAudioSession Category Playback OK")
    do {
        try AVAudioSession.sharedInstance().setActive(true)
        //print("AVAudioSession is Active")
    } catch _ as NSError {
        //print(error.localizedDescription)
    }
} catch _ as NSError {
    //print(error.localizedDescription)
}

Where to Find Good Sounds

Sonics
DevTones
FreeSound
Raised Beaches

  • joe_han

    need to import AVFoundation

  • joe_han

    use AudioServicesPlaySystemSound(soundID) this to not making vibration instead of AudioServicesPlayAlertSound(soundID).