The API and overall approach for working with MPMoviePlayerController has changed just enough in the 3.2 SDK (iPad) and iOS4 SDK to cause working applications in earlier releases to be problematic when running on later SDKs. In this post I’ll walk through a short example of a movie player application that will work with 3.x, 3.2 and 4.0 SDKs.
In the next post, Display iPhone Movies in Portrait Mode, I’ll show how to create a movie player that runs in a view that is not fullscreen as well as how to show a few lines of code to display a movie in portrait mode – the one caveat here is that both of the tips in the second post will apply only to OS versions 3.2 and up.
What’s Changed with MPMoviePlayerController in 3.2 and 4.0?
There are any number of changes in the MPMoviePlayerController, however, a few standout as potential roadblocks to getting a movie to display:
- In 3.1 and earlier versions, MPMoviePlayerController was full-screen only. Playing a movie was straight-forward, create a player, initialize with a file (path or URL) and call a method to start playback – the rest was taken care of for you.
- With 3.2 and later, movies can playback in fullscreen or a custom view, as well as portrait or landscape.
- The notification MPMoviePlayerContentPreloadDidFinishNotification has been deprecated. This notification was used in earlier versions to notify observers that a movie was loaded and ready to play.
The Movie Application
The application in this post is quite simple, it consists of a view controller with nothing more than a button to start playback and a second view controller to manage aMPMoviePlayerController and the NSURL of the movie. The two views are shown in the figures below:
Primary View Controller
The interface definition for the primary view is shown below:
CustomMoviePlayerViewController is the controller for managing the movie, we’ll look at that code in a moment.
In the code below we create the view, add a play button, create a method for processing a button press event and within loadMoviePlayer, we get a reference to the movie file and create an instance of the CustomMoviePlayerViewController, which will load and play the movie.
Notice that the movie for this example is loaded from the application bundle. Also, once the custom movie player is created, the view is shown as a modal view.
Custom Movie Player View Controller
The interface definition for the movie view controller is below:
The initialization code is where the movie player and associated URL are created. The primary goal of the initialization is to create the NSURL needed by the movie player.
The code to create the player and setup the notifications is where we start to deal with the differences in how the movie player works on various OS versions. Notice below the call torespondsToSelector, this is the Apple recommended way to check for feature availability, versus looking for a specific OS version.
For devices running 3.2 and above, the movie player controller has a method named loadstate, if this exists we can access a few additional methods to set the player to fullscreen as well as request the movie to begin preloading.
Equally important is the distinction of which notification to setup – see lines 16 and 24.
You can read more about managing different OS version in this post Developing iPhone Apps with iOS4 SDK, Deploying to 3.x Devices
The next two chunks of code are for each of the selectors, one for each notification. The code below is for earlier OS versions – nothing more than removing the notification and asking the movie to play.
For the notification generated in 3.2 and above, there are a few more details to tend to:
Beyond removing the notification, we also adjust the status bar, rotate the view, set the frame of the movie player, add the movie player as a subview in the view controller and wrap it all up by asking the movie to play.
Note: MPMoviePlayerViewController is also an option over creating your own view controller as I’ve done here.
Source Code
The easiest way to see all this working is to download the source code and step through the code in the debugger.
No comments:
Post a Comment