2011-09-22 2 views
1

카메라의 비디오 소스를 배경으로 보여줄 수있는 응용 프로그램이 있습니다. 내 viewcontroller있는 다음 코드가 있습니다 :Objective-c UIImagePickerController를 비디오 모드로 변경합니다.

#if !TARGET_IPHONE_SIMULATOR 
    imagePickerController = [[UIImagePickerController alloc] initWithRootViewController:self]; 
    imagePickerController.delegate = self; 
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; 

    imagePickerController.navigationBarHidden = YES; 
    imagePickerController.toolbarHidden = NO; 
    imagePickerController.showsCameraControls = NO; 
    //... 
    [self.view addSubview:self.imagePickerController.view]; 
    [imagePickerController viewWillAppear:YES]; 
    [imagePickerController viewDidAppear:YES]; 
#endif 
    //... 
    [self.view addSubview:otherthings]; 

다음 나는 위에 다른보기를 추가하고 나도 소리가. 그러나 imagepicker 모드를 비디오로 변경했지만 사운드가 재생되면 고정됩니다. 여기 내가 바뀐 것 :

#if !TARGET_IPHONE_SIMULATOR 
    imagePickerController = [[UIImagePickerController alloc] init];//initWithRootViewController:self]; 
    imagePickerController.delegate = self; 
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; 

    NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]; 
    NSArray *videoMediaTypesOnly = [mediaTypes filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(SELF contains %@)", @"movie"]]; 
    BOOL movieOutputPossible = (videoMediaTypesOnly != nil); 

    if (movieOutputPossible) { 
     imagePickerController.mediaTypes = videoMediaTypesOnly; 
     imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh; 
     imagePickerController.navigationBarHidden = YES; 
     imagePickerController.toolbarHidden = YES; 
     imagePickerController.showsCameraControls = NO;      
    } 


#endif 

사운드가 재생 될 때 카메라 선택 도구가 정지하는 이유는 누구나 아는 사람이 있습니까? 그런데 사운드는 AVAudioPlayer입니다.

+0

왜 alloc 동안 rootViewController의 정의를 제거 했습니까? –

+0

Dunno, 소리가 들릴 때만 시도해 보니 왜? –

+0

이상 당신은 뭔가를 autorelease하고있어 alloc'd 자신의 보존해야한다. 구체적으로이 두 줄부터 시작하겠다. (NSArray * mediaTypes =) 및 (NSArray * videoMediaTypesOnly =). 이것들은 모두 당신의 통제없이 자동 릴리즈되어 문제를 일으킬 수 있습니다. –

답변

2

솔루션 : UIImagePickerController 대신 AVFOundation을 사용하십시오.

videoBackground = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)]; 

    AVCaptureSession *session = [[AVCaptureSession alloc] init]; 
    session.sessionPreset = AVCaptureSessionPresetMedium; 


    CALayer *viewLayer = videoBackground.layer; 
    NSLog(@"viewLayer = %@", viewLayer); 

    AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; 

    captureVideoPreviewLayer.frame = videoBackground.bounds; 
    [videoBackground.layer addSublayer:captureVideoPreviewLayer]; 

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 

    NSError *error = nil; 
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; 
    if (!input) { 
     // Handle the error appropriately. 
     NSLog(@"ERROR: trying to open camera: %@", error); 
    } 
    [session addInput:input]; 
    [session startRunning]; 
    [self.view addSubview:videoBackground]; 
관련 문제