How to Use High-level Abstraction to Play Video in iOS Apps - dummies

How to Use High-level Abstraction to Play Video in iOS Apps

By Rajiv Ramnath

This section starts by showing you how to use a high-level abstraction, the MPMoviePlayerViewController class, to play video in an iOS app. Here is the code (in the Video view controller, from the file TTTVideoViewController.m):

- (IBAction) playVideo:(id)sender{
 MPMoviePlayerViewController *moviePlayer =
  [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
 [self presentMoviePlayerViewControllerAnimated:moviePlayer];
}

Simplicity itself! All you need is a URL that points to a movie file on the file system or to an Internet URL with which to instantiate the movie player. The player’s UI handles stopping, starting, and so on.

Taking a photo on an app.

The member variable movieURL is set in the viewDidLoad method that’s called after the display is initialized for this view controller (a sample movie is now saved in a resource file named Videos.bundle), as shown here:

- (void)viewDidLoad{
 [super viewDidLoad];
 // Do any additional setup after loading the view.
 NSString* videoPath =
  [[NSBundle mainBundle] pathForResource:@"Videos.bundle/SampleVideo"
        ofType:@"MOV"];
 movieURL = [NSURL fileURLWithPath:videoPath];
 }

The following example shows you how to use an abstraction that’s one level lower than the preceding one using the lower-level method known as recordVideo:

- (IBAction) recordVideo:(id)sender{
 UIImagePickerController *imagePicker =
  [[UIImagePickerController alloc] init];
 imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
 imagePicker.mediaTypes =
  [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
 imagePicker.delegate = self;
 [[UIApplication sharedApplication] setStatusBarHidden:YES];
 [self presentViewController:imagePicker animated:YES completion:nil];
}

When this method is called, a screen with a camera interface appears. Using this screen, video recording can be started and stopped. When video recording is done, this screen transfers control back to the calling view controller.

Recording a video on an app emulator.

The following two methods are the callback methods called by the Image picker (notice how the Video view controller registers itself as a delegate with the ImagePicker object):

  • imagePickerControllerDidCancel is called when the recording is cancelled. It simply dismisses the picker and shows the iOS status bar again.

    - (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker {
     [[UIApplication sharedApplication] setStatusBarHidden:NO];
     [picker dismissViewControllerAnimated:YES completion:nil];
    }
  • didFinishPickingMediaWithInfo starts by showing the iOS status bar again and dismisses the picker. It then saves the recorded video in the camera roll of the device. Finally, this method sets the member variable movieURL to point to this new video, so when you select Play Video again, the newly recorded movie plays.

- (void) imagePickerController:(UIImagePickerController *) picker
   didFinishPickingMediaWithInfo :(NSDictionary *) results {
 NSURL *moviePathURL =
   [results objectForKey:UIImagePickerControllerMediaURL];
 NSString *moviePath = [moviePathURL path];
 movieURL = moviePathURL;
 //Save in camera roll
 if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)){
  UISaveVideoAtPathToSavedPhotosAlbum (moviePath, nil, nil, nil);
 }
 [[UIApplication sharedApplication] setStatusBarHidden:NO];
 [picker dismissViewControllerAnimated:YES completion:nil];
}