O'Reilly logo

iOS 5 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

9.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:

self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];

In this code, moviePlayer is a property of type MPMoviePlayerController defined and synthesized 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 to give more control to programmers and ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required