I have a video file saved in my app's local directory in the documents folder. I want to playback the file when the user clicks on the item in an embedded table view I created. My code for playing back the video is as follows:
NSString* documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* path = [documentPath stringByAppendingPathComponent:@"DemoRecording.mp4"];
NSURL* movieURL = [NSURL fileURLWithPath: path];
[player.view setFrame: self.view.bounds];
[self.view addSubView: player.view];
[player play];
MPMoviePlayerViewController* moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL: movieURL];
[self presentMoviePlayerViewControllerAnimated: moviePlayer];
The video doesn't play back. The path is correct and the file exists there.
I know this is a repetitive question. I have referred to these links:
Playing a Downloaded Video from the Documents Directory with Cocoa-Touch
MPMoviePlayer load and play movie saved in app documents
But couldn't find a definitive answer.
Please help me out on this one. I am using iOS 5.1.1, if that changes anything.
EDIT:
It does work. I had forgotten to pass the URL to the movie player.
you can play Video from Document Directory like this way:-
-(IBAction)playVideo
{
NSURL *vedioURL;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
NSLog(@"files array %@", filePathsArray);
NSString *fullpath;
for ( NSString *apath in filePathsArray )
{
fullpath = [documentsDirectory stringByAppendingPathComponent:apath];
vedioURL =[NSURL fileURLWithPath:fullpath];
}
NSLog(@"vurl %@",vedioURL);
MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];
[self presentMoviePlayerViewControllerAnimated:videoPlayerView];
[videoPlayerView.moviePlayer play];
}
NOTE
Please note that don't Forget to include requested Framework or connect Delegate
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With