12.6. Playing Video Files

Problem

You would like to be able to play video files in your iOS application.

Solution

Use an instance of the MPMoviePlayerController class.

Note

If you simply want to display a full-screen movie player, you can use the MPMoviePlayerViewController class and push your movie player view controller into the stack of view controllers of a navigation controller (for instance), or simply present your movie player view controller as a modal controller on another view controller using the presentMoviePlayerViewControllerAnimated: instance method of UIViewController. In this recipe, we will use MPMoviePlayerController instead of MPMoviePlayerViewController in order to get full access to various settings that a movie player view controller does not offer, such as windowed-mode video playback (not full-screen).

Discussion

The Media Player framework in the iOS SDK allows programmers to play audio and video files, among other interesting things. To be able to play a video file, we will instantiate an object of type MPMoviePlayerController like so:

MPMoviePlayerController *newMoviePlayer =
[[MPMoviePlayerController alloc] initWithContentURL:url];

self.moviePlayer = newMoviePlayer;

In this code, moviePlayer is a property of type MPMoviePlayerController defined for the current view controller. In older iOS SDKs, programmers had very little control over how movies were played using the Media Player framework. With the introduction of the iPad, the whole framework changed drastically ...

Get iOS 7 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.