ohh*_*hho 88 iphone camera cocoa-touch avfoundation ios
我试图通过AVFoundation captureStillImageAsynchronouslyFromConnection在摄像机的实时预览期间捕获图像.到目前为止,该计划按预期工作.但是,如何将快门声静音?
k06*_*06a 1495
我使用此代码一次捕获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];
Run Code Online (Sandbox Code Playgroud)
然后我使用第三方应用程序photoShutter.caf从Documents目录(DiskAid for Mac)中提取.下一步我photoShutter.caf在Audacity音频编辑器中打开并应用了反转效果,它在高变焦时看起来像这样:

然后我保存了这个声音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:
...
Run Code Online (Sandbox Code Playgroud)
这真的有效!每次听到没有快门声时我都会跑几次测试:)
您可以通过以下链接获取已在iPhone 5S iOS 7.1.1上捕获的已反转声音:https://www.dropbox.com/s/1echsi6ivbb85bv/photoShutter2.caf
sud*_*-rf 22
方法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);
Run Code Online (Sandbox Code Playgroud)
如果需要,这是一个空白的音频文件. http://cl.ly/3cKi
________________________________________________________________________________________________________________________________________
方法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];
Run Code Online (Sandbox Code Playgroud)
如果您希望通过预览视频流来填充整个屏幕,以便您的屏幕截图看起来不错:
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];
Run Code Online (Sandbox Code Playgroud)
Won*_*Won 20
当调用AVCapturePhotoOutput.capturePhoto方法来捕获图像时,如下面的代码所示。
photoOutput.capturePhoto(with: self.capturePhotoSettings, delegate: self)
Run Code Online (Sandbox Code Playgroud)
AVCapturePhotoCaptureDelegate方法将被调用。并且系统在willCapturePhotoFor调用后尝试播放快门声音。这样您就可以在willCapturePhotoFor方法中处置系统声音。
extension PhotoCaptureService: AVCapturePhotoCaptureDelegate {
func photoOutput(_ output: AVCapturePhotoOutput, willCapturePhotoFor resolvedSettings: AVCaptureResolvedPhotoSettings) {
// dispose system shutter sound
AudioServicesDisposeSystemSoundID(1108)
}
}
Run Code Online (Sandbox Code Playgroud)
我可以通过在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];
Run Code Online (Sandbox Code Playgroud)
当你的应用程序返回时,请记住保持良好并取消静音!
小智 5
我住在日本,因此出于安全考虑,我在拍照时无法使音频静音。在视频中,但是音频关闭。我不明白为什么。
我拍摄没有快门声的照片的唯一方法是使用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);
Run Code Online (Sandbox Code Playgroud)
在我的3GS中,设置CMTimeMake(1,1)会很重;//每秒一帧。
在WWDC 2010示例代码FindMyiCone中,我发现以下代码,
[output setAlwaysDiscardsLateVideoFrames:YES];
Run Code Online (Sandbox Code Playgroud)
使用此API时,不会授予时间,而是顺序调用API。我这是最好的解决方案。