如何在imagePicker中选择视频

时间:2012-07-11 06:51:23

标签: ios video uiimagepickercontroller

这是我从imagePicker中选择视频的代码:

imagepicker = [[UIImagePickerController alloc] init];
[imagepicker setDelegate:self];      

NSArray *mediaTypesAllowed = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[imagepicker setMediaTypes:mediaTypesAllowed];

imagepicker.wantsFullScreenLayout = YES;

pickImagePop = [[UIPopoverController alloc] initWithContentViewController:imagepicker];

[pickImagePop setDelegate:self];
[pickImagePop presentPopoverFromRect:CGRectMake(60, 100, 1, 20) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[pickImagePop setPopoverContentSize:CGSizeMake(320, 400) animated:YES];

但是此代码在Picker中一起显示照片和视频。我想只播放视频。我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:2)

如果您只想要视频,请添加:

imagepicker.mediaTypes =
[[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];

请记住将MobileCoreServices框架添加到您的项目中。

编辑:

您应事先使用以下方法检查设备可以使用哪些类型。

+ (NSArray *)availableMediaTypesForSourceType:(UIImagePickerControllerSourceType)sourceType

它将返回一个包含设备支持的所有媒体类型的数组。

答案 1 :(得分:1)

只需尝试此

 1.Add MobileCoreServices framework to your project and import to your view controller
  #import <MobileCoreServices/UTCoreTypes.h> .
 2.Add this delegate methods in Interface 
    <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
 3.Create a Button Action for Record Video and play video like this

- (IBAction)recordVideo:(UIButton*)sender {
    UIImagePickerController *videoScreen = [[UIImagePickerController alloc] init];
    videoScreen.sourceType = UIImagePickerControllerSourceTypeCamera;

    // Displays movie capture control
    videoScreen.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];

    videoScreen.allowsEditing = NO;
    videoScreen.delegate = self;

    [self presentViewController:videoScreen animated: YES completion:NO];
}

//ImagePickerDelegate

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
    [self dismissViewControllerAnimated:NO completion:NO];
    // Handle a movie capture
    if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
        NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath)) {
            UISaveVideoAtPathToSavedPhotosAlbum(moviePath, self, nil, nil);
        }
    }
}
}

- (IBAction)playVideo:(id)sender {
    UIImagePickerController *mediaLibrary = [[UIImagePickerController alloc] init];
    mediaLibrary.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    mediaLibrary.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];

    mediaLibrary.allowsEditing = NO;
    [self presentViewController:mediaLibrary animated:YES completion:NO];
}

答案 2 :(得分:0)

    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie];
    NSArray *sourceTypes = [UIImagePickerController availableMediaTypesForSourceType:imagePicker.sourceType];
    if (![sourceTypes containsObject:(NSString *)kUTTypeMovie ])
    {
        //NSLog(@"no video");
    }


    [self presentModalViewController:imagePicker animated:YES];




 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
            NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
            //NSLog(@"type=%@",type);
            if ([type isEqualToString:(NSString *)kUTTypeVideo] || 
                [type isEqualToString:(NSString *)kUTTypeMovie])
            {// movie != video
                NSURL *urlvideo = [info objectForKey:UIImagePickerControllerMediaURL];
}
    }