49

Is it possible to play already existing system sounds without importing your own?

Oksana
  • 13,442
  • 10
  • 53
  • 89
  • 2
    Xcode also bundles simulator iOS system files within itself. This is different from `~/Library/Developer/CoreSimulator` (user space files only) System files are here: `/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Audio/UISounds` – stringCode Sep 29 '18 at 19:08

11 Answers11

57

I find this list of systemSoundID very useful for accessing the sound ID directly.
http://iphonedevwiki.net/index.php/AudioServices

For example, to play a key press tock sound.

#define systemSoundID    1104
AudioServicesPlaySystemSound (systemSoundID);

You'll also need to add the AudioToolbox framework in your project, and add #include <AudioToolbox.h> to your .m or .h file.

Stan James
  • 2,535
  • 1
  • 28
  • 35
yannc2021
  • 681
  • 5
  • 7
42

This code plays apple system sound "Tock.aiff"..I believe you can play different system sounds using this

NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:@"Tock" ofType:@"aiff"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound(soundID);
AudioServicesDisposeSystemSoundID(soundID);

See this thread

Apple's documentation on System sounds

https://developer.apple.com/documentation/audiotoolbox/system_sound_services

Community
  • 1
  • 1
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
  • 6
    In the linked example project, it states explicitly in the read me that there are no system sounds available (in ios4). I don't think using a named file from the uikit bundle is a great idea as it could be changed at any time. The idea in the linked thread of extracting the sound from the simulator bundle and including it in your own bundle is better, if still slightly shaky. – jrturton Oct 20 '11 at 06:28
  • Works great! Just check for nil so that you don't crash. – mxcl Nov 13 '11 at 14:08
  • 8
    don't "dispose" of the soundID right away, you wouldn't hear anything, (atleast when i did it) you have to dispose of it latter... possibly if appropriate in the dealloc or viewDidUnload. – hokkuk Apr 26 '12 at 16:19
  • The 3rd line gives this error: "Cast of Objective-C pointer type 'id' to C pointer type CFURLRef' (aka 'const struct_CFURL *') requires a bridged cast". I found the solution here: iosdeveloperzone.com/2012/07/28/… (its a ARC thing. you have to change the (CFURLRef) to (__bridge CFURLRef)). But the "problem" is that this way you only get 3 sounds. Tink.aiff, Tock.aiff and iPod Click.aiff. Any ideas on how to access the (other) 14 ones, available at the Settings->Sound->Text Tone (for example)? – Gik Nov 02 '12 at 13:40
  • 3
    This answer did not work for me in ios 7. You might have a look at this: https://github.com/TUNER88/iOSSystemSoundsLibrary – Ursin Brunner Jun 03 '14 at 18:38
  • Just remove this line (last one) AudioServicesDisposeSystemSoundID(soundID); – Jay Gajjar Jul 14 '15 at 12:48
  • Here is a Swift 3/4 sample project that lets you play all available sounds on an iOS device: https://github.com/klaas/SwiftySystemSounds – Klaas Jul 12 '17 at 14:11
  • url is invalid. Please fix it – fnc12 Sep 18 '17 at 09:23
  • URL seems ok now. –  Jan 02 '19 at 23:13
32

You can use this for all default system audio.

Example, for the tap sound user this:

AudioServicesPlaySystemSound(1104);

For positive sounds, use this:

 AudioServicesPlaySystemSound(1054);

And, negative sounds use this:

 AudioServicesPlaySystemSound(1053);

The complete list you can see here.

Arnab Nandy
  • 6,472
  • 5
  • 44
  • 50
Matheus Veloza
  • 492
  • 5
  • 4
18

List of all system sounds: iOSSystemSoundsLibrary

After you import AVKit, you can play all this sounds with:

AudioServicesPlaySystemSound (systemSoundID);
mfaani
  • 33,269
  • 19
  • 164
  • 293
TUNER88
  • 923
  • 1
  • 20
  • 36
  • 1
    Can my app be banned from AppStore if I use this sounds? It's not in public library so it may be tricky for review. – Josip B. Mar 05 '14 at 10:20
  • 1
    @Josip B. I never got review-problems with this code – TUNER88 Mar 05 '14 at 10:45
  • @DaniSpringer, I have made a swift version based on TUNER88's Objective C version. It's updated: https://github.com/CQH/iOS-Sounds-and-Ringtones – CQH Jun 25 '18 at 21:02
  • @CQH not sure why I never got notified of your reply :/ I have since forked and used your repo. It does seem like a bunch of stuff is missing, or at least I cannot find the Sound ID for many of the filenames. Thanks! –  Jan 02 '19 at 23:17
6

adapted from @yannc2021

http://iphonedevwiki.net/index.php/AudioServices

if you want to use system sound in Swift

// import this
import AVFoundation

// add this method
required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

// declared system sound here
let systemSoundID: SystemSoundID = 1104

// to play sound
AudioServicesPlaySystemSound (systemSoundID)
Sruit A.Suk
  • 7,073
  • 7
  • 61
  • 71
6

Swift 4 +

NOTE: Try on real device only:

import AVKit
AudioServicesPlaySystemSound(1007);

Or you can try with URL as -

let url = URL(fileURLWithPath: "/System/Library/Audio/UISounds/payment_success.caf")
var soundID: SystemSoundID = 0
AudioServicesCreateSystemSoundID(url as CFURL, &soundID)
AudioServicesPlaySystemSound(soundID);

https://github.com/klaas/SwiftySystemSounds/blob/master/README.md

Jack
  • 13,571
  • 6
  • 76
  • 98
4

For swift, you can have a look at complete list of system sounds and ringtones example.

Edit: Ok, here are the most important peaces of code from this example:

    ///The directories where sound files are located.
    let rootSoundDirectories: [String] = ["/Library/Ringtones", "/System/Library/Audio/UISounds"]

    ///Array to hold directories when we find them.
    var directories: [String] = []

    ///Tuple to hold directories and an array of file names within.
    var soundFiles: [(directory: String, files: [String])] = []

    //Starting with the "/Library/Ringtones" & "/System/Library/Audio/UISounds" directories, it looks for other sub-directories just one level lower and saves their relative path in directories array.
    //- URLs: All of the contents of the directory (files and sub-directories).
    func getDirectories() {
        let fileManager: NSFileManager = NSFileManager()
        for directory in rootSoundDirectories {
            let directoryURL: NSURL = NSURL(fileURLWithPath: "\(directory)", isDirectory: true)

            do {
                if let URLs: [NSURL] = try fileManager.contentsOfDirectoryAtURL(directoryURL, includingPropertiesForKeys: [NSURLIsDirectoryKey], options: NSDirectoryEnumerationOptions()) {
                    var urlIsaDirectory: ObjCBool = ObjCBool(false)
                    for url in URLs {
                        if fileManager.fileExistsAtPath(url.path!, isDirectory: &urlIsaDirectory) {
                            if urlIsaDirectory {
                                let directory: String = "\(url.relativePath!)"
                                let files: [String] = []
                                let newSoundFile: (directory: String, files: [String]) = (directory, files)
                                directories.append("\(directory)")
                                soundFiles.append(newSoundFile)
                            }
                        }
                    }
                }
            } catch {
                debugPrint("\(error)")
            }
        }
    }

    //For each directory, it looks at each item (file or directory) and only appends the sound files to the soundfiles[i]files array.
    //- URLs: All of the contents of the directory (files and sub-directories).
        func loadSoundFiles() {
            for i in 0...directories.count-1 {
                let fileManager: NSFileManager = NSFileManager()
                let directoryURL: NSURL = NSURL(fileURLWithPath: directories[i], isDirectory: true)

                do {
                    if let URLs: [NSURL] = try fileManager.contentsOfDirectoryAtURL(directoryURL, includingPropertiesForKeys: [NSURLIsDirectoryKey], options: NSDirectoryEnumerationOptions()) {
                        var urlIsaDirectory: ObjCBool = ObjCBool(false)
                        for url in URLs {
                            if fileManager.fileExistsAtPath(url.path!, isDirectory: &urlIsaDirectory) {
                                if !urlIsaDirectory {
                                    soundFiles[i].files.append("\(url.lastPathComponent!)")
                                }
                            }
                        }
                    }
                } catch {
                    debugPrint("\(error)")
                }
            }
        }

The example shows the system sound files in a table view. Sounds are played as shown in this function:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        //Play the sound
        let directory: String = soundFiles[indexPath.section].directory
        let fileName: String = soundFiles[indexPath.section].files[indexPath.row]
        let fileURL: NSURL = NSURL(fileURLWithPath: "\(directory)/\(fileName)")
        do {
            model.audioPlayer = try AVAudioPlayer(contentsOfURL: fileURL)
            model.audioPlayer.play()
        } catch {
            debugPrint("\(error)")
        }
    }

Where model.audioPlayer is just an instance of AVAudioPlayer:

///Audio player responsible for playing sound files.
var audioPlayer: AVAudioPlayer = AVAudioPlayer()
Despotovic
  • 1,807
  • 2
  • 20
  • 24
  • 1
    Glad you like the repo. I hope it helps people. I just updated it to Swift 3 and iOS 10. – CQH Oct 24 '16 at 04:17
  • @CQH may you add text message sounds? Like for example "Circles"? –  Jun 24 '18 at 00:59
  • 1
    Hi, @DaniSpringer. Unfortunately, I can't get a handle on the alert sounds folder. Any help will be appreciated. – CQH Jun 25 '18 at 21:10
2

This cocoa-only solution uses existing audio files to play sounds. This method can be used to play any sound file. AVFoundation.framework will have to be added to your frameworks. You will have to define or remove the macros I use which are self explanatory.

I added a category to AVAudioPlayer as follows:

AVAudioPlayer+.h

 #import <AVFoundation/AVAudioPlayer.h>

@interface AVAudioPlayer ( CapSpecs )
+ (AVAudioPlayer*) click;
+ (AVAudioPlayer*) tink;
+ (AVAudioPlayer*) tock;
+ (AVAudioPlayer*) withResourceName: (NSString*) aName;
@end

AVAudioPlayer+.m

 #import "AVAudioPlayer+.h"

@implementation AVAudioPlayer ( CapSpecs )
+ (AVAudioPlayer*) click {
    StaticReturn ( [AVAudioPlayer withResourceName: @"iPod Click"] );
}
+ (AVAudioPlayer*) tink {
    StaticReturn ( [AVAudioPlayer withResourceName: @"Tink"] );
}
+ (AVAudioPlayer*) tock {
    StaticReturn ( [AVAudioPlayer withResourceName: @"Tock"] );
}
+ (AVAudioPlayer*) withResourceName: (NSString*) aName {
    NSBundle* zBundle = [NSBundle bundleWithIdentifier: @"com.apple.UIKit"];
    NSURL* zURL = [zBundle URLForResource: aName withExtension: @"aiff"];
    (void) RaiseIfNil ( nil, zURL, ([SWF @"URL for %@",aName]) );
    NSError* zError = nil;
    AVAudioPlayer* zAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: zURL error: &zError];
    RaiseError ( nil, zError, @"AVAudioPlayer init error" );

#ifdef DEBUG
    //  Apple records the console dump which occurs as a bug in the iOS simulator
    //  all of the following commented code causes the BS console dump to be hidden
    int zOldConsole = dup(STDERR_FILENO);   // record the old console
    freopen("/dev/null", "a+", stderr); // send console output to nowhere
    (void)[zAudio prepareToPlay];       // create the BS dump
    fflush(stderr);             // flush the console output
    dup2(zOldConsole, STDERR_FILENO);   // restore the console output
#endif

    return zAudio;
}
@end
carmin
  • 421
  • 3
  • 5
0

For Swift

import AVFoundation

func play(sound: String) {
        var soundID: SystemSoundID = SystemSoundID()
        let mainBundle = CFBundleGetMainBundle()
        if let ref = CFBundleCopyResourceURL(mainBundle, sound as CFString, nil, nil) {
            AudioServicesCreateSystemSoundID(ref, &soundID);
            AudioServicesPlaySystemSound(soundID);
        }
}

the implementation of @Krishnabhadra

alecnash
  • 1,750
  • 1
  • 18
  • 42
0

This should play most macOS system sounds

#include <AudioToolbox/AudioToolbox.h>


for (int i = 0; i < 50; i++) {
    NSLog(@"playing %i", i);
    AudioServicesPlaySystemSound (i);
    [NSThread sleepForTimeInterval:1.0];
}

Matt Andrzejczuk
  • 2,001
  • 9
  • 36
  • 51
0
extension NSSound {
    
    static var systemSoundNames = {
        return FileManager.default.urls(for: .libraryDirectory, in: .allDomainsMask).flatMap { libraryUrl in
            return ((try? FileManager.default.contentsOfDirectory(at: libraryUrl.appendingPathComponent("Sounds"), includingPropertiesForKeys: nil)) ?? []).compactMap { soundUrl -> String? in
                return NSSound(named: soundUrl.deletingPathExtension().lastPathComponent)?.name
            }
        }.sorted()
    }()
    
}
Nickkk
  • 2,261
  • 1
  • 25
  • 34