Brandon Trebitowski is the author of iPhone and iPad in Action.
One of the major updates included with the iPhone 3Gs was a built-in video camera. This allows users to easily record video and save it to their media library. The code for recording video is almost identical to the code to show the camera. It does, however, have a few checks that are required. Listing 1 shows the code for bringing up the video camera interface.
-(void) showVideoCamera {
if ([UIImagePickerController <b>#1</b>
isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
myImagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
NSLog(@"Camera not supported");
Return;
}
<b>#2</b>
NSArray *media = [UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypeCamera];
If([media containsObject:kUTTypeMovie]) { <b>#3</b>
myImagePicker.mediaTypes = [NSArray
arrayWithObjects: kUTTypeMovie,nil];
[self presentModalViewController:myImagePicker animated:YES]; <b>#4</b>
} else {
NSLog(@"Video not supported");
}
}
<b>
#1 check to see if the camera is available
#2 get a list of the media types the camera supports
#3 check to see if the camera supports video
#4 show the video camera
</b>
The first thing we are doing in #1 is checking to see if the device has camera support. There are two cases where this would return False. The first is, the user has an iPod touch. As of this writing, the iPod touch does not support taking photos. The other case is if the camera is damaged on the iPhone.
In #2, we are checking to see what media types are supported by the camera. In this case we are looking for the media type kUTTypeMovie. If this is found, then the camera will support video. #3 performs this check and sets the media type of our picker to kUTTypeMovie to tell it to display the video camera. By default, it is set to kUTTypeImage, which specifies photos, so it is necessary that we set it.
Finally, in #4 we display the video camera on the screen. Once great feature that Apple added is the ability to edit the video on-the-fly. This is very easy to integrate in our code. Simply add this line prior to displaying the video camera:
myImagePicker.allowsEditing = YES;
This great one-liner from Apple adds a ton of functionality. Once the user finishes recording the video the delegate method didFinishPickingMediaWithInfo: for the picker will be called. The dictionary passed to this method will contain a system path URL to the video file that was just recorded. Listing 2 shows how to use this path to retrieve and play back the video that was just recorded.
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL * pathURL = [info objectForKey: UIImagePickerControllerMediaURL];
MPMoviePlayerController * player =
[[MPMoviePlayerController alloc] initWithContentURL:pathURL];
[player play];
}
The first thing this method does is retrieves the path URL from the info dictionary. The path URL is the object stored with the key UIImagePickerControllerMediaURL. Next, an MPMoviePlayerController is allocated with the contents of the path URL. This will load the video and prepare it to play. The last thing to do is call the play method and the video will begin.


