2011-09-22 45 views
1

我有一個應用程序,我想在背景中顯示相機的視頻源。我在我的視圖控制器中有以下代碼: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

鄧諾,嘗試它後,只有當我發聲時凍結。爲什麼? –

+0

更有可能你已經有了一些自動釋放的東西,你應該已經分配了自己的東西來保存。具體來說,我會從這兩行開始:(NSArray * mediaTypes =)和(NSArray * videoMediaTypesOnly =)。這些都是無法控制的自動釋放,並可能導致問題。 –

回答

2

解決方案:使用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]; 
相關問題