Skip to content

Instantly share code, notes, and snippets.

@hirohitokato
Last active November 16, 2018 04:16
Show Gist options
  • Save hirohitokato/6235b668829313d3478c to your computer and use it in GitHub Desktop.
Save hirohitokato/6235b668829313d3478c to your computer and use it in GitHub Desktop.
How to mute a photo shutter sound.
/*
Mute a shutter sound.
In detail, see http://stackoverflow.com/questions/4401232/avfoundation-how-to-turn-off-the-shutter-sound-when-capturestillimageasynchrono
*/
- (void)becomeSilentModeForCaptureOutput:(AVCaptureStillImageOutput *)imageOutput
{
if (!self.invertedSound) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"photoShutter_inverted"
ofType:@"wav"];
NSURL *fileURL = [NSURL fileURLWithPath:path isDirectory:NO];
self.invertedSound = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL
error:NULL];
[self.invertedSound prepareToPlay];
[imageOutput addObserver:self
forKeyPath:@"capturingStillImage"
options:0 context:NULL];
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if (!self.invertedSound.isPlaying) {
[self.invertedSound play];
}
}
- (void)takePicture
{
AVCaptureConnection *connection = [self.imageOutput connectionWithMediaType:AVMediaTypeVideo];
[self.imageOutput captureStillImageAsynchronouslyFromConnection:connection
completionHandler:
^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { }];
}
@LinhTN-AltPlus
Copy link

@hirohitokato: Can you please upload sound file? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment