设置最长录制时长,同时从iphone应用程序录制视频

时间:2013-01-22 09:16:41

标签: iphone objective-c media-player avfoundation video-capture

在我的iPhone应用程序中,用户可以录制视频。我想将录制的最大允许时间设置为30秒。如何做到这一点,有什么想法?

使用此代码

-(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];
   }
  }

1 个答案:

答案 0 :(得分:4)

配置视频录制后,需要在UIImagePickerController上设置videoMaxiumDuration属性。

该值是以秒为单位指定的NSTimeInterval,因此如果您想要5分钟的视频,则需要将其设置为300秒。

相关问题