Dhanesh
Dhanesh

Reputation: 93

How to pick videos in imagePicker

here is my code to pick videos from 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];

But this code shows photos and videos together in Picker. I want to show only video. How can I do that?

Upvotes: 1

Views: 5844

Answers (3)

Rameshios
Rameshios

Reputation: 11

Simply try This

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

Upvotes: 1

Ajeet
Ajeet

Reputation: 313

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

Upvotes: 0

tomidelucca
tomidelucca

Reputation: 2543

If you only want videos you should add:

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

Remember to add the MobileCoreServices framework to your project.

EDIT:

You should check beforehand which types can be used by the device with the following method.

+ (NSArray *)availableMediaTypesForSourceType:(UIImagePickerControllerSourceType)sourceType

It will return an array with all the media types supported by the device.

Upvotes: 2

Related Questions