Monday, August 8, 2011

How to Play Video in iPhoneOS4


Step 1: Create a View base application using template. Give the application name “VideoPlayiPhoneOS4”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Xpand classes and notice Interface Builder created the VideoPlayiPhoneOS4ViewController class for you. Expand Resources and notice the template generated a separate nib,VideoPlayiPhoneOS4ViewController.xib, for the “VideoPlayiPhoneOS4”.

Step 4: We need to add MediaPlayer.framework in the Frameworks folder. Select-> Frameworks folder -> Add ->Existing Frameworks -> then select MediaPlayer.framework.

Step 5: In the VideoPlayiPhoneOS4ViewController.h file , we have created instance of MPMoviePlayerController class, that manage the playback of a movie from a file or from the network, and create a instance of NSURL class . So make the following changes in the file.

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface VideoPlayiPhoneOS4ViewController : UIViewController {
    MPMoviePlayerController *moviePlayer;
        NSURL *movieURL;
       
}

Step 6: Open the VideoPlayiPhoneOS4ViewController.m file and make the following changes:

-(void)viewWillAppear:(BOOL)animated
{
        NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"3idiots.mov" ofType:nil];
        NSURL *url = [NSURL fileURLWithPath:urlStr];
        moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
        [self.view addSubview:moviePlayer.view];
        moviePlayer.view.frame = CGRectMake(0, 0, 300, 400);
        [moviePlayer play];
}

Step 7: Now compile and run the application in the Simulator.



You can Download SourceCode from here VideoPlayiPhoneOS4

No comments:

Post a Comment