Multimedia Programming Guide

Using Video

Important: This document contains information that used to be in App Programming Guide for iOS. The information in this document has not been updated specifically for iOS 4.0.

Recording and Editing Video

Starting in iOS 3.0, you can record video, with included audio, on supported devices. To display the video recording interface, create and push a UIImagePickerController object, just as for displaying the still-camera interface.

To record video, you must first check that the camera source type (UIImagePickerControllerSourceTypeCamera) is available and that the movie media type (kUTTypeMovie) is available for the camera. Depending on the media types you assign to the mediaTypes property, the picker can directly display the still camera or the video camera, or a selection interface that lets the user choose.

In iOS 4.0 and later, you can edit, assemble, and compose video using existing assets or with new raw materials. Assets are represented by AVAsset, which you can inspect asynchronously for better performance. You use AVMutableComposition to compose media from one or more sources, then AVAssetExportSession to encode output of a composition for delivery.

Playing Video Files

Important: The information in this section currently reflects the usage of the Media Player framework in iOS 3.1 and earlier. Please see the headers for information about changes to this framework in iOS 4.0.

iOS supports the ability to play back video files directly from your application using the Media Player framework, described in Media Player Framework Reference. Video playback is supported in full screen mode only and can be used by game developers who want to play short animations or by any developers who want to play media files. When you start a video from your application, the media player interface takes over, fading the screen to black and then fading in the video content. You can play a video with or without user controls for adjusting playback. Enabling some or all of these controls (shown in Figure 2-1) gives the user the ability to change the volume, change the playback point, or start and stop the video. If you disable all of these controls, the video plays until completion.

Figure 2-1 Media player interface with transport controls

To initiate video playback, you must know the URL of the file you want to play. For files your application provides, this would typically be a pointer to a file in your application’s bundle; however, it can also be a pointer to a file on a remote server. Use this URL to instantiate a new instance of the MPMoviePlayerController class. This class presides over the playback of your video file and manages user interactions, such as user taps in the transport controls (if shown). To start playback, call the play method described in MPMediaPlayback Protocol Reference.

Listing 2-1 shows a sample method that plays back the video at a specified URL. The play method is an asynchronous call that returns control to the caller while the movie plays. The movie controller loads the movie in a full-screen view, and animates the movie into place on top of the application’s existing content. When playback is finished, the movie controller sends a notification received by the application controller object, which releases the movie controller now that it is no longer needed.

Listing 2-1 Playing full-screen movies

-(void) playMovieAtURL: (NSURL*) theURL {

MPMoviePlayerController* theMovie =

[[MPMoviePlayerController alloc] initWithContentURL: theURL];

theMovie.scalingMode = MPMovieScalingModeAspectFill;

theMovie.movieControlMode = MPMovieControlModeHidden;

// Register for the playback finished notification

[[NSNotificationCenter defaultCenter]

addObserver: self

selector: @selector(myMovieFinishedCallback:)

name: MPMoviePlayerPlaybackDidFinishNotification

object: theMovie];

// Movie playback is asynchronous, so this method returns immediately.