Thursday, May 5, 2011

Playing Video file in Iphone


Let’s start by looking at a short video of the application. The basic idea to make this work is simple, your video content viewable in your simulator.
Step 1: Create a new project from Xcode using View-base application.
Step 2: We need to add some code in the header file.
#import <MediaPlayer/MediaPlayer.h>
#import <UIKit/UIKit.h> @interface Tabbar1ViewController : UIViewController { MPMoviePlayerController *moviePlayer; NSURL *movieURL; }
Step 3: Now you add MediaPlayer.framework in your framework folder.Only shows up in the iPhone SDK frameworks folder, at  /Developer/platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.0.sdk/System/Library/Frameworks.


Step 4: In the controller file, 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.

- (void) moviePlayBackDidFinish:(NSNotification*)notification 
{       
 [[NSNotificationCenter defaultCenter] removeObserver:self
 name:NULL
 object:nil];
 [movieURL release];
 [moviePlayer release];
}

-(void)viewWillAppear:(BOOL)animated
{
 NSString *path = [[NSBundle mainBundle] pathForResource:@"video" 
    ofType:@"mov"];      
 movieURL = [NSURL fileURLWithPath:path];

 moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[moviePlayer setMovieControlMode:MPMovieControlModeHidden];  

 [[NSNotificationCenter defaultCenter] addObserver:self 
 selector:@selector(moviePlayBackDidFinish:) 
 name:MPMoviePlayerPlaybackDidFinishNotification 
 object:nil];

 [moviePlayer play];
}
 
Step 5: Now compile and run your application on the simulator

No comments:

Post a Comment