Thursday, 28 March 2013

How to add a video clip in a ios app.

First of all add MediaPlayer/MediaPlayer framework into ur project.

Then drag your video into project resource folder.

then add the below code in viewDidLoad() method or as per your requirement:

- (void)viewDidLoad

 [super viewDidLoad];

 NSString *url = [[NSBundle mainBundle]
    player =[[MPMoviePlayerController alloc]
             initWithContentURL:[NSURL fileURLWithPath:url]];
    player.controlStyle=NO;                      // to add or remove controls eg. stop / play
    player.movieSourceType = MPMovieSourceTypeFile;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieViewFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
    //---play movie---//
    player.view.frame = CGRectMake(0, 0, 1024  , 768);  
    [self.view addSubview:player.view];  // to add player view on ur view
    [player play];


IF you want to perform some action just after video completion then add below delegate method:

    MPMoviePlayerController *moviePlayer=[theNotification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self

    [moviePlayer.view removeFromSuperview];     // to remove player view

    [self yourMethod];                                         // call ur method

you must add delegate into your interface declaration


@interface ViewController : UIViewController <MPMediaPickerControllerDelegate>