Editor’s Note: With the release of SDK’s supporting 3.2 and 4.x, this approach is no longer necessary. The new movie player can now be displayed in either direction, landscape or portrait. Read this post for a more information: Display iPhone Movies in Portrait Mode (Updated)
It’s been covered by a number of websites and blogs on how to play movies in portrait mode using MPMoviePlayerController. Problem is, every solution that I’ve been able to find uses private API’s to tell the player to flip the direction of play.
Other than the built-in movie player, another option is to use a UIWebview, however, there are a few drawbacks including no support for notifications on when a movie has been preloaded, which is handy for displaying a “please wait” loading message.
This post is introduces another approach to playing a movie in portrait mode without delving into private API’s, and I think you’ll agree, it’s quite clever.
Portrait Video Mode on the iPhone
I spend a fair amount of time working with video related applications and content, so when I saw a portrait video playing seamlessly inside Dog Tricks – Best of 101 Dog Tricks I was very interested to figure out how to do the same.
I wrote Michael Schneider of Hive Brain Software, developer of the above app and asked if he would share his secret. Michael was kind enough to pass along some great notes explaining how we got this to work. This post walks through a complete working example I wrote based on the information Michael shared with me.
The End Result
Let’s start by looking at a short video of the application I’ll build in this post running in the simulator:
Notice how the video is shown within the application with the tabbar controls still visible on the screen.
The 30,000 Foot View
The basic idea to make this work is simple, rotate the orientation of the original video, turn off the movie player controls and fire up the movie player as normal.
What you end up with is the movie player running landscape mode with your video content viewable in portrait mode. The reason for hiding the controls is that they would appear sideways. As an example, notice in the screenshot below that the video is viewable in portrait mode, however, the controls are still oriented for a landscape movie player:
Although a simple trick, it’s the little things that will make this approach appear seamless within your application. As an example of a nice effect, I’ll show how to overlay an image on top of the movieplayer so it appears as though the movie is playing directly inside your app.
There are three steps to make this all work:
- Change the orientation of the movie
- Turn off the movie player controls
- Overlay an image of your application UI on top of the movie player
- Turn off the movie player controls
- Overlay an image of your application UI on top of the movie player
Let’s see how to pull all the pieces together…
Video Orientation
The first step is to use a capable video editing tool to rotate your video to portait mode. I used Quicktime Pro (on Snow Leopard), the steps that I used follow:
- From the Window menu choose Show Movie Properties
- Click on the row for the video track
- Click the Visual Settings tab
- In the Flip/Rotate section, rotate the video counter-clockwise
(or clockwise depending on the orientation you set for your iPhone app).
- Click on the row for the video track
- Click the Visual Settings tab
- In the Flip/Rotate section, rotate the video counter-clockwise
(or clockwise depending on the orientation you set for your iPhone app).
Code for App Delegate
The app delegate code for this code example is nothing more than a Window object and a Tabbar controller. The interface and implementation files are below:
The implementation file:
Code for Tabbar Controller
The tabbar controller is code is nothing out of the ordinary, the interface file is shown below:
As you can see in the code below I’ve defined three tabs, each with a unique title:
Code for Tab 1
The only tabbar that has any working code for this example is the left-most tab, which plays the movie in portrait mode. The interface definition for this tab is shown below:
Inside the implementation file, the first block of relevant code is the initialization, which sets up the view, the background color and the title for the tab:
The next section of code to write is when the tab is about to appear, here we create the path to the movie, setup the movie player, register to receive a notification when the movie has finished playing, start the movie, and finally, overlay an image on top of the player (more on that below):
The interesting code starts on line 21, this is where I define a new view controller,MovieOverlayViewController, which will overlay an image on top of the running movie player. This overlay will present the illusion that the movie is running directly inside the app.
Movie Image Overlay
To give the app the appearance of running natively in portrait mode, we’ll overlay an image that shows the tabbars along the bottom and a text banner across the top. I’ll also write code to detect when the user taps on the image and do the necessary checks to see if the user tapped in an area that would translate to one of the tabs, more on that momentarily.
The finished look on the device is below:
Notice in the figure below that actual image we will overlay is nothing more than a transparent image with some text across the top and a series of tabbar images across the bottom.
The movie overlay view controller for this app is quite trivial, it contains nothing more than aUIImageView:
Inside the implementation file, we begin with the initialization and code for creating theUIImageView that will be overlayed on the movie player:
The other relevant code for this view controller is managing touches on the image. To provide the most realistic user experience, once the image is overlayed and the movie is playing, you will need to detect touches on the image where the tabs live. The code below will determine if a touched point is within the rectangle of each of the tabs, printing a message to the console on which tab was touched.
You could add code to the above example and when tab 2 or 3 is tapped, stop the movie, remove the overlay and switch to the view controller associated with the appropriate tab.
Xcode Portrait Mode Video Source Code
You can download the entire Xcode project here: Xcode Portrait Video.
No comments:
Post a Comment