Thursday, August 26, 2010

Display iPhone Movies in Portrait Mode (Updated)



Your Ad Here




The figure below shows the same movie as played back in landscape and portrait:

Play Movies in Portrait Mode
The code below is from the previous post and shows how to configure the movie player view to display in landscape:
MPMoviePlayerController *mp =  [[MPMoviePlayerController alloc]
     initWithContentURL:movieURL];
 
...
 
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification 
{
  ...
  // Rotate the view for landscape playback
  [[self view] setBounds:CGRectMake(0, 0, 480, 320)];
  [[self view] setCenter:CGPointMake(160, 240)];
  [[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)]; 
 
  // Set frame of movieplayer
  [[mp view] setFrame:CGRectMake(0, 0, 480, 320)];
 
  ...
}
To playback a movie in portrait mode is as easy as removing the code for the rotation and changing the frame of the movieplayer. I’ve also added a call to scale the movie so it fills the screen, this will maintain the aspect ratio.
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification 
{
  ...
  // Rotate the view for landscape playback
  //   [[self view] setBounds:CGRectMake(0, 0, 480, 320)];
  //  [[self view] setCenter:CGPointMake(160, 240)];
  //  [[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)]; 
 
  // Set frame of movieplayer
  // [[mp view] setFrame:CGRectMake(0, 0, 480, 320)];
  [[mp view] setFrame:CGRectMake(0, 160, 320, 160)];
 
  // Scale uniformly, maintaining aspect ratio
  [mp setScalingMode:MPMovieScalingModeAspectFill];
 
  ...
}
This approach works on 3.2 (iPad) as well as 4.0 and greater. Needless to say, given the large display on the iPad this is a nice alternative as the user can watch videos without rotating the device.
Source Code
Download the source code to build an iPhone app that displays movies in portrait mode.

No comments:

Post a Comment

 
Submit Express Inc.Search Engine Optimization Services