Search code examples
iphoneobjective-cmedia-playeravfoundationvideo-capture

Set maximum recording duration,while video recording from iphone app


In my iPhone app,a user can record the video.I want to set the maximum allowed time of recording is to 30seconds.How to do that,any ideas ?

using this code

-(BOOL)startCameraControllerFromViewController:(UIViewController*)controller
                             usingDelegate:(id )delegate {
   if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO)
    || (delegate == nil)
    || (controller == nil)) {
    return NO;
   }
   UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
  cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
 cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];

cameraUI.allowsEditing = NO;
cameraUI.delegate = delegate;

[controller presentModalViewController: cameraUI animated: YES];
return YES;
 }

-(void)imagePickerController:(UIImagePickerController *)picker  didFinishPickingMediaWithInfo:(NSDictionary *)info {
 NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:NO];
  if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
    NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
    NSLog(@"movie path %@",moviePath);


    if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath)) {
        UISaveVideoAtPathToSavedPhotosAlbum(moviePath, self,
                                              @selector(video:didFinishSavingWithError:contextInfo:), nil);
    }
  }
}

-(void)video:(NSString*)videoPath didFinishSavingWithError:(NSError*)error contextInfo:  (void*)contextInfo {
   if (error) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Video   Saving Failed"
                                                   delegate:nil cancelButtonTitle:@"OK"   otherButtonTitles:nil];
    [alert show];
  } else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Video Saved"   message:@"Saved To Photo Album"
                                                   delegate:self cancelButtonTitle:@"OK"   otherButtonTitles:nil];
    [alert show];
   }
  }

Solution

  • You need to set the videoMaxiumDuration property on UIImagePickerController after configuring it for video recording.

    The value is an NSTimeInterval which is specified in seconds, so you'll want to set it to 300 seconds if you want 5 mins of video.