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];
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