2011-04-18 2 views
6

비디오 캡처 및 저장 기능이있는 앱을 만들었습니다. 저는 AVFoundation을 사용했고 Apple's AVCam은 제 가이드입니다.AVFoundation이있는 카메라 스위치에서 비디오가 동결됩니다.

는 나는 그것을 명확하게 할 수 있기를 바랍니다 : 나는 (AVCamViewController 될 것이라고 AVCam에서) 처음 AVCamCaptureManager을 처리하는 videoViewController을 해제 할 때까지
모든 것이 잘 작동합니다. 그런 다음 다시 만들면 카메라 전환 직후에 비디오가 정지됩니다. 다시 실행해도 도움이되지 않거나 치료 또는 기기 재설정이되지 않습니다. (때때로 사물 중 하나가 도움이되지만 그것은 규칙이 아닙니다.)

메모리를 절약 할 필요가 없을 때 videoViewController를 출시했습니다.

코드 카메라를 전환하는 것은 기본적으로 AVCam와 동일하다를 위해 : 나는 단순히 떠날 것입니다

[self.videoViewController.view removeFromSuperview]; 
self.videoViewController = nil; 

내 현재의 "해결 방법"VideoView를 지켜 보면서을 기각있는

NSError *error; 
AVCaptureDeviceInput *newVideoInput; 
AVCaptureDevicePosition position = currentVideoInput.device.position; 

if (position == AVCaptureDevicePositionBack) 
    newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:frontFacingCamera error:&error]; 
else if (position == AVCaptureDevicePositionFront) 
    newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:backFacingCamera error:&error]; 

if (newVideoInput != nil) { 
    [session beginConfiguration]; 
    [session removeInput:currentVideoInput]; 
    if ([session canAddInput:newVideoInput]) { 
     [session addInput:newVideoInput]; 
     [self setVideoInput:newVideoInput]; 
} else { 
    [session addInput:currentVideoInput]; 
} 
    [session commitConfiguration]; 
    [newVideoInput release]; 
} else if (error) { 
    NSLog(@"%@",[error localizedDescription]); 
} 

코드, 심지어 내가 필요 없다면.

누군가 이런 일이 일어나는 이유와 해결 방법을 설명 할 수 있습니까?

편집 : 다이슨 W

[[[self.videoViewController captureManager] session] stopRunning]; 
[self.videoViewController.view removeFromSuperview]; 
self.videoViewController = nil; 

감사 : 다이슨 W로
해결은 내가 지금처럼 내 videoViewController을 해제하기 전에 세션을 중지해야 자신의 응답 지적했다.

답변

3

오류 로그가 표시됩니까? 그렇지 않다면, 위의 코드를 수정하고 그들이 말하는 것을보아야합니다. 사용중인 AVCam의 버전은 무엇입니까? 그들은 최근 버전 1.2로 프로젝트를 업데이트했습니다.이 버전은 훨씬 효율적이고 블록을 사용합니다.

내 경험에 비추어 볼 때, 세션을 만들고 다시 만들어서는 안된다. 어쩌면 당신의 앱을 조금 다르게 구성해야 할 수도 있습니다. 앱에 대해 정확히 무엇이 있습니까? 어쩌면 우리가 널 도울 수있어. 응용 프로그램이 카메라 주변에 집중되어 있다면 세션을 그대로 두는 것이 더 쉽습니다. 단지 비디오를 모달 형식으로 사용한다면 AVCam을 사용하는 것이 과도 할 수도 있습니다.

실제 문제는 나에게 AVCaptureDeviceInput과 같은 것 같습니다. 원본 AVCam 패키지를 다운로드하고 보관 횟수 또는 안전 if 문을 변경했는지 확인하십시오. 다른 코드가 있으면 게시하십시오.

UPDATE : 당신은

} else if (error) { 
    NSLog(@"%@",[error localizedDescription]); 
} 

} if (error) { 
    NSLog(@"%@",[error localizedDescription]); 
} 

을 변경하고 오류가 있다면 말씀해 주시겠습니까?

또한 세션을 소유 한보기 컨트롤러를 해제하기 전에 세션을 중지하고 캡처 관리자를 nil로 설정해야합니다.

업데이트 2 :이 토글 코드를 사용해보세요. 그것은 내가 사용 해왔다.

enum { 
    AVCamMirroringOff = 1, 
    AVCamMirroringOn = 2, 
    AVCamMirroringAuto = 3 
}; 
typedef NSInteger AVCamMirroringMode; 


- (BOOL) toggleCamera 
{ 
    BOOL success = NO; 

    if ([self cameraCount] > 1) { 
     NSError *error; 
     AVCaptureDeviceInput *newVideoInput; 
     AVCaptureDevicePosition position = [[videoInput device] position]; 

     BOOL mirror; 
     if (position == AVCaptureDevicePositionBack){ 
      newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self frontFacingCamera] error:&error]; 
      switch ([self mirroringMode]) { 
       case AVCamMirroringOff: 
        mirror = NO; 
        break; 
       case AVCamMirroringOn: 
        mirror = YES; 
        break; 
       case AVCamMirroringAuto: 
       default: 
        mirror = YES; 
        break; 
      } 
     } 
     else if (position == AVCaptureDevicePositionFront){ 
      newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self backFacingCamera] error:&error]; 
      switch ([self mirroringMode]) { 
       case AVCamMirroringOff: 
        mirror = NO; 
        break; 
       case AVCamMirroringOn: 
        mirror = YES; 
        break; 
       case AVCamMirroringAuto: 
       default: 
        mirror = NO; 
        break; 
      } 
     } 
     else 
      goto bail; 

     if (newVideoInput != nil) { 
      [[self session] beginConfiguration]; 
      [[self session] removeInput:[self videoInput]]; 
      if ([[self session] canAddInput:newVideoInput]) { 
       [[self session] addInput:newVideoInput]; 
       AVCaptureConnection *connection = [AVCamUtilities connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self stillImageOutput] connections]]; 
       if ([connection isVideoMirroringSupported]) { 
        [connection setVideoMirrored:mirror]; 
       } 
       [self setVideoInput:newVideoInput]; 
      } else { 
       [[self session] addInput:[self videoInput]]; 
      } 
      [[self session] commitConfiguration]; 
      success = YES; 
      [newVideoInput release]; 

     } else if (error) { 
      if ([[self delegate] respondsToSelector:@selector(captureManager:didFailWithError:)]) { 
       [[self delegate] captureManager:self didFailWithError:error]; 
      } 
     } 
    } 

bail: 
    return success; 
} 
+0

아니요, 오류 로그가 없습니다. 너는 무엇을 제안 하는가? AVCam (예, 1.2 사용)을 살펴본 후 -captureManager : didFailWithError :를 구현했지만 여전히 오류 로그가 없습니다. 앱이 카메라 주위에 가운데에 있지 않습니다. 카메라는 측면에있는 것이므로 일부 카메라에서는 전혀 사용하지 못할 수도 있습니다. AVCam을 다시 확인했습니다. 유일한 차이점은 AVCam에서 카메라가 항상 존재한다는 것입니다. 위에서 언급 한 이유 때문에 카메라를 닫고 싶습니다. – Reggian

+0

내 업데이트 된 내용보기 –

+0

프로젝트를 보지 않고 AVCam 프로젝트의 변경 사항을 보지 않으면이 작업이 까다 롭습니다. 또한, 분석기를 실행하여 무엇인가 잡는 지 확인하십시오. –

9

이-이 시도 나를 위해 챔피언처럼 일 :

BOOL isUsingFrontFacingCamera; 

- (BOOL) swapCameras 
{ 
    if ([self cameraCount] > 1) { 
     AVCaptureDevicePosition desiredPosition; 
     if (isUsingFrontFacingCamera) { 
      desiredPosition = AVCaptureDevicePositionBack; 
     } else { 
      desiredPosition = AVCaptureDevicePositionFront; 
     } 

     for (AVCaptureDevice *d in [AVCaptureDevice devicesWithMediaType: AVMediaTypeVideo]) { 
      if ([d position] == desiredPosition) { 
       [[self session] beginConfiguration]; 
       AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:d error:nil]; 
       for (AVCaptureInput *oldInput in [[self session] inputs]) { 
        [[self session] removeInput:oldInput]; 
       } 
       [[self session] addInput:input]; 
       [[self session] commitConfiguration]; 
       break; 
      } 
     } 
     isUsingFrontFacingCamera = !isUsingFrontFacingCamera; 
     return YES; 
    } 
    return NO; 
} 

당신은 원래 swapCameras에게를 대체하기 위해이 코드를 드롭 할 수 있어야 다음과 같이 AVCamMirringMode는 구조체이다 VideoRecorderCaptureManager.m

출처 : Apple 's SquareCam 샘플 코드.

+1

감사합니다. 우수 – Dejell

+0

완벽하게 작동합니다. – user3117509

+0

이것은 나를 위해 일한 유일한 코드입니다. 트릭은 세션에서 모든 입력 장치를 제거하는 것입니다. 감사. – CarmenA