Programing

AVFoundation, captureStillImageAsynchronouslyFromConnection시 셔터 소리를 끄는 방법은 무엇입니까?

lottogame 2020. 9. 12. 11:23
반응형

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];

그런 다음 타사 앱을 사용 photoShutter.caf하여 Documents 디렉토리 (Mac 용 DiskAid)에서 추출 했습니다. 다음 단계 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:
...

그리고 이것은 정말로 작동합니다! 셔터 소리가 들리지 않을 때마다 테스트를 여러 번 실행합니다. :)

다음 링크에서 iPhone 5S iOS 7.1.1에서 캡처 한 이미 반전 된 사운드를 얻을 수 있습니다. https://www.dropbox.com/s/1echsi6ivbb85bv/photoShutter2.caf


방법 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);

필요한 경우 여기에 빈 오디오 파일이 있습니다. 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];

스크린 샷이보기 좋게 보이도록 비디오 스트림의 미리보기로 전체 화면을 채우려면 다음을 수행하십시오.

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];

Swift의 내 솔루션

AVCapturePhotoOutput.capturePhoto아래와 같이 이미지를 캡쳐하는 메소드를 호출 할 때 .

photoOutput.capturePhoto(with: self.capturePhotoSettings, delegate: self)

AVCapturePhotoCaptureDelegate methods will be invoked. And system try to play shutter sound after willCapturePhotoFor invoked. So you can dispose system sound in willCapturePhotoFor method.

여기에 이미지 설명 입력

extension PhotoCaptureService: AVCapturePhotoCaptureDelegate {

    func photoOutput(_ output: AVCapturePhotoOutput, willCapturePhotoFor resolvedSettings: AVCaptureResolvedPhotoSettings) {
        // dispose system shutter sound
        AudioServicesDisposeSystemSoundID(1108)
    }
}

See also


I was able to get this to work by using this code in the snapStillImage function and it works perfectly for me on iOS 8.3 iPhone 5. I have also confirmed that Apple won't reject your app if you use this (they didn't reject mine)

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];

Just remember to be nice and unmute it when your app returns!


I live in in Japan, so I can not mute the audio when we take photos for security reason. In video, however audio turns off. I don't understand why.

The only way I take a photo without shutter sound is using AVCaptureVideoDataOutput or AVCaptureMovieFileOutput. For analyze still image AVCaptureVideoDataOutput is only way. In AVFoundatation sample code,

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);

In my 3GS it is very heavy when I set CMTimeMake(1, 1); // One frame per second.

In WWDC 2010 Sample code, FindMyiCone, I found following code,

[output setAlwaysDiscardsLateVideoFrames:YES];

When this API is used, the timing is not granted, but API is called sequentially. I this it is best solutions.


You can also take a frame from a video stream to capture a (not full resolution) image.

It is used here to capture images at short intervals:

- (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;
}

See this post for a different kind of answer: Capture the image from the image buffer. Screen capture during video preview fails


The only possible work-around I can think of would be to mute the iphone sound when they press the "take picture" button, and then un-mute it a second later.


A common trick in such cases is to find out if the framework invokes a certain method for this event, and then to overwrite that method temporarily, thereby voiding its effect.

I'm sorry but I am not a good enough hack to tell you right away if that works in this case. You could try the "nm" command on the framework executables to see if there's a named function that has a suitatable name, or use gdb with the Simulator to trace where it goes.

무엇을 덮어 쓸지 알면 함수 조회를 리디렉션하는 데 사용할 수있는 저수준 ObjC 디스패치 함수가 있다고 생각합니다. 예전에 해본 것 같은데 세부 사항이 기억 나지 않습니다.

바라건대, 내 힌트를 사용하여 이에 대한 몇 가지 솔루션 경로를 Google에 알릴 수 있습니다. 행운을 빕니다.

참고 URL : https://stackoverflow.com/questions/4401232/avfoundation-how-to-turn-off-the-shutter-sound-when-capturestillimageasynchrono

반응형