Last active
November 16, 2018 04:16
-
-
Save hirohitokato/6235b668829313d3478c to your computer and use it in GitHub Desktop.
How to mute a photo shutter sound.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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) { }]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@hirohitokato: Can you please upload sound file? Thanks!