I am trying to play a video stored in my application's documents directory through MPMoviePlayerController. I am using the following method to play video:
NSArray *directoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [directoryPath objectAtIndex:0];
int videoNumber = (int)[[NSUserDefaults standardUserDefaults] integerForKey:#"videoSaved"];
NSString* videoName = [NSString stringWithFormat:#"video-%d.mov",videoNumber];
NSString *exportPath = [docsDir stringByAppendingPathComponent:videoName];
NSURL *exportUrl = [NSURL fileURLWithPath:exportPath isDirectory:NO];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:exportUrl];
moviePlayer.fullscreen = YES;
moviePlayer.view.frame = self.view.bounds;
[self.view addSubview:moviePlayer.view];
[moviePlayer prepareToPlay];
[moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieDidExitFullscreen:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
It works fine on all iOS versions except iOS7. In iOS7 when i try to play video it ives me this error:
_itemFailedToPlayToEnd: { kind = 1; new = 2; old = 0;}
Try to change your code to this:
int videoNumber = (int)[[NSUserDefaults standardUserDefaults] integerForKey:#"videoSaved"];
moviePlayer= [[MPMoviePlayerController alloc] initWithContentURL:
[NSURL fileURLWithPath: [[NSBundle mainBundle]
pathForResource: [NSString stringWithFormat:#"video-%d",videoNumber] ofType:#"mov"]]];
moviePlayer.fullscreen = YES;
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
moviePlayer.view.frame = self.view.bounds;
[self.view addSubview:moviePlayer.view];
[moviePlayer prepareToPlay];
[moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieDidExitFullscreen:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
Related
How to display a recorded video as repeat as an'n' number of times like in Vine Application.
Here I use the MPMoviePlayerViewController, and works great display the recorded video. But the problem is, it doesn't repeating.
Here the currently using code is,
NSURL *url = [NSURL fileURLWithPath:videoPath];
playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playerController];
[playerController.moviePlayer prepareToPlay];
playerController.view.frame = CGRectMake(200, 402, 300, 200);
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
playerController.moviePlayer.controlStyle = MPMovieControlStyleNone;
playerController.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
playerController.moviePlayer.repeatMode = MPMovieRepeatModeOne;
[self.view addSubview: playerController.view];
[playerController.moviePlayer play];
NSLog(#"repeatMode: %d",playerController.moviePlayer.repeatMode);
[playerController.view addSubview:customview];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[playerController moviePlayer]];
And the NSNotification code,
- (void) movieFinishedCallback:(NSNotification*) aNotification
{
NSLog( #"myMovieFinishedCallback: %#", aNotification );
MPMoviePlayerController *movieController = aNotification.object;
NSLog( #"player.playbackState = %d", movieController.playbackState );
}
Can anyone please give the solution..
Note:
I'm using the XCode 4.5.2 tool and ios simulater 6.0
Try this code. It is working.
NSURL *fileUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Video" ofType:#"mp4"]];
MPMoviePlayerViewController *moviePlayerController = [[MPMoviePlayerViewController alloc]initWithContentURL:fileUrl];
[moviePlayerController.moviePlayer prepareToPlay];
[moviePlayerController.moviePlayer setRepeatMode:MPMovieRepeatModeOne];
[moviePlayerController.moviePlayer setControlStyle:MPMovieControlStyleEmbedded];
[self.view addSubview:moviePlayerController.view];
you can try this code
-(void) movieFinishedCallback:(NSNotification *) aNotification
{
if (aNotification.object == self.moviePlayer) {
NSInteger reason = [[aNotification.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
if (reason == MPMovieFinishReasonPlaybackEnded)
{
[playerController play];
}
}
}
You have to keep the MPMoviePlayerController object as a member, otherwise the app would lose context to it and the movie will not loop.
if (indexPath.row==0)
{
NSString *urlAsString = [[NSBundle mainBundle] pathForResource: [self.array objectAtIndex:indexPath.row] ofType:#"mov"];
NSURL *url = [NSURL fileURLWithPath:urlAsString];
MPMoviePlayerController *myPlayer = [[MPMoviePlayerController alloc] init];
myPlayer.shouldAutoplay = YES;
myPlayer.repeatMode = MPMovieRepeatModeOne;
myPlayer.fullscreen = YES;
myPlayer.movieSourceType = MPMovieSourceTypeFile;
myPlayer.scalingMode = MPMovieScalingModeAspectFit;
myPlayer.contentURL =[NSURL fileURLWithPath:urlAsString];
[self.view addSubview:myPlayer.view];
[myPlayer.view setFrame:CGRectMake(0, 0, 320, 400)];
[myPlayer setFullscreen:YES animated:YES];
[myPlayer play];
}
i have one button in MPMoviePlayerController called done when i click on done button i need to come back to my tableview plz help me any one
MPMoviePlayerController *myPlayer declare global and init . and
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(doneButtonClick:)
name:MPMoviePlayerWillExitFullscreenNotification
object:nil];
And selector method:
-(void)doneButtonClick:(NSNotification*)aNotification{
[myPlayer stop];
}
i got a project that someone else write.
the project saves video file in this path,file://localhost/var/mobile/Applications/CAC8F2CB-1C7D-4805-BF1A-42B63B258E95/Documents/output.mp4
i want to play the file with MPMoviePlayerController, how can i access the file?
Firslty load the path and file URL:
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *localFilePath = [photoDirectoryPath stringByAppendingPathComponent:#"output.mp4"];
Then load up MPMoviePlayerController with the URL
- (void)initMoviePlayer
{
didExit = NO;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePreloadDidFinish:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:mMoviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
NSLog(#"initMoviePlayer: %# ",[self getMovieURL]);
mMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self getMovieURL]];
mMoviePlayer.shouldAutoplay = YES;
mMoviePlayer.view.hidden = YES;
[mMoviePlayer setControlStyle:MPMovieControlStyleNone];
mMoviePlayer.view.frame = [self.view bounds];
mMoviePlayer.scalingMode = MPMovieScalingModeNone;
[[self view] addSubview:[mMoviePlayer view]];
[mMoviePlayer play];
}
- (void) moviePreloadDidFinish:(NSNotification*)notification {
// start playing the movie
mMoviePlayer.view.hidden = NO;
animateTimer = [NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:#selector(playMovie:)
userInfo:nil
repeats:NO];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
}
Try this code mate,
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
NSString *videoFilePath = [documentDirectory stringByAppendingPathComponent:#"output.mp4"];
NSURL *videoURL = [NSURL URLWithString:videoFilePath];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
Try this :-
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:#"output.mp4"];
Here in fullPath you will have path you mentioned above.
For more detail on how to play video, refer this tutorial
Hope this helps you..
If you store the above URL somewhere
NSString *fullURL = #"/var/mobile/Applications/CAC8F2CB-1C7D-4805-BF1A-42B63B258E95/Documents/output.mp4"
NSURL *movieURL = [NSURL fileURLWithPath: isDirectory:YES]; //If YES not works use NO
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
Hope that helps
I have a MPMoviePlayerViewController to play Movies in fullscreen. I checked the movies they play fine via Quicktime.
The Problem is, that using the MPMoviePlayerViewController (Simulator and device) the Movie doesn't start playing(it's a video that is stored locally on the iPad btw).
NSString *path = [[NSBundle mainBundle]pathForResource:resource ofType:#"mov"];
self.mpviewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:path]];
[self.mpviewController.moviePlayer prepareToPlay];
self.mpviewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
self.mpviewController.moviePlayer.controlStyle = [self presentModalViewController:self.mpviewController animated:YES];
NSLog(#"%d",self.mpviewController.moviePlayer.loadState);
[self.mpviewController.moviePlayer play];
Any Ideas what I am missing ?
tia
well, I dont know what it was. I ended up writing a new view controller with this code - that worked:
NSURL *url = [NSURL fileURLWithPath:self.urlPath];
self.playerController = [[MPMoviePlayerController alloc] initWithContentURL: url];
CGRect bounds = CGRectMake(0, 0, 1024, 748);
self.playerController.scalingMode = MPMovieScalingModeNone;
[self.playerController.view setFrame:bounds];
[self.view addSubview:self.playerController.view];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerDidExitFullscreenCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.playerController];
self.playerController.controlStyle = MPMovieControlStyleFullscreen;
[self.playerController setFullscreen:YES animated:YES];
[self.playerController play];
I am having issues getting an embedded video file to play using MPMoviePlayerController. My code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *proud = [[documentsDirectoryPath stringByAppendingPathComponent:#"archives"] stringByAppendingPathComponent:selectedCountry];
NSString *content = [proud stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL fileURLWithPath:content] retain];
NSLog(#"%#", url);
player =
[[MPMoviePlayerController alloc] initWithContentURL: url];
[player prepareToPlay];
player.allowsAirPlay = YES;
player.scalingMode = MPMovieScalingModeAspectFit;
player.view.frame = self.view.frame;
[self.view addSubview: player.view];
[player setFullscreen:YES animated:YES];
// ...
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:player];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerWillExitFullscreen:)
name:MPMoviePlayerWillExitFullscreenNotification
object:player];
[player play];
The NSLog returns
var/mobile/Applications/EF62B00B-2906-435C-BC84-036FE14D89E9/Documents/archives/Test%2520-%2520Daddy%2520May%25201%252001:26:35%2520PM.mp4
But the video never plays, though MovieDone does get triggered from the log I use in MPMoviePlayerController MovieFinished Callback. Any thoughts?
i think you play only video file then try this code :
NSString *urlStr=[[NSBundle mainBundle]pathForResource:#"Video1VoiceOver.mp4" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(174,154,720,428);
[moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
Try this
MPMoviePlayerViewController *playerViewController =[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:#"FileName"]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:)name:MPMoviePlayerPlaybackDidFinishNotification object:[playerViewController moviePlayer]];
playerViewController.view.frame=CGRectMake(Set the frame);
[self.view addSubview:playerViewController.view];
//---play movie---
player = [playerViewController moviePlayer];