Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective-c将UIImagePickerController更改为视频模式_Objective C_Video_Uiimagepickercontroller - Fatal编程技术网

Objective-c将UIImagePickerController更改为视频模式

Objective-c将UIImagePickerController更改为视频模式,objective-c,video,uiimagepickercontroller,Objective C,Video,Uiimagepickercontroller,我有一个应用程序,我想在后台显示来自摄像机的视频源。我的viewcontroller中有以下代码: #if !TARGET_IPHONE_SIMULATOR imagePickerController = [[UIImagePickerController alloc] initWithRootViewController:self]; imagePickerController.delegate = self; imagePickerController.sourceT

我有一个应用程序,我想在后台显示来自摄像机的视频源。我的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];
然后我在顶部添加其他视图,我也有声音。然而,我将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 

有人知道为什么当一个声音响起时,摄像头会冻结吗?顺便说一句,这声音是一个音频播放器

解决方案:使用AVFOundation而不是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];

为什么你在alloc期间删除了rootViewController的定义?不知道,在尝试之后,它只在我播放声音时冻结。为什么?很可能你已经做了一些自动释放的事情,你应该保留自己的。具体来说,我将从以下两行开始:(NSArray*mediaTypes=)和(NSArray*videoMediaTypesOnly=)。这些都是在没有您控制的情况下自动释放的,可能会导致问题。即使我删除它们并添加imagePickerController.mediaTypes=[[NSArray alloc]initWithObjects:(NSString*)KuttypeMoine,nil];它在播放声音时仍然会冻结。当声音结束时,相机不会解冻。我不确定我是否明白…你是说播放AVAudioPlayer声音时电影播放会冻结,然后在声音播放结束后恢复播放?