开发者

Objective-c Changing UIImagePickerController to video mode

I have an application which I want onlt to show in the background the video source from the camera. I have the following code in my viewcontroller:

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

Then I add other views on top and I have sounds too. However I changed the imagepicker mode to video but it freezes when a sound plays. here's what i changed:

#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.navigationB开发者_Python百科arHidden = YES;
        imagePickerController.toolbarHidden = YES;
        imagePickerController.showsCameraControls = NO;                      
    }


#endif 

Anyone knows why the camera pickers freezes when a sound plays? The sound is an AVAudioPlayer by the way.


Solution: Use AVFOundation instead of UIImagePickerController.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜