AVFoundation, how to turn off the shutter sound when captureStillImageAsynchronouslyFromConnection?
I used this code once to capture iOS default shutter sound (here is list of sound file names https://github.com/TUNER88/iOSSystemSoundsLibrary):
NSString *path = @"/System/Library/Audio/UISounds/photoShutter.caf";
NSString *docs = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSData *data = [NSData dataWithContentsOfFile:path];
[data writeToFile:[docs stringByAppendingPathComponent:@"photoShutter.caf"] atomically:YES];
Then I used third-party app to extract photoShutter.caf
from Documents directory (DiskAid for Mac). Next step I opened photoShutter.caf
in Audacity audio editor and applied inversion effect, it looks like this on high zoom:
Then I saved this sound as photoShutter2.caf
and tried to play this sound right before captureStillImageAsynchronouslyFromConnection
:
static SystemSoundID soundID = 0;
if (soundID == 0) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"photoShutter2" ofType:@"caf"];
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
}
AudioServicesPlaySystemSound(soundID);
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:
...
And this really works! I runs test several times, every time I hear no shutter sound :)
You can get already inverted sound, captured on iPhone 5S iOS 7.1.1 from this link: https://www.dropbox.com/s/1echsi6ivbb85bv/photoShutter2.caf
How to mute the capture sound in AVFoundation?
i found the code to do it here..
http://www.benjaminloulier.com/articles/ios4-and-direct-access-to-the-camera
Important parts outlined..
Setup Your Session Like this
-(void)initialize_and_Start_Session_without_CaptureSound
{
/*We setup the input*/
AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput
deviceInputWithDevice:[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]
error:nil];
/*We setupt the output*/
AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];
/*While a frame is processes in -captureOutput:didOutputSampleBuffer:fromConnection: delegate methods no other frames are added in the queue.
If you don't want this behaviour set the property to NO */
captureOutput.alwaysDiscardsLateVideoFrames = YES;
/*We specify a minimum duration for each frame (play with this settings to avoid having too many frames waiting
in the queue because it can cause memory issues). It is similar to the inverse of the maximum framerate.
In this example we set a min frame duration of 1/10 seconds so a maximum framerate of 10fps. We say that
we are not able to process more than 10 frames per second.*/
//captureOutput.minFrameDuration = CMTimeMake(1, 10);
/*We create a serial queue to handle the processing of our frames*/
dispatch_queue_t queue;
queue = dispatch_queue_create("cameraQueue", NULL);
[captureOutput setSampleBufferDelegate:self queue:queue];
dispatch_release(queue);
// Set the video output to store frame in BGRA (It is supposed to be faster)
NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
[captureOutput setVideoSettings:videoSettings];
/*And we create a capture session*/
self.session = [[AVCaptureSession alloc] init];
/*We add input and output*/
[self.session addInput:captureInput];
[self.session addOutput:captureOutput];
/*We start the capture*/
[self.session startRunning];
}
You will get the camera output in the following method..
I make a image and add it to my parent View..You can change it to your need
#pragma mark AVCaptureSession delegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
/*We create an autorelease pool because as we are not in the main_queue our code is
not executed in the main thread. So we have to create an autorelease pool for the thread we are in*/
if (captureImageNow)
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
/*Lock the image buffer*/
CVPixelBufferLockBaseAddress(imageBuffer,0);
/*Get information about the image*/
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
/*Create a CGImageRef from the CVImageBufferRef*/
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef newImage = CGBitmapContextCreateImage(newContext);
/*We release some components*/
CGContextRelease(newContext);
CGColorSpaceRelease(colorSpace);
/*We display the result on the custom layer. All the display stuff must be done in the main thread because
UIKit is no thread safe, and as we are not in the main thread (remember we didn't use the main_queue)
we use performSelectorOnMainThread to call our CALayer and tell it to display the CGImage.*/
/*We display the result on the image view (We need to change the orientation of the image so that the video is displayed correctly).
Same thing as for the CALayer we are not in the main thread so ...*/
self.captureImage = [UIImage imageWithCGImage:newImage scale:1.0 orientation:UIImageOrientationRight];
/*We relase the CGImageRef*/
CGImageRelease(newImage);
[self performSelectorOnMainThread:@selector(AddImageToParentView) withObject:nil waitUntilDone:YES];
/*We unlock the image buffer*/
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
[pool drain];
captureImageNow = NO;
}
}
I wish i could mute shutter sound of camera using ios sdk
No. There is no way to programatically mute the shutter sound when capturing a still image. In most territories, muting the device in hardware (sliding the Mute button across) will mute the shutter sound, but not all—in Japan, for example it won't, because of legal restrictions.
Muting AVCapture shutter sound on iPhone
You have to use an AVCaptureVideoDataOutput
if you want to avoid the shutter sound. Note that covertly capturing images is against App Store policy. You need to make sure you user is told in some way that your taking a picture.
Related Topics
iOS Perform Action After Period of Inactivity (No User Interaction)
How to Call Gesture Tap on Uiview Programmatically in Swift
Swift Apply .Uppercasestring to Only the First Letter of a String
How to Convert My Device Token (Nsdata) into an Nsstring
Wait Until Multiple Networking Requests Have All Executed - Including Their Completion Blocks
Given a View, How to Get Its Viewcontroller
Determine If the Access to Photo Library Is Set or Not - PHPhotolibrary
Adding a Uilabel to a Uitoolbar
How to Set the Height of Tableheaderview (Uitableview) with Autolayout
How to Fix "No Valid 'Aps-Environment' Entitlement String Found for Application" in Xcode 4.3
How to Add a Toolbar Above the Keyboard
Getting User Location Every N Minutes After App Goes to Background
Uialertview First Deprecated iOS 9
Presentviewcontroller:Animated:Yes View Will Not Appear Until User Taps Again