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;
-
- (void) playVideo:(NSString *)fileName
-
{
-
NSString *url = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
-
-
playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
-
-
[[NSNotificationCenter defaultCenter]
-
addObserver:self
-
selector:@selector(movieFinishedCallback:)
-
name:MPMoviePlayerPlaybackDidFinishNotification
-
object:[playerViewController moviePlayer]];
-
-
[self.view addSubview:playerViewController.view];
-
-
//play movie
-
-
MPMoviePlayerController *player = [playerViewController moviePlayer];
-
[player play];
-
}
-
-
// The call back
-
- (void) movieFinishedCallback:(NSNotification*) aNotification {
-
MPMoviePlayerController *player = [aNotification object];
-
[[NSNotificationCenter defaultCenter]
-
removeObserver:self
-
name:MPMoviePlayerPlaybackDidFinishNotification
-
object:player];
-
-
//player.initialPlaybackTime = -1;
-
//[player pause];
-
[player stop];
-
-
[player.view removeFromSuperview];
-
-
[player release];
-
// call autorelease the analyzer says call too many times
-
// call release the analyzer says incorrect decrement
-
}
2 Comments
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.
Hi,
Could you try the snippets at: http://snipd.net/play-a-movie-in-fullscreen-objective-c .
Regards,
Ferhat
One Trackback
[...] 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 [...]