我正在尝试通过 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];
然后我使用第三方应用程序从 Documents 目录(Mac 的 DiskAid)中提取 photoShutter.caf
。下一步我在 Audacity 音频编辑器中打开 photoShutter.caf
并应用了反转效果,它在高缩放时看起来像这样:
https://i.stack.imgur.com/mzFez.png
然后我将此声音保存为 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
我在 Swift 中的解决方案
当您调用 AVCapturePhotoOutput.capturePhoto
方法捕获图像时,如下代码所示。
photoOutput.capturePhoto(with: self.capturePhotoSettings, delegate: self)
将调用 AVCapturePhotoCaptureDelegate 方法。并且系统在调用 willCapturePhotoFor
后尝试播放快门声音。因此,您可以在 willCapturePhotoFor
方法中处理系统声音。
https://i.stack.imgur.com/ZkT8g.png
extension PhotoCaptureService: AVCapturePhotoCaptureDelegate {
func photoOutput(_ output: AVCapturePhotoOutput, willCapturePhotoFor resolvedSettings: AVCaptureResolvedPhotoSettings) {
// dispose system shutter sound
AudioServicesDisposeSystemSoundID(1108)
}
}
也可以看看
AVCapturePhotoDelegate
photoOutput
。
方法 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: 如果这不起作用,还有一个替代方法。只要你不需要很好的分辨率,你可以grab a frame from the video stream,这样就完全避免了画面声音。
______________________________________________________________________________________________________________________________
方法 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 上非常适合我。我还确认,如果您使用它,Apple 不会拒绝您的应用程序(他们没有拒绝矿)
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 是唯一的方法。在 AVFoundation 示例代码中,
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);
在我的 3GS 中,当我设置 CMTimeMake(1, 1); 时它非常重。 // 每秒一帧。
在 WWDC 2010 示例代码 FindMyiCone 中,我找到了以下代码,
[output setAlwaysDiscardsLateVideoFrames:YES];
使用此 API 时,不授予计时,而是按顺序调用 API。我这是最好的解决方案。
您还可以从视频流中获取一帧以捕获(非全分辨率)图像。
它用于 here 以短间隔捕获图像:
- (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;
}
我能想到的唯一可能的解决方法是在他们按下“拍照”按钮时将 iphone 声音静音,然后在一秒钟后取消静音。
在这种情况下,一个常见的技巧是找出框架是否为此事件调用了某个方法,然后临时覆盖该方法,从而使其无效。
我很抱歉,但如果这在这种情况下有效,我不是一个足够好的黑客来立即告诉你。您可以在框架可执行文件上尝试“nm”命令以查看是否存在具有合适名称的命名函数,或者使用 gdb 和模拟器来跟踪它的去向。
一旦你知道要覆盖什么,我相信你可以使用那些低级的 ObjC 调度函数来重定向函数的查找。我想我以前做过一次,但不记得细节了。
希望您可以使用我的提示搜索一些解决方案路径。祝你好运。