3

在我的 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];
   }
  }
4

1 回答 1

4

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

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

于 2013-01-22T09:27:02.043 回答