EDIT: MPMoviePlayerController
is Deprecated Now. So I have used AVPlayerViewController
. and written the following code:
NSURL *videoURL = [NSURL fileURLWithPath:filePath];
//filePath may be from the Bundle or from the Saved file Directory, it is just the path for the video
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
//[playerViewController.player play];//Used to Play On start
[self presentViewController:playerViewController animated:YES completion:nil];
Please do not forget to import following frameworks:
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
You can use MPMoviePlayerController
to play local file.
1. Add Mediaplayer
framework and do #import <MediaPlayer/MediaPlayer.h>
in your viewController.
2. Drag and drop your video file you created on desktop into the xcode.
3. Get the path of the local video.
NSString*thePath=[[NSBundle mainBundle] pathForResource:@"yourVideo" ofType:@"MOV"];
NSURL*theurl=[NSURL fileURLWithPath:thePath];
4. Initialize the moviePlayer with your path.
self.moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:theurl];
[self.moviePlayer.view setFrame:CGRectMake(40, 197, 240, 160)];
[self.moviePlayer prepareToPlay];
[self.moviePlayer setShouldAutoplay:NO]; // And other options you can look through the documentation.
[self.view addSubview:self.moviePlayer.view];
5. To control what is to be done after playback:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playBackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
//playBackFinished will be your own method.
EDIT 2: To handle completion for AVPlayerViewController
rather than MPMoviePlayerController
, use the following...
AVPlayerItem *playerItem = player.currentItem;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playBackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
In this example, I dismiss the AVPlayerViewController
after completion:
-(void)playBackFinished:(NSNotification *) notification {
// Will be called when AVPlayer finishes playing playerItem
[playerViewController dismissViewControllerAnimated:false completion:nil];
}