1. Code
  2. iOS SDK

iPhone SDK: Playing Video with the MediaPlayer Framework


This iPhone SDK tutorial will introduce you to the Media Player framework and the MPMoviePlayerController class. You will learn how to play videos in your iPhone applications, subscribe to notifications with NSNotificationCenter, and log messages to the console. Along the way, you will hunt down and plug a memory leak and watch the Apollo 11 crew land on the moon.

Subsequent Changes to Techniques & Software

Certain aspects of applications or techniques used in this tutorial have changed since it was originally published. This might make it a little difficult to follow along. We'd recommend looking at these more recent tutorials on the same topic:

This tutorial is good for iPhone SDK beginners, as each step assumes no prior-knowledge of the Xcode environment. Intermediate developers may wish to jump straight to step 4, and those who just want to see the code snippets for playing a video can jump to steps 6 and 7.

MediaPlayer Tutorial Overview:

Step 1. Create a New View-Based Project

This step will demonstrate the creation of a new view-based application template.

iOS Media Player Framework Step 1 - Creating a project in Xcode

Step 2. Convert the Video File to an iOS Compatible Format

This step discusses the video file formats and codecs supported by the iPhone OS, and the steps necessary to convert a video from an unsupported format to a supported format using iTunes.

iOS Media Player Framework Step 2 - Valid Video File Formats

Step 3. Add Project Resources

This step displays the various project files that will be used in the MoonWalk application and demonstrates the process of adding them into the project resources folder.

iOS Media Player Framework Step 3 - Adding Project Resources

Step 4. Add the MediaPlayer Framework & Configure MoonWalkViewController.h

This step will demonstrate adding the MediaPlayer framework to the project and will customize the Moon Walk header file with the method prototype used in the application.

iOS Media Player Framework Step 4 - Adding the Framework in MoonWalk ViewController

Step 5. Create the Interface with Interface Builder

This step will utilize Interface Builder to customize the application interface with the graphic files provided.

iOS Media Player Framework Step 5 - Building the Interface with Interface Builder

Step 6. Add the “play” Method Implementation to MoonWalkViewController

This step demos the use of the MPMoviePlayerController class in order to play the moon walk landing video.

iOS Media Player Framework Step 6 - MPMoviePlayerController

Step 7. Add an NSNotification Callback for MPMoviePlayerPlaybackDidFinishNotification

This step introduces the user to the NSNotificationCenter and resolves the memory leak created in step 6. The NSLog() method created in step 6 is demonstrated and a small bug introduced in that function is fixed.

iOS Media Player Framework Step 7 - NSNotificationCenter Example
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.