Monday, July 4, 2011

Record Video in iPhone


In this post we will learn how to record a video from the iphone and save it in the iphone photo album.
Design Phase: The design phase for this application will contain only one button and we will first make a check whether the device has a camera or not and then we will perform the further actions.

Step 1: Open Xcode create a windows based application give the project an appropriate name and then add UIViewController subclass files to it with the name MyCameraViewController  or any name of your choice now their will be two files inside your project with the name MyCameraViewController.h and MyCameraViewController.m.
Step 2: Declare a function which will be associated with the button in the MyCameraViewController.h file. Here’s the code to do that
@interface MyCameraViewController : UIViewController <UIImagePickerControllerDelegate,UINavigationControllerDelegate> {
   
}
-(void)displayCamera;

@end

Since we will be using the delegate protocol method of the UIImagePickerController class we have to implement two protocol method which are shown above.
Now select the MyCameraViewController.m file and give body to that function which you have declared in the .h file of the MyCameraViewController, the body of the function looks like this

-(void)displayCamera
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
       
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
       
        //all media types
        imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
        imagePicker.allowsEditing = NO;
       
        imagePicker.delegate = self;
        [self presentModalViewController:imagePicker animated:YES];
    }
   
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Camera Demo" message:@"Device Lacks Camera" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
       
        [alert show];
        [alert release];
    }
   
}

Code Explanation: The first line of code checks whether the device has camera or not with the help of the static function of the class UIImagePickerController. The UIImagePickerController class manages customizable, system-supplied user interfaces for taking pictures and movies on supported devices, and for choosing saved images and movies for use in your app. An image picker controller manages user interactions and delivers the results of those interactions to a delegate

The UIImagePickerController has a property called as the mediaTypes with the help of which you can either take pics or record videos or both so in the above code I have selected both the options and then I have specified where is the delegate method of the UIImagePickerController class and by using the delegate method I have saved the video inside the iPhone photo album.

Also inside the else part I have written the code for the alert view which will popup if the device lacks the camera. So if you run the application inside the simulator then in that case you will get a alert view saying that the device lacks camera because the simulator is not capable of working with the camera and to test the apps which require camera you have to deploy the apps into the actual iPhone or iPod touch device.



Step 3: Let’s have a look at the delegate method of the UIImagePickerController

- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info {
   
    NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL]path];

    if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
       
        UISaveVideoAtPathToSavedPhotosAlbum (moviePath, nil, nil, nil);
       
    }
   
    [[picker parentViewController] dismissModalViewControllerAnimated: YES];
   
    [picker release];
  
    }

Code Explanation: The first line of code contains the location of where the movie file is present in the iPhone file system and we have to save the video present in that location to the iPhone gallery but before that we perform a check whether the movie is compatible with the file system or not and then with the help of the function UISaveVideoAtPathToSavedPhotosAlbum we save the video file into the photo album.

Step 5: Now the only part pending is going into the app delegate .m and creating the object of the MyCameraViewController so heres the code to do that

#import "VideoRecorderDemoAppDelegate.h"
#import "MyCameraViewController.h"

@implementation VideoRecorderDemoAppDelegate

@synthesize window=_window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    MyCameraViewController *cameraObject = [[MyCameraViewController alloc]init];
    [self.window addSubview:cameraObject.view];
    [self.window makeKeyAndVisible];
    return YES;
}

So in order to test this demo app please deploy the app into device and then run the application. I hope that this post was helpful to you in learning how to take the video from the iPhone via code and save it into the gallery. 


Download the source code from this link.


Happy iCoding and have a great day.

4 comments: