我正在尝试通过AVFoundation的captureStillImageAsynchronouslyFromConnection在摄像头实时预览期间捕获图像。目前程序按预期工作。但是,我该如何消除快门声音?
我正在尝试通过AVFoundation的captureStillImageAsynchronouslyFromConnection在摄像头实时预览期间捕获图像。目前程序按预期工作。但是,我该如何消除快门声音?
我曾经用过这段代码来捕获iOS默认快门声音(这里是声音文件名称列表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];
然后我使用第三方应用程序从文档目录(Mac的DiskAid)中提取了photoShutter.caf
。接下来,我在Audacity音频编辑器中打开了photoShutter.caf
并应用了反转效果,在高放大倍数下看起来像这样:
然后我将这个声音保存为photoShutter2.caf
,并尝试在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:
...
这真的有效!我测试了多次,每次都没有听到快门声音 :)
您可以从此链接获取在 iPhone 5S iOS 7.1.1 上捕获的已反转的声音:https://www.dropbox.com/s/1echsi6ivbb85bv/photoShutter2.caf
当您调用 AVCapturePhotoOutput.capturePhoto
方法捕获图像时,如下面的代码所示。
photoOutput.capturePhoto(with: self.capturePhotoSettings, delegate: self)
AVCapturePhotoCaptureDelegate方法将被调用。
在willCapturePhotoFor
方法被调用后,系统会尝试播放快门声音。
因此,您可以在willCapturePhotoFor
方法中处理系统声音。
extension PhotoCaptureService: AVCapturePhotoCaptureDelegate {
func photoOutput(_ output: AVCapturePhotoOutput, willCapturePhotoFor resolvedSettings: AVCaptureResolvedPhotoSettings) {
// dispose system shutter sound
AudioServicesDisposeSystemSoundID(1108)
}
}
photoOutput
。 - Edward Brey方法1:不确定是否有效,但是在发送捕获事件之前尝试播放空白音频文件。
要播放音频剪辑,请添加Audio Toolbox
框架,#include <AudioToolbox/AudioToolbox.h>
并在拍照之前立即像这样播放音频文件:
NSString *path = [[NSBundle mainBundle] pathForResource:@"blank" ofType:@"wav"];
SystemSoundID soundID;
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
AudioServicesPlaySystemSound(soundID);
如果需要,这里有一个空白音频文件。 https://d1sz9tkli0lfjq.cloudfront.net/items/0Y3Z0A1j1H2r1c0z3n3t/blank.wav
方法2:如果这个不起作用,还有一个替代方案可供选择。只要你不需要高分辨率,就可以从视频流中抓取一帧,以此避免声音和图像的问题。
方法3:另一种方法是截取应用程序的“屏幕截图”。做法如下:
UIGraphicsBeginImageContext(self.window.bounds.size);
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
[data writeToFile:@"foo.png" atomically:YES];
如果您想要让预览视频流充满整个屏幕,以便截图更好看:AVCaptureSession *captureSession = yourcapturesession;
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
UIView *aView = theViewYouWantTheLayerIn;
previewLayer.frame = aView.bounds; // Assume you want the preview layer to fill the view.
[aView.layer addSublayer:previewLayer];
我通过在snapStillImage函数中使用以下代码使其工作,对于我的iOS 8.3 iPhone 5来说完美地工作。我还确认如果您使用此代码,苹果不会拒绝您的应用程序(他们没有拒绝我的)。
MPVolumeView* volumeView = [[MPVolumeView alloc] init];
//find the volumeSlider
UISlider* volumeViewSlider = nil;
for (UIView *view in [volumeView subviews]){
if ([view.class.description isEqualToString:@"MPVolumeSlider"]){
volumeViewSlider = (UISlider*)view;
break;
}
}
// mute it here:
[volumeViewSlider setValue:0.0f animated:YES];
[volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];
请记得在您的应用程序返回时,友好地取消静音!
我住在日本,因为安全原因无法在拍照时静音。然而,在视频中,声音会关闭。我不明白为什么。
我拍摄不带快门声的照片的唯一方法是使用AVCaptureVideoDataOutput或AVCaptureMovieFileOutput。对于分析静态图像,AVCaptureVideoDataOutput是唯一的方法。在AVFoundatation示例代码中,
AVCaptureVideoDataOutput *output = [[[AVCaptureVideoDataOutput alloc] init] autorelease];
// If you wish to cap the frame rate to a known value, such as 15 fps, set
// minFrameDuration.
output.minFrameDuration = CMTimeMake(1, 15);
当我设置CMTimeMake(1, 1); // 每秒一帧时,我的3GS非常卡顿。
在2010年WWDC示例代码FindMyiCone中,我发现了以下代码:
[output setAlwaysDiscardsLateVideoFrames:YES];
当使用此API时,时间不被保证,但是API会按顺序依次调用。我认为这是最好的解决方案。
您还可以从视频流中获取一帧,以捕获(不是全分辨率的)图像。
在此处使用它来短时间间隔捕获图像:
- (IBAction)startStopPictureSequence:(id)sender
{
if (!_capturingSequence)
{
if (!_captureVideoDataOutput)
{
_captureVideoDataOutput = [AVCaptureVideoDataOutput new];
_captureVideoDataOutput.videoSettings = @{(NSString *)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA)};
[_captureVideoDataOutput setSampleBufferDelegate:self
queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)];
if (_sequenceCaptureInterval == 0)
{
_sequenceCaptureInterval = 0.25;
}
}
if ([_captureSession canAddOutput:_captureVideoDataOutput])
{
[_captureSession addOutput:_captureVideoDataOutput];
_lastSequenceCaptureDate = [NSDate date]; // Skip the first image which looks to dark for some reason
_sequenceCaptureOrientation = (_currentDevice.position == AVCaptureDevicePositionFront ? // Set the output orientation only once per sequence
UIImageOrientationLeftMirrored :
UIImageOrientationRight);
_capturingSequence = YES;
}
else
{
NBULogError(@"Can't capture picture sequences here!");
return;
}
}
else
{
[_captureSession removeOutput:_captureVideoDataOutput];
_capturingSequence = NO;
}
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
// Skip capture?
if ([[NSDate date] timeIntervalSinceDate:_lastSequenceCaptureDate] < _sequenceCaptureInterval)
return;
_lastSequenceCaptureDate = [NSDate date];
UIImage * image = [self imageFromSampleBuffer:sampleBuffer];
NBULogInfo(@"Captured image: %@ of size: %@ orientation: %@",
image, NSStringFromCGSize(image.size), @(image.imageOrientation));
// Execute capture block
dispatch_async(dispatch_get_main_queue(), ^
{
if (_captureResultBlock) _captureResultBlock(image, nil);
});
}
- (BOOL)isRecording
{
return _captureMovieOutput.recording;
}