Thursday, March 10, 2011

Movie file playback stored in Project Resources





Introduction


This blog entry adds methods to the UIViewController class for presenting and dismissing a movie player using a specific set of animations. The transitions used by these methods are the same ones used by the YouTube and iPod applications to display video content.


Steps :-


1. Add "MediaPlayer.framework " as existing framework

2. Make another viewController class, suppose we name it "MoviePlayerViewController"

3. In "MoviePlayerViewController.h", add the below code,

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface MoviePlayerViewController : UIViewController 
{ 
MPMoviePlayerController *mp;
NSURL *movieURL;
}
- (id)initWithPath:(NSString *)moviePath;
- (void)readyPlayer;
- (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration;
@end
4. In "MoviePlayerViewController.m", add the below code,
#import "MoviePlayerViewController.h"
#pragma mark -
#pragma mark Compiler Directives & Static Variables
@implementation MoviePlayerViewController
-(void)viewWillDisappear:(BOOL) animated
{  
if(mp) {
[mp stop];
}
}
- (id)initWithPath:(NSString *)moviePath
{ 
mp.controlStyle =  MPMovieControlStyleNone;
// Initialize and create movie URL
if (self = [super init])
{
movieURL = [NSURL fileURLWithPath:moviePath];    
[movieURL retain];
}
return self;
}
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification 
{   
[mp setControlStyle:MPMovieControlStyleFullscreen];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
NSString *deviceModel = [NSString stringWithFormat: @"Device Type: %@\n", [[UIDevice currentDevice] model]];    
NSRange range = [deviceModel rangeOfString:@"iPad"];
if(range.location != NSNotFound){
if ([mp loadState] != MPMovieLoadStateUnknown)
{
[[NSNotificationCenter     defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
//view bounds are defined as par iPad screen, so adjust accordingly
[[self view] setBounds:CGRectMake(0, 0, 1024, 748)];
UIInterfaceOrientation interfaceOrientation = [self interfaceOrientation];
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft ){
[[mp view] setFrame:CGRectMake(0, 0, 1024, 748)];
}
else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight ){
[[mp view] setFrame:CGRectMake(0, 0, 1024, 748)];
}
[[self view] addSubview:[mp view]];   
[mp play];
}
}
else
{  
if ([mp loadState] != MPMovieLoadStateUnknown)
{
[[NSNotificationCenter     defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
UIInterfaceOrientation interfaceOrientation = [self interfaceOrientation];
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight){
[[UIApplication sharedApplication] setStatusBarOrientation:interfaceOrientation animated:NO];
}
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
[[self view] setBounds:CGRectMake(0, 0, 1024, 768)];
[[self view] setCenter:CGPointMake(160, 240)];
[[mp view] setFrame:CGRectMake(0, 0, 1024, 748)];
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
[[self view] addSubview:[mp view]];   
[mp play];
}
}
#endif
}
/*---------------------------------------------------------------------------
* For 3.1.x devices
* For 3.2 and 4.x see moviePlayerLoadStateChanged: 
*--------------------------------------------------------------------------*/
- (void) moviePreloadDidFinish:(NSNotification*)notification 
{   
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[mp play];
}
/*---------------------------------------------------------------------------
* 
*--------------------------------------------------------------------------*/
- (void) moviePlayBackDidFinish:(NSNotification*)notification 
{    
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self dismissModalViewControllerAnimated:NO];    
}
/*---------------------------------------------------------------------------
*
*--------------------------------------------------------------------------*/
- (void) readyPlayer
{   
[[UIApplication sharedApplication] setStatusBarHidden:YES];
mp =  [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
mp.scalingMode= MPMovieScalingModeAspectFit;
if ([mp respondsToSelector:@selector(loadState)]) 
{
// Set movie player layout
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200 
//[mp setControlStyle:MPMovieControlStyleFullscreen];
[mp setControlStyle:MPMovieControlStyleNone];
[mp setFullscreen:YES];
#endif
// May help to reduce latency
[mp prepareToPlay];
// Register that the load state changed (movie is ready)
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
#endif
}  
else
{
// Register to receive a notification when the movie is in memory and ready to play.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePreloadDidFinish:) name:MPMoviePlayerContentPreloadDidFinishNotification object:nil];
//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePreloadDidFinish:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
}
// Register to receive a notification when the movie has finished playing. 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
/*---------------------------------------------------------------------------
* 
*--------------------------------------------------------------------------*/
- (void) loadView
{ 
isFirstLoad = YES;
[self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]];
[[self view] setBackgroundColor:[UIColor blackColor]];
self.view.clipsToBounds = YES;
self.view.autoresizesSubviews = YES;
if(mp) {
[mp stop];
}
UIInterfaceOrientation interfaceOrientation = [self interfaceOrientation];
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight){
[[UIApplication sharedApplication] setStatusBarOrientation:interfaceOrientation animated:NO];
}        
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
- (void)dealloc 
{
[mp release];
[movieURL release];
[super dealloc];
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
#pragma mark -
#pragma mark InterfaceOrientationMethods
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft|| toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
//[self changeTheViewToPortrait:NO andDuration:duration];
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
if(fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
[self changeTheViewToPortrait:NO andDuration:0.0];
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
- (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
if(portrait){
}
else if (isFirstLoad){
isFirstLoad = FALSE;
UIInterfaceOrientation interfaceOrientation = [self interfaceOrientation];
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft ){
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
[[self view] setBounds:CGRectMake(0, 0, 1024, 748)];
}
else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight ){
NSLog(@"UIInterfaceOrientationLandscapeRight");
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
[[self view] setBounds:CGRectMake(0, 0, 1024, 748)];
}
}
[UIView commitAnimations];
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
@end

5. Now wherever , you want to play the movie file, add the below code,
NSString *movieFile=[[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"];
    
 // Create custom movie player   
 moviePlayer = [[[MoviePlayerViewController alloc] initWithPath:movieFile] autorelease];
    
 [self presentModalViewController:moviePlayer animated:NO];
 [moviePlayer readyPlayer]; 
In this class, import "MoviePlayerViewController.h" & "moviePlayer" is a global object of MoviePlayerViewController

No comments:

Post a Comment