Need to access the default camera shutter sound used by the iPhone
Asked Answered
R

2

20

Right now when a user presses a button I play a custom camera shutter sound.

However, if possible, I would much rather just use the camera shutter sound that plays by default whenever you take a photo using your iPhone.

Is there a way that I can access and use the default iPhone camera shutter sound? And if yes, then where is it actually located?

I need to figure out it's file path so that I can use it with the code below and just change the input for pathForResource:

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"shutter" ofType: @"wav"];

    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath ];

    self.myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];

    [self.myAudioPlayer play];

Thanks for the help.

Rotherham answered 3/4, 2014 at 22:13 Comment(0)
C
39
AudioServicesPlaySystemSound(1108);

Based on: Are there any other iOS system sounds available other than 'tock'? and http://iphonedevwiki.net/index.php/AudioServices

For 2016 ....

    if #available(iOS 9.0, *) {
        AudioServicesPlaySystemSoundWithCompletion(SystemSoundID(1108), nil)
    } else {
        AudioServicesPlaySystemSound(1108)
    }
Cod answered 4/4, 2014 at 1:7 Comment(2)
From iOS9, AudioServicesPlaySystemSoundWithCompletion is preferred - the header comments for AudioServicesPlaySystemSound indicate it will be deprecated.Silvanus
Note that you need to import AVKit to get access to these methodsCalhoun
R
6

Looking at stevesliva's answer helped me greatly. However, I ended up with this:

Unexpected type name 'SystemSoundID': expected expression

... using this line...

AudioServicesPlaySystemSoundWithCompletion(SystemSoundID(1108), nil);

So, using this lump of code got my App playing the camera-shutter noise:

SystemSoundID soundID = 1108;

AudioServicesPlaySystemSoundWithCompletion(soundID, ^{
    AudioServicesDisposeSystemSoundID(soundID);
});

Hope this helps someone.

Romney answered 2/3, 2018 at 11:24 Comment(3)
That's because the code you quoted is Swift. Did you even look at the ObjC/C API? It should be AudioServicesPlaySystemSoundWithCompletion((SystemSoundID)1108, nil);Mondrian
The OP question is in Obj-C. The accepted answer is in Swift - I thought this odd. I added my comment hoping my actual Obj-C response would help someone.Romney
The answer is in Obj C, and has been edited with Swift.Mondrian

© 2022 - 2024 — McMap. All rights reserved.