Play and Record Videos in iOS

One of the cool things of modern iOS devices is taking pictures or shooting videos. In this tutorial we will record a video and save it to the media library. The video can be selected and played from the library.

Open Xcode and create a new Single View Application. For product name, use RecordVideoDemo and then fill out the Organization Name, Company Identifier and Class Prefix fields with your customary values. Make sure only iPhone is selected in Devices, and that the Use Storyboards checkbox is deselected and Use Automatic Reference Counting checkbox is selected.

To use the video type inside iOS we need the MobileCoreServices framework. Got to the project Options -> Linked Frameworks and Libraries and add the framework

In ViewController.m import the MobileCoreServices Framework

#import <MobileCoreServices/MobileCoreServices.h>

Next, let's create our User Interface. Go to ViewController.xib. Drag 2 UIButtons to the main View. Double-click each button and name them "Record" and "Play".The main View should look like this.

To display the Media Library and the view Controller must conform to the navigation controller delegate and the image picker delegate. In ViewController.m change the @interface line in

@interface ViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>

In the interface section add the following IBAction methods.

- (IBAction)recordVideo:(id)sender;
- (IBAction)playVideo:(id)sender;

Go back to ViewController.xib. Select the Assistant Editor. On the right side select ViewController.m. Ctrl-click and drag from the Play button to the playVideo method in ViewController.m. Next, Ctrl-click and drag from the Record button to the recordVideo method.

Implement the recordVideo method

- (IBAction)recordVideo:(id)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];

The image picker controller is initialized with the Movie mediaType. This view Controller will be presented to the user. When the user presses the Use button the delegate method imagePickerController:didFinishPickingMediaWithInfo will be called.

-(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);

The video Capture View Controller is dismissed and the video is saved to the media library. The last thing we need to do is implement the playVideo method

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

The media library is presented to the users showing all videos.

Build and Run, record a video and then press the play button and select the video from the Media Library.


You can download the source code of the RecordVideoDemo at the ioscreator repository on github.