(Objective C) Play a video in full screen

1. include MediaPlayer framework in the project
2. in the view controller header file,
#import
3. declare the player instance in the controller header file:
MPMoviePlayerViewController *playerViewController;

  1. - (void) playVideo:(NSString *)fileName
  2. {
  3.     NSString *url = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
  4.  
  5.     playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
  6.    
  7.     [[NSNotificationCenter defaultCenter]
  8.          addObserver:self
  9.          selector:@selector(movieFinishedCallback:)
  10.          name:MPMoviePlayerPlaybackDidFinishNotification
  11.          object:[playerViewController moviePlayer]];
  12.    
  13.     [self.view addSubview:playerViewController.view];
  14.    
  15.     //play movie
  16.  
  17.     MPMoviePlayerController *player = [playerViewController moviePlayer];
  18.     [player play];         
  19. }
  20.  
  21. // The call back
  22. - (void) movieFinishedCallback:(NSNotification*) aNotification {
  23.     MPMoviePlayerController *player = [aNotification object];
  24.     [[NSNotificationCenter defaultCenter]
  25.          removeObserver:self
  26.          name:MPMoviePlayerPlaybackDidFinishNotification
  27.          object:player];
  28.        
  29.     //player.initialPlaybackTime = -1;
  30.     //[player pause];
  31.     [player stop];
  32.    
  33.         [player.view removeFromSuperview];    
  34.    
  35.     [player release];    
  36.     // call autorelease the analyzer says call too many times
  37.     // call release the analyzer says incorrect decrement
  38. }
This entry was posted in Objective-C and tagged , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Subhadeep
    Posted October 5, 2012 at 11:36 am | Permalink

    Hi, I am using similar kind of code for playing a video and it is playing perfectly but when I checked this using Instrument I have got Leak. I have searched many places but none of the articles are resolve my problem. Please help me in this regards.

  2. Ferhat
    Posted October 6, 2012 at 11:02 am | Permalink

    Hi,

    Could you try the snippets at: http://snipd.net/play-a-movie-in-fullscreen-objective-c .

    Regards,

    Ferhat

One Trackback

  1. By Play a movie in fullscreen (Objective-C) on October 6, 2012 at 11:00 am

    [...] reply to http://snipd.net/objective-c-play-a-video-in-full-screen#comment-290 . The following snippets show how to play a movie in [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Why ask?