Channels ▼
RSS

Mobile

iPhone: Recording, Playing, and Accessing Video


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>
Listing 1: Displaying the video camera

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

Listing 2: Playing the recorded video.

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.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video